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.

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?

share|improve this question
    
Yes I will specify them manually and they are at ad-hoc intervals. I want to do everything by hand. –  user1912 Jan 23 '12 at 21:04
add comment

1 Answer

up vote 5 down vote accepted

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.

share|improve this answer
    
This seems to be perfectly what I need. But I need the other numbers to disappear, is that possible too? :-) –  user1912 Jan 23 '12 at 21:04
1  
I have updated the answer to include that as well. –  zeroth Jan 23 '12 at 21:25
    
Thank you so much! Now it is perfect. –  user1912 Jan 23 '12 at 21:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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