Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex + Source Code Import

Tags:

latex

I'm using Latex to write a program listing all my code and I am following this:

http://texblog.wordpress.com/2008/04/02/include-source-code-in-latex-with-listings/

It works, but my code runs of the side of the page. How can I fix this? Additional question: How can I get it to highlight syntax? I do have lang set to Java.

like image 226
KP65 Avatar asked Apr 22 '10 12:04

KP65


2 Answers

Try something like this:

\documentclass{article}

\usepackage{listings}
\usepackage{color}
\usepackage{textcomp}
\definecolor{listinggray}{gray}{0.9}
\definecolor{lbcolor}{rgb}{0.9,0.9,0.9}
\lstset{
    language=c,
    basicstyle=\scriptsize,
    upquote=true,
    aboveskip={1.5\baselineskip},
    columns=fullflexible,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    identifierstyle=\ttfamily,
    keywordstyle=\color[rgb]{0,0,1},
    commentstyle=\color[rgb]{0.133,0.545,0.133},
    stringstyle=\color[rgb]{0.627,0.126,0.941},
}

\begin{document}

\begin{lstlisting}

#include <stdio.h>

int main() 
{
    // A line comment
    printf("A really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, really, REALLY long line. && & \n");
    return 0;
}

\end{lstlisting}

\end{document}

which produces:

alt text http://img260.imageshack.us/img260/1608/codes.png

like image 111
Bart Kiers Avatar answered Sep 21 '22 07:09

Bart Kiers


Did you consider

\lstset{...}
breaklines=true -> sets automatic line breaking
breakatwhitespace=false -> automatic breaks happen at whitespace

?

like image 33
Tobias Kienzler Avatar answered Sep 21 '22 07:09

Tobias Kienzler