Results 1 to 5 of 5

Thread: LaTeX and displaying code in document

  1. #1
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    LaTeX and displaying code in document

    Hi,

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

    Code:
    \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?

  2. #2
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: LaTeX and displaying code in document

    I found a some kind of solutions to this problem. Now I use

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

  3. #3
    Join Date
    Dec 2005
    Location
    Copenhagen, Denmark
    Beans
    386
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: LaTeX and displaying code in document

    I always use the 'listings' package.

    You can find the manual here
    ftp://tug.ctan.org/pub/tex-archive/m...s/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.
    Last edited by tkjacobsen; May 10th, 2007 at 07:34 PM.
    Troels Kofoed Jacobsen
    Consult the wiki before posting: wiki.ubuntu.com

  4. #4
    Join Date
    Apr 2005
    Location
    Finland
    Beans
    300
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: LaTeX and displaying code in document

    Quote Originally Posted by tkjacobsen View Post
    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

  5. #5
    Join Date
    Dec 2005
    Location
    Copenhagen, Denmark
    Beans
    386
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: LaTeX and displaying code in document

    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).
    Troels Kofoed Jacobsen
    Consult the wiki before posting: wiki.ubuntu.com

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •