Does anybody know how to use labels inside a code listing that it looks something similarly like this:
I know that it's easier to use the line number but I prefer using some labels.
cross-referencinglistings
Does anybody know how to use labels inside a code listing that it looks something similarly like this:
I know that it's easier to use the line number but I prefer using some labels.
Best Answer
The following example defines a new counter
llabel
for the listings' markings. Packagepifont
is used to get the symbols, therefore the markings are limited to the range of 1 upto 10. Otherwise, packagetikz
can be used to draw own symbols without this limitation.Macro
\llabel
takes a symbolic name that can be used later for\ref
. It needs to be executed right at the beginning of the line, because then its position is known. The label is put to the left with the distance\llabelsep
from the listings line number. To get\llabel
executed inside the listings themathescape
feature of packagelistings
is used.It is also possible to have source files that keep working by using special markers and using option
escapeinside={/@}{@/}
instead of
mathescape
:Putting
\llabel
at end of lineThe following variant allows to put
\llabel
anywhere on the source line, also the end of line, where it does not disrupt the formatting. For this purpose the line marker is also remembered by a label with the line number in its name. The marker is then put along the line number hooking intonumberstyle
:This should also work with an external file instead of the embedded listings.