2

I'm trying to cross reference program code line numbers within a program listing using lyx. From what I can tell, both labels and ERT are disabled within the program listing, but I'm not sure why.

I know in regular latex, you can add labels within a program listing, but it appears disabled in lyx. See: ftp://ftp.tex.ac.uk/tex-archive/macros/latex/contrib/listings/listings.pdf

The internet seems strangely unhelpful on this issue, unless I'm missing something obvious. Any ideas?

2

It looks like you can't use LyX's system for managing labels and cross references within listings, but you can still reference lines, by writing the label manually, and using e.g. \ref{labelname} in an ERT.

You basically just do what is described in chapter 7 of the manual:

  1. Go to Document --> Settings --> Listings, add

    escapeinside={*@}{@*}
    

    to the listings parameters. Now, anything you write between *@ and @* inside a listing will be read and executed as LaTeX code, instead of being printed verbatim.

    You can use other delimiters as well, this was just an example.

  2. Inside the listing, write

    *@\label{mylinelabel}@*
    

    at the line you want to reference.

  3. To reference the line, add an ERT and write \ref{mylinelabel}. That will print just the line number. As mentioned, these labels wont show up in LyX' label list, so you have to this the manual way.

Here's an example of how it can appear in LyX:

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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