I'm using LyX software and need to embed python code snippets and the option I found is to put snippet in a file and then use: insert> file> Child Document ----> choose the snippet file ----> set include type to "Program Listing", set label to e.g. py:xx and caption to e.g. "My Python code" and define parameters such as backgroundcolor etc.

Firstly, how can I change the title of the object from Listing 1: to e.g. Snippet 1?

Secondly, how can I set parameters globally for all Program listing widgets so that I won't have to redefine them every time?

Thirdly, please let me know if you know of a better way to embed Python code (I'm using PyDev on Eclipse IDE for python programming).

  1. From karlkoeller's comment: Document --> Settings --> LaTeX preamble, add


    The \AtBeginDocument is necessary because LyX 2.1 adds \usepackage{listings} after the stuff that is added to Document --> Settings --> LaTeX preamble, so \lstlistingname is not defined at that time. You could alternatively do


    though this means listings is loaded twice.

  2. Caption and label has to be set for each listing of course, but the general style can be defined in Document --> Settings --> Listings:

    enter image description here

  • Is there a change regarding \lstlistingname? It doesn't seem to work for me. Using LyX Version 2.1.1 (Wednesday, July 2, 2014). – Nikos Alexandris Oct 11 '14 at 8:11
  • 2
    @NikosAlexandris The problem that LyX adds \usepackage{listings} after the user specified preamble stuff, so \lstlistingname isn't defined at the time. Use \AtBeginDocument{\renewcommand{\lstlistingname}{Snippet}} instead. – Torbjørn T. Oct 11 '14 at 8:49
  • Perfect, issue resolved. Maybe the answer deserves an edit/update? – Nikos Alexandris Oct 11 '14 at 9:02
  • @NikosAlexandris I intended to do so, but I didn't have time earlier. It's updated now. – Torbjørn T. Oct 11 '14 at 12:44
  • I was about to do it (for you) but, sometimes, I feel like not touching other people's work. Maybe I was wrong hesitating. Thanks. – Nikos Alexandris Oct 12 '14 at 18:31

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.