Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

I have this urgent need to display two pieces of code side by side for comparison in my article. Displaying a code, I've couple of requirement

  1. Should indent properly
  2. Output should be syntax highlighted
  3. Should be as automatic as possible in handling long lines of code (that means, wrapping the lines if necessary)
  4. Code should be easily copied from output pdf
  5. Code can be arbitrarily long, i.e. spanning over many pages, so automatic handling of such thing would be preferred

I have explored two choices briefly, Verbatim and listing along with minipage. They sort of work alright! But only the output is absolutely hideous. What is a good setting for any of the two, which atleast would produce good looking output. For automation part, I can still write python scripts that would allow me to do that. If any of you know example of research articles that renders code in it beautifully- I'd appreciate if you share it with me. Keep in mind that I don't use Latex often.

share|improve this question

marked as duplicate by zeroth, Herbert, Kurt, Claudio Fiandrino, lockstep Mar 8 '13 at 19:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
Did you try package listings? –  Eddy_Em Mar 8 '13 at 18:15
1  
Can this help you? tex.stackexchange.com/questions/22988/… –  Dave Mar 8 '13 at 18:22
    
See Print programs with its proper syntax for a list of possibilities when printing source code. With your requirements, I would suggest that listings is the way to go (since it accommodates page breaking, syntax highlighting and line wrapping). See, for example, listings code style for HTML5 (CSS, HTML, JavaScript). –  Werner Mar 8 '13 at 18:25

1 Answer 1

Here is an example of package listings usage (for Octave's m-files):

\usepackage{listings}
\lstset{basicstyle=\small,breaklines=true,language=Octave,
    extendedchars=true,aboveskip=1em,belowcaptionskip=5pt,
    prebreak = \hbox{%
\normalfont\small\hfill\green{\ensuremath{\hookleftarrow}}},
    postbreak = \hbox to 0pt{%
\hss\normalfont\small\green{\ensuremath{\hookrightarrow}}\hspace{1ex}},
    commentstyle=\color{blue},showspaces=false,
    showstringspaces=false,stringstyle=\bfseries\color[rgb]{0.6,0,1},
    numbers=left,numberstyle=\tiny,stepnumber=2,
    keywordstyle=\bfseries\color[rgb]{0,0.1,0.5},
    frameround=tttt,frame=trBL,tabsize=4,backgroundcolor=\color[rgb]{.9,.9,1}}
\lstloadlanguages{Octave}
\def\lstlistingname{Listing}
\def\lstref#1{(look~listing~\ref{#1})}
\begin{document}
\lstinputlisting[caption={Caption for listing},label=alabel]{file.m}

Read documentation of listings: you can adjust colors, line numbering, boxes around listings and many other things.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.