LaTeX Listing Package Last Line Number

I use LaTeX with the listings package, and I count every fifth line of code, having:

 \lstset{ numbers=left, stepnumber=5, firstnumber=1, numberfirstline=true } 

Now, due to numberfirstline=true in these settings, in addition to a multiple of five, the first line of each listing is also numbered, but I would also like to number the last line of code. Therefore, if I have a list with 17 lines of code, the lines that I would like to number are 1 5 10 15 17 .

Is there a way I can make lists behave like this? I tried numberlastline=true , but this does not seem to exist.

Edit: I would prefer to use this with \lstinputlisting and without having to change the code that is imported this way.

+5
source share
1 answer

Actually, since you are using numberfirstline=true , this is easier to do than you might think. The listings package provides a mechanism for exiting shorthand mode in the middle of the list via escapeinside to be able to insert additional macros. The trick here is that after returning from the escaped code, it again treats the next line as the β€œfirst” line, even if there is no count reset. Thus, all you have to do is exit (and do nothing!) On the second to the last line of the listing. If you do this for the last time, you will be late! The example below demonstrates the use of this to get what you want.

 \documentclass{article} \usepackage{listings} \lstset{ numbers=left, stepnumber=5, firstnumber=1, numberfirstline=true escapeinside={|(}{)|} } \begin{document} \begin{lstlisting} lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt|()| mollit anim id est laborum. \end{lstlisting} \end{document} 

listing with the number in the last row

You can also check out this question about randomly changing line numbering .

UPDATE

This requires a modification to the listing source and, as such, may not be sufficient when using \lstinputlisting . An alternative approach is to change the logic of the listing package itself to indicate the last line when it is explicitly indicated using the lastline or linerange , as shown below.

 \documentclass{article} \usepackage{listings} \lstset{ numbers=left, stepnumber=5, firstnumber=1, numberfirstline=true, } \makeatletter \gdef\ lst@SkipOrPrintLabel {% \ifnum\ lst@skipnumbers =\ z@ \global\advance\ lst@skipnumbers- \ lst@stepnumber \relax \ lst@PlaceNumber \ lst@numberfirstlinefalse \else \ifnum\ lst@lineno =\ lst@lastline \ lst@PlaceNumber \ lst@numberfirstlinefalse \fi \ lst@ifnumberfirstline \ lst@PlaceNumber \ lst@numberfirstlinefalse \fi \fi \global\advance\ lst@skipnumbers \@ne} \makeatother \begin{document} \lstinputlisting[lastline=13]{input.tex} \end{document} 

This requires that you manually specify the last line number for each listing, but this seems like a reasonable sacrifice, given the complexity of manipulating temporary files, which could potentially be the only real solution for dynamically detecting the number of lines that only requires pure TeX. If an intolerable solution is acceptable, you can run a shell command, such as wc -l via \write18 , to get the number of lines. Of course, this will require that the selected team be present in all systems that use the source, which can be difficult if these systems span different operating systems. See this question for an explanation of \write18 and this question for how to output it.

It should be noted that when using linerange it will indicate the last line of each individual range, but not the first line for each range when using numberfirstline=true . Although this can be changed in a similar way, linerange already leads to such quirks that if this is a problem, then most likely it will be separate from the problem.

+4
source

Source: https://habr.com/ru/post/1268159/


All Articles