[Tex/LaTex] Custom line numbers in lstlisting environment

line-numberinglistings

I am using the lstlisting environment to display code and it works great, however for my current application I need something like:

 line 1;    
 3.1: line 2;     
 line 3;     
 3.1.5: line 4;     
 line 5;

e.g. I want numbers on some lines and I have to decide the numbers. I know that I can just type them in like in the above box but it will look very bad as the lines don't match up anymore. I read the lstlisting documentation and it didn't bring anything like this up. Is there some workaround here only more experienced users know?

Best Answer

I dont think you can do without some manual labour, if you don't have a system of numbering. However you can trick listings to use other values.
For reference it is the counter lstnumber which holds the current line number.

So the solution is to do conditional checking with the counter and insert the correct format, or reference.

I have created a macro which collects information on the line number and inserts the correct format upon the line number reached.

% Save the original way of printing the number
\let\othelstnumber=\thelstnumber
\def\createlinenumber#1#2{
    \edef\thelstnumber{%
        \unexpanded{%
            \ifnum#1=\value{lstnumber}\relax
              #2%
            \else}%
        \expandafter\unexpanded\expandafter{\thelstnumber\othelstnumber\fi}%
    }
    \ifx\othelstnumber=\relax\else
      \let\othelstnumber\relax
    \fi
}

This macro will consecutively store any information that you wish to replace with the lstnumber counter.

So calling:

\createlinenumber{105}{3.1}
\createlinenumber{107}{3.1.5}

will globally change lines 105 and 107 to 3.1, 3.1.5, respectively.

Thus the following

\documentclass{article}
\usepackage{listings}
\begin{document}
% Save the original way of printing the number
\let\othelstnumber=\thelstnumber
\def\createlinenumber#1#2{
    \edef\thelstnumber{%
        \unexpanded{%
            \ifnum#1=\value{lstnumber}\relax
              #2%
            \else}%
        \expandafter\unexpanded\expandafter{\thelstnumber\othelstnumber\fi}%
    }
    \ifx\othelstnumber=\relax\else
      \let\othelstnumber\relax
    \fi
}

\bgroup
\createlinenumber{105}{3.1}
\createlinenumber{107}{3.1.5}
\begin{lstlisting}[numbers=left,firstnumber=100,numberstyle=\ttfamily]
  begin { empty lines }








  end; { empty lines }
\end{lstlisting}
\egroup

\end{document}

will produce:

output

Notice the use of \bgroup and \egroup to not being globally changed.

Only manually selected lines

If you do not wish to display any other than yours provided you simply need to tell TeX to discard those. Notice that you cannot use numbers=none as that will never execute \thelstnumber.
Thus the code looks like this:

\def\createlinenumber#1#2{
    \edef\thelstnumber{%
        \unexpanded{%
            \ifnum#1=\value{lstnumber}\relax
              #2%
            \fi}%
        \ifx\thelstnumber\relax\else
        \expandafter\unexpanded\expandafter{\thelstnumber}%
        \fi
    }
}

However the above requires an initialisation before first use. You then have to add: \let\thelstnumber\relax after the first \bgroup but before the first execution of \createlinenumber.

As bonus info. This was originally intended for a friends thesis who would love to have 007 printed on page 7. Hence I of course used it on \thepage. :)
I just expanded it to do other things as well.

Related Question