1

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: enter image description here

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.

Please some advise how I could overcome this problem.

2

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},
}
\renewcommand{\thelstnumber}{\padzeroes[5]{\decimal{lstnumber}}}

\begin{document}
\begin{lstlisting}[firstnumber=19]
int i; /*!\color{red}/// Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam commodo mattis orci, id gravida leo dignissim condimentum.\label{lst:inti}\stepcounter{lstnumber}!*/
/*!\color{blue}float!*/ /*!\color{green}j!*/; /// /*!\color{pink}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{lstlisting}
In line~\ref{lst:inti}, you see the definition of an integer.
\end{document}

enter image description here

  • Good answer, looks nice. Missing the part about the hyperlink / hypertarget. Furthermore I would like to have the control over the coloring of the different parts. Not mentioned before (my bad) is that subsequent lines in the code part don't have consecutive numbers (due to the fact that certain comment are removed by the generating program). – albert Apr 13 '18 at 10:47
  • @albert: Find an updated answer. I just forgot the hyperlinks (very straightforward with listings). And I also adressed your other issues. Probably not the perfect solution you are looking for, but the best I can achieve with listings. – Tiuri Apr 13 '18 at 12:08
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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