or you can have it typeset whole files:\usepackage{listings} \lstset{language=C} ... \begin{document} \begin{lstlisting} #include <stdio.h> int main(int argc, char ** argv) { printf("Hello world!\n"); return 0; } \end{lstlisting} \end{document}
These very simple examples may be decorated in a huge variety of ways, and of course there are other languages in the package’s vocabulary than just C… For a long time, advice on (La)TeX lists seemed to regard listings as the be-all and end-all on this topic. In the last few years, viable alternatives have appeared Highlight is attractive if you need more than one output format for your program: as well as (La)TeX output, highlight will produce (X)HTML, RTF and XSL-FO representations of your program listing. The manual leads you through the details of defining a parameter file for a “new” language, as well as the presentation details of a language. The minted package is another alternative that offers the means of creating new language definitions. It requires that documents be pre-processed using an external (python) script, Pygments. Pygments, in turn, needs a “lexer” that knows the language you want to process; lots of these are available, for the more commonly-used languages, and there is advice on “rolling your own” on the <a href=’http://pygments.org/docs/lexerdevelopment/’>Pygments site</a> Longer-established, and variously less “powerful” systems include:\usepackage{listings} \lstset{language=C} ... \begin{document} \lstinputlisting{main.c} \end{document}
This answer last edited: 2011-03-09
This question on the Web: http://www.tex.ac.uk/cgi-bin/texfaq2html?label=codelist