Pages

05 August, 2015

Listing code in LaTeX/LyX

In the past I used external tools to provide listings for my papers. However, with 90% of my listings being SQL today, I am regularly editing and tweaking my listings for readability, which is easier when having them directly inside my LyX document. I had the fear that this would impair code highlighting. It doesn't, since the default listings package provides a ton of options [1].

Current working mode and options

  1. Use default listings package
  2. Want floated listings (in LyX)? -> (Right-Click >Settings > Placement > Float [x])
  3. Float needs caption (in LyX)? -> (Menu > Insert > Caption)
  4. Want to add/change colors? -> use colors + listing options
    \definecolor{byzantium}{rgb}{0.44, 0.16, 0.39}
    commentstyle={\color{byzantium}\textit}
  5. Want more colors? -> go to http://latexcolor.com/
  6. Want to add keywords? -> use listing options 
    language=SQL
    morekeywords={WITH} 
  7. Want to emphasize words? -> use listing options
    emph={Q,Q_c,Q_r,Q_b,Q_d,Q_}
    emphstyle={\color{blue}\textbf}
LyX provides the (Document > Settings > LaTeX preamble) to add colors.
LyX also to define default listing properties in (Document  > Settings > Listings).

Here are my current defaults.
basewidth={0.5em}
basicstyle={\ttfamily\small}
breaklines=true
columns=flexible
commentstyle={\color{byzantium}\textit}
emph={Q,Q_c,Q_r,Q_b,Q_d,Q_}
emphstyle={\color{blue}\textbf}
keepspaces=true
keywordstyle={\color{darkmidnightblue}}
language=SQL
morekeywords={WITH}
tabsize=4

Here is some example output.


Links


[1] Carsten Heinz, Brooks Moses, Jobst Ho mann. The Listings Package.
      http://mirror.unl.edu/ctan/macros/latex/contrib/listings/listings.pdf

No comments: