C source code in a Latex document

Can someone recommend me a good template to include C source code with line numbering in latex? For example, taking the classic Hello World program, I would like to make it as follows:

(1) /* Hello World program */ (2) (3) #include<stdio.h> (4) (5) main() (6) { (7) printf("Hello World"); (8) } 

Typicall, I have always used verbatim environment, but I am wondering if there is a better and better way to do this.

Thanks a lot Richard

+7
source share
5 answers

Maybe you should take a look at the listings package. It is very flexible and easy to use.

+9
source

As others have said, the listings package is likely to do what you want using something like the following:

 \lstset{ language=C, % choose the language of the code numbers=left, % where to put the line-numbers stepnumber=1, % the step between two line-numbers. numbersep=5pt, % how far the line-numbers are from the code backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color} showspaces=false, % show spaces adding particular underscores showstringspaces=false, % underline spaces within strings showtabs=false, % show tabs within strings adding particular underscores tabsize=2, % sets default tabsize to 2 spaces captionpos=b, % sets the caption-position to bottom breaklines=true, % sets automatic line breaking breakatwhitespace=true, % sets if automatic breaks should only happen at whitespace title=\lstname, % show the filename of files included with \lstinputlisting; } \lstinputlisting{HelloWorld.c} 

A more powerful alternative would be to use a minted package, although it will be much more than what you are currently requesting, since it uses / requires the installation of fragments in your system so that it can completely tokenize the code that you give it.

+9
source

Take a look at the code listings in LaTeX . There you will find a couple of alternatives. Some options:

+1
source

Use the lgrind package for latex. It will convert your code to a .tex file

0
source

CWEB had a good formatter C.

-one
source

All Articles