PDA

View Full Version : LaTeX and displaying code in document



Laterix
May 10th, 2007, 06:14 PM
Hi,

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



\begin{figure}[htb]
\begin{center}
\small
\verbatiminput{source_code.java}
\normalsize
\caption{This is the caption text for the code.}
\label{fig:code_example}
\end{center}
\end{figure}


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?

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



\begin{figure}[htbp]
\makebox[\textwidth]{\hrulefill}{
\small
\verbatiminput{source_file.java}
\normalsize}
\caption{Caption for the code.}
\label{fig:code}
\end{figure}

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

You can find the manual here
ftp://tug.ctan.org/pub/tex-archive/macros/latex/contrib/listings/listings.pdf

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.

Laterix
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

tkjacobsen
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).