Go to page:

Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

IncludeExternalProgramListingUsingPygments

Categories: Example
<< | Page list | >>

How to include external program listings using pygments

Include programs listings

This automatically uses the source code, and any change in the external source code will be visible in the LyX output.

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

  • Install pygments that is available on http://pygments.org/(approve sites)
  • Run the program pygmentize with the command:
    pygmentize -f tex -S colorful > preamblePygments.tex
  • Open preamblePygments.tex and copy and paste everything from the row
    \makeatletter
    to
    \makeatother
(but do not include these two lines!) to the LaTeX Preamble window in document settings. Add these two lines at the beggining of the preamble:
\usepackage{fancyvrb}
\usepackage{color}
Click the Save as Document Defaults button.
  • In the Preferences window you should add a new FileFormat
    Format:cpp
    GUIname:C++
    Extension:cpp
    Editor:gedit
    
gedit 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: pygmentize -l cpp -o $$o  $$i
    
Add the same thing for conversion from C++ to LATEX(plain). Dont forget to change to reflect the language you are using. Also make sure that you don't use any "extra parameters".
  • Open the file external_templates that is in the LyX 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]"         
    HelpText                 
       Add C++ code
    HelpTextEnd         
    InputFormat cpp         
    FileFilter "*.{cpp,h,hpp,c}"         
    AutomaticProduction true         
    Format LaTeX                 
    Product "{\\small \\input{$$FPath$$Basename.tex} }"
       UpdateFormat latex                 
       UpdateResult "$$FName.tex"         
    FormatEnd         
    Format PDFLaTeX                 
       Product "{\\small \\input{$$FPath$$Basename.tex} }"
       UpdateFormat latex                 
       UpdateResult "$$FPath$$Basename.tex"         
    FormatEnd 
    TemplateEnd 
    
  • Now you can use Insert , File, External Material and add your program code.

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

Issues

  • I don't know how to break long lines in code.

Contributors

Categories

Category: Example

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2011-02-07 11:34 CET