views:

273

answers:

5

Hi guys, I'm using latex to write a program listing of all my code and 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?

Thanks

Edit:

Ok i got it working sort of! How can i get it to highlight syntax? I do have lang set to java

+2  A: 

Did you consider

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

?

Tobias Kienzler
+1  A: 

Use breaklines=true e. g.:

\lstnewenvironment{bash}
{\lstset{language=bash,breaklines=true,frame=trBL}}
{}

Yasir Arsanukaev
+2  A: 

You want to turn on line breaking with

\lstset{breaklines=true} 

in the command options. Now you might no like its choices but that is another question.

Ukko
+4  A: 

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

Bart Kiers
KP65
ok thanks, edited the op
KP65
An aesthetic note: columns=fixed often gives bad results (like here, the letters are spaced too much). I prefer columns=fullflexible, even with a fixed-width font, it gives a much more natural look to the code.
Damien Pollet
@Damien: agreed, that looks better! Edited my answer.
Bart Kiers
+2  A: 

Instead of including the source directly, include it from a file:

\lstinputlisting{/Volumes/docs/p2k_files_in_qt.sh}

You'll save lots of work doing it that way. Alternatively, if you have to include source into the file, there's lgrind.

\lstset{ %
language=C,                             % choose the language of the code
basicstyle=\footnotesize,       % the size of the fonts that are used for the code
numbers=left,                   % where to put the line-numbers
numberstyle=\footnotesize,      % the size of the fonts that are used for the line-numbers
stepnumber=1,                   % the step between two line-numbers. If it's 1 each line will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
%backgroundcolor=\color{Blue},  % choose the background color. You must add \usepackage{color}
showspaces=false,               % show spaces adding particular underscores
showstringspaces=false,         % underline spaces within strings
showtabs=false,                 % show tabs within strings adding particular underscores
frame=single,                   % adds a frame around the code
tabsize=2,                          % sets default tabsize to 2 spaces
captionpos=b,                   % sets the caption-position to bottom
breaklines=true,                % sets automatic line breaking
breakatwhitespace=false,        % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)},         % if you want to add a comment within your code
% size, font
commentstyle=\fontsize{7}{7}\selectfont,
basicstyle=\ttfamily\fontsize{7}{7}\selectfont,
keywordstyle=\color{red},
commentstyle=\color{blue},
stringstyle=\color{green}
}
wishi