2

i would set a label at listings like on figures. If I use the label command there is no label for reference

  • Hi, welcome. How do you insert the listing? – Torbjørn T. Sep 1 '17 at 9:49
  • with insert->programm listing – user142514 Sep 1 '17 at 9:51
  • OK. Captions/labels is actually a bit easier I think if you use Insert -> File -> Child Document, and use "Program listing" as include type. But give me some minutes and I'll explain one way of doing this with Insert -> Program listing as well. – Torbjørn T. Sep 1 '17 at 9:56
  • while using child document label could set but the references doesn't show the numbers like in figures – user142514 Sep 1 '17 at 10:08
  • 1
    I don't understand what you mean by that. Note that you must have a caption to get a number. – Torbjørn T. Sep 1 '17 at 10:09
4

There are two parts here, one for the Insert -> Program listing, one for using child documents, at the end.

Insert -> Program listing

After making your listing with Insert -> Program listing, right click in the inset and open the Settings:

opening settings of inset

Settings:

settings dialog

In the "Main settings" tab you can change various parameters, such as setting the language in the bottom left. But to add caption and label, go to the Advanced tab:

Advanced settings

Configuration of listings is done via key=value pairs, and you can write such in the empty white rectangle on the right part of this dialog. Perhaps unsurprisingly, there are caption and label keys, so you can write something like caption={bla bla, bla}, label=foo. This will then look like this, after you hit the Apply button:

added caption and label

Note that you must include a caption. It is the caption that is numbered, so without it you wont get a number, the label alone isn't enough.

Press OK or Close to close the dialog. You're not quite done yet though. LyX doesn't see this label in its cross-reference system, so if you try Insert --> Cross reference, you fill not find foo there.

To trick LyX into being able to use that label, place the cursor right before or after the listing (doesn't actually matter where), and do Insert --> Note --> LyX Note. Inside this note, insert a label that you call foo (use exactly the same name as you did for the listing):

LyX note with label

Now, if you do Insert --> Cross reference, the foo label appears in the list:

insert cross reference dialog

Child document

Another way of making a listing is to include a file with code directly, using Insert -> File -> Child document, and setting the "Include type" to "Program listing". Here you can set the caption and label directly, and no tricks are need to get LyX to see the label:

insert child document

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.