[Tex/LaTex] How to define colors in style file (lstlang0.sty)

colorlistings

Per the listings package documentation, I have a file lstlang0.sty in the same directory as my .tex file (well, .lyx file) which defines a new language. I used to have this language definition in my preamble but since it's being used in at least four documents now, I am very motivated to maintain it as a separate style file.

I've defined colors to provide syntax highlighting and that's where things get hung up.

This block was previously in my preamble and now resides in my lstlang0.sty file. Note it used lstdefinelanguage in the preamble (no '@'). Also, I've snipped out most of the keywords for convenience.

\lst@definelanguage[CR]{Basic}{%
  basicstyle={\ttfamily\footnotesize},%
  sensitive=false,%
  upquote=true,%
  string=[bd][\color{crbString}]{"},%
  showstringspaces=false,%
  morekeywords=[1]{Const, Public, <snip> ...},%
  keywordstyle={[1]{\color{crbKeyword}}},%
  otherkeywords={=, /, *, +, -, *=, /=, +=, -=, <<, >>, ^, <, >},%
  morekeywords=[2]{=, /, *, +, -, *=, /=, +=, -=, <<, >>, ^, <, >},%
  keywordstyle={[2]{\color{crbKeyword}}},%
  morecomment={[l][\color{crbComment}\ttfamily\itshape]{'}}%
}
\endinput

Things compile correctly so long as the colors (crbKeyword, crbString, crbComment) are defined in the preamble. Attempting to define them in the style file like so:

\definecolor{crbString}{RGB}{255,0,255}
\definecolor{crbKeyword}{RGB}{0,0,255}
\definecolor{crbComment}{RGB}{0,128,128}
\lst@definelanguage[CR]{Basic}{%
  ...

gives me a long list of errors:

LaTeX Error: Undefined color 'crbKeyword'.
LaTeX Error: Undefined color 'crbKeyword'.
LaTeX Error: Undefined color 'crbComment'.
...

I tried adding \RequirePackage{color} above the color definitions in the style file and LaTeX first complains "Can only be used in preamble" followed by the same undefined color errors. Encasing the color definitions with \AtBeginDocument{..} produced the same results.

What do I need to change to get the color definitions into lstlang0.sty?


I did notice no other listings language definitions include syntax coloring. Is there any (technical) reason?

Best Answer

The language definition files, despite using the file extension .sty aren't regular packages, so you can't put anything inside them other than \lst@definelanguage commands. Instead, for other listings related LaTeX code, like your colour definitions, you should use the listings.cfg file.

So put the colour definitions there, and everything will work as you expect.

If you only want these definitions to work for a particular language, (in your case Basic), you can put them into a file named listings-<language>.prf and then in your lstlocal.cfg file you add \input{listings-<language>.prf}. Both files can be placed in a listings directory within your local texmf directory.

So to be more explicit for your particular example, you can create the following files:

listings-basic.prf

\definecolor{crbString}{RGB}{255,0,255}
\definecolor{crbKeyword}{RGB}{0,0,255}
\definecolor{crbComment}{RGB}{0,128,128}
\endinput

lstlocal.cfg

\input{listings-basic.prf}

Or simply put the definitions directly into lstlocal.cfg and they will be available for any language definitions that use them.

Related Question