139

I'm currently writing a project report, and to explain the slightly crazy un-intuitive code that I've used I need to put lots and lots of short excerpts of example code in.

How I can get code into LaTeX that:

  • looks nice
  • doesn't go off the side of the page if the line's too long (list lstlisting or verbatim)
  • is preferably inline with the rest of the text

EDITED (Figured I'd add the settings so people don't have to try and figure them out themselves (taken from wikibooks (link further down) and edited for niceness))

Nice settings:

\usepackage{color}
\usepackage{listings}
\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 is 1 each line will be numbered
numbersep=5pt,                  % how far the line-numbers are from the code
backgroundcolor=\color{white},  % 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
}

\begin{lstlisting}
!!code!!
\end{lstlisting}

An example of these settings:

Jason Kleban
  • 20,024
  • 18
  • 75
  • 125
Ed James
  • 10,385
  • 16
  • 71
  • 103
  • lstlisting *can* do line wrap, you just need to use the correct lstset command. – Joachim Sauer Feb 25 '09 at 17:34
  • Yes, I eventually found something that told me that. Cheers though! – Ed James Feb 25 '09 at 17:54
  • 1
    Wonderful! I love to be able to just copy-paste something into my latex-document and have it become beautiful instantly!You saved me for some hours of tweaking now. Thanks! :) – Espen Herseth Halvorsen May 27 '10 at 14:44
  • can lstlisting show only few line numbers. If the listing has 20 lines and I want to display all the source code lines but display selected line *numbers*. Is this possible? I don't want latex commands within source code. – thequark Jul 22 '10 at 08:13
  • @thequark sorry, it's been a while since I used LaTeX (posted this Q over a year ago!), try asking as a seperate question, someone else might be able to help. – Ed James Jul 22 '10 at 11:07
  • 12
    A screenshot of the formatted code produced by the above would be nice! – Adrian Mouat Nov 22 '10 at 10:06
  • The [listings](http://www.ctan.org/tex-archive/macros/latex/contrib/listings/) package is quite nice and very flexible (e.g. different sizes for comments and code). – Fabian Steeg Feb 25 '09 at 15:51
  • After some lengthy googling, it turns out that lstlisting is able to format code nicely, but it requires a lot of tweaking. Wikibooks has a good example for the parameters you can tweak. (Sorry to answer my own question ! ) – Ed James Feb 25 '09 at 17:25
  • Seriously, this got closed after THREE YEARS of being open? I appreciate that this would be considered off-topic(ish) now, even though LaTeX is basically a programming/scripting language, however, when this question was first asked the community was much more forgiving... – Ed James Feb 25 '13 at 11:32
  • 7
    LaTeX is Turing complete, this should not be closed.. – dcow Mar 30 '13 at 16:28

3 Answers3

43

The listings package is quite nice and very flexible (e.g. different sizes for comments and code).

Fabian Steeg
  • 44,988
  • 7
  • 85
  • 112
18

It turns out that lstlisting is able to format code nicely, but requires a lot of tweaking.

Wikibooks has a good example for the parameters you can tweak.

Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
Ed James
  • 10,385
  • 16
  • 71
  • 103
2

For simple document, I sometimes use verbatim, but listing is nice for big chunk of code.

claf
  • 9,043
  • 17
  • 62
  • 79