# Displaying source code

I have the following, generated, code:

\documentclass[twoside]{book}

\RequirePackage{alltt}
\RequirePackage{xcolor}

\definecolor{comment}{rgb}{0.5,0.0,0.0}
\definecolor{keywordtype}{rgb}{0.38,0.25,0.125}

\begin{document}

gravida leo dignissim condimentum. Aliquam porta est ac placerat laoreet. Nam faucibus tincidunt augue eget
\par
\scriptsize
\begin{alltt}
00019   \textcolor{keywordtype}{int} i; \textcolor{comment}{/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam commodo mattis orci, id
gravida leo dignissim condimentum. Aliquam porta est ac placerat laoreet. Nam faucibus tincidunt augue eget
faucibus arcu. Aliquam vel enim venenatis nisl malesuada condimentum. Cras vel sollicitudin enim. Nulla sagittis
cursus urna, eget euismod massa vehicula eget. }
\end{alltt}
\normalsize
gravida leo dignissim condimentum. Aliquam porta est ac placerat laoreet. Nam faucibus tincidunt augue eget
\end{document}


when running pdflatex over it this results in:

The generated code did the line breaks and indentation (based on a fixed number of characters on a line). What I would like to have is that the line breaks and indentation happen automatically. So that the 'code' can be written like:

00019   \textcolor{keywordtype}{int} i; \textcolor{comment}{/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam commodo mattis orci, id gravida leo dignissim condimentum. Aliquam porta
est ac placerat laoreet. Nam faucibus tincidunt augue eget faucibus arcu. Aliquam vel enim venenatis nisl malesuada condimentum. Cras vel sollicitudin enim. Nulla sagittis cursus urna, eget euismod ma
ssa vehicula eget. }


As it is a code listing there are some restrictions as well:

• fixed width font
• preserve spacing, especially in regards to multiple spaces
• possibility to color the text (edit: by the user)
• the line number (00019) can have a hyperlink and hypertarget as well as e.g. the variable.
• edit: subsequent lines in the listing don't necessary have to have subsequent numbers (generating program can leave out certain types of comment lines).

I've been looking at the hanging and verbatim but could not get to a result.

This is a solution with the listings package. Some notes:

• If there is any logic in which things you want to have in which color, than use the highlighting mechanism of listings by declaring stuff as keywords, comments, etc. There are lot of questions and answers on this platform on how to customize this. Here, however, I'll show a way to do that manually.
• I define /*! and !*/ as an escape pattern. Inside these, you can use normal LaTeX commands (like changing the text color).
• Inside the escape pattern, you can define \labels that refer to the current line number.
• With the option firstnumber, you can define the value of the first line number. Within the escape pattern, you can add commands to edit the value of the lstnumber counter (like \stepcounter or \setcounter).
• Inside the escape pattern, multiple spaces are not preserved. So try to have as few escape patterns as possible (which is, most easily, achieved by defining as many stuff as possible as keyword etc.)
\documentclass[twoside]{book}

\usepackage{fmtcount}
\usepackage{xcolor}
\usepackage{listings}

\lstset{%
numbers=left,
numberstyle=\tiny,
basicstyle=\scriptsize\ttfamily,
breaklines=true,
escapeinside={/*!}{!*/},
escapebegin={\ttfamily},
}

• Thanks this gives an awful lot good points to continue with. I was already experimenting with escapechar but escapeinside  is far more powerful. I still had some problems with '#' and multiple spaces, but I could escape these quite easily. – albert Apr 13 '18 at 12:44