Search:   Help



LyX documentation


Shared groups



Categories: Example
<< | Page list | >>

How to include an external program listing into LyX.

Table of contents (hide)

  1.   1.  Include programlistings
  2.   2.  Use in figures
  3.   3.  Issues
  4.   4.  Contributors
  5.   5.  Categories

1.  Include programlistings

This automatically uses the source code, and any change in the external source code will be visible in the LyX output. The latest version is available at

Notice that you can skip the -S cpp parameter and then the highlight program will automatically select the appropriate language.

  • Install the program highlight that is available on
  • Run the program highlight on one of your sourcecode files with the command:
    highlight -L -t 4 -I -S cpp -r -q KOD.cpp
Change the file name (KOD.cpp) to your file, and change language if you use something else than C++ (cpp).
  • Things are written in a window, copy and paste everything from the row
(but do not include this last line!) to the LaTeX Preamble window in document settings. Click the Save as Document Defaults button.
  • In the Preferences window you should add a new FileFormat
nedit is the texteditor that I use to write program code. Change to what you want to use. Notice that the Format name must be something unique that is not used elsewhere.
  • In the Converters windows you should add
    From: C++
    To: LaTeX(pdflatex)
    Converter: highlight -A -L -t 4 -I -S cpp -f -r -q $$i -o $$o
Add the same thing for conversion from C++ to LATEX(plain). Dont forget to change to reflect the language you are using. IMPORTANT! If you get errors with undefined command sequences and see the command \dq, then try removing the -r switch! Also make sure that you don't use any "extra parameters".
  • Open the file external_templates that is in the LY X directory (either the global one, or copy the file to your own personal directory and edit that one). To the file you should add the following:
    Template CppCode         
    GuiName "[C++ $$Basename]"         
       Add C++ code
    InputFormat cpp         
    FileFilter "*.{cpp,h,hpp,c}"         
    AutomaticProduction true         
    Format LaTeX                 
    Product "{\\small \\input{$$FPath$$Basename.tex} }"
       UpdateFormat latex                 
       UpdateResult "$$FName.tex"         
    Format PDFLaTeX                 
       Product "{\\small \\input{$$FPath$$Basename.tex} }"
       UpdateFormat latex                 
       UpdateResult "$$FPath$$Basename.tex"         
  • Now you can use Insert , File, External Material and add your program code.

2.  Use in figures

You will likely want to use your code in figures as well. The process for this is fairly simple:

  • Insert a new figure with a caption
  • Optionally, insert a box
  • Insert->File->External Material and insert your code file
  • You will likely want to change the parameters for the highlight program to include:
    -J 67 -W
  • The -J parameter sets the number of characters per line. The -W parameter enables word wrapping.

3.  Issues

  • There is an extra blank line between the end of a code listing and the caption. It seems that highlight ends its output with a line break. If anyone has a way around this, please put it here.

4.  Contributors

  • ?

5.  Categories

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2008-07-30 14:20 UTC