View Full Version : LaTeX and displaying code in document

May 10th, 2007, 06:14 PM

I need to display code in my LaTeX document and I have problems with long code snippets. I use LaTeX-code like this:

\caption{This is the caption text for the code.}

This works ok with short codes, but longer codes are moved to the end of the document for some reason. Even though code and caption fits to one page. Does anyone have any good suggestions how to display code at LaTeX?

May 10th, 2007, 06:40 PM
I found a some kind of solutions to this problem. Now I use

\caption{Caption for the code.}

May 10th, 2007, 07:32 PM
I always use the 'listings' package.

You can find the manual here

You can tell it which language to display. It can input from source code files or you can insert code directly/manually.It also break lines nicely.

May 10th, 2007, 07:46 PM
I always use the 'listings' package.
Thanks for the tip. This sounds interesting although it won't recognize my code syntax, because this is completely new language that I discuss in my masters thesis. But I will take a look. Thanks

May 11th, 2007, 12:00 PM
You can define your own language with \lstdefinelanguage in the .tex or \lst@definelanguage in a .sty file. You can see how it is done in existing lstlangN.sty, where N is 1,2 or 3 on my installation (using texlive). lstlang files are located in /usr/share/texmf-texlive/tex/latex/listings (again in my installation).