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.
This macro will consecutively store any information that you wish to replace with the
lstnumber
counter.So calling:
will globally change lines 105 and 107 to 3.1, 3.1.5, respectively.
Thus the following
will produce:
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 usenumbers=none
as that will never execute\thelstnumber
.Thus the code looks like this:
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 page7
. Hence I of course used it on\thepage
. :)I just expanded it to do other things as well.