3

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?

3

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.

| improve this answer | |
  • I'm purposefully avoiding editing the MikTex distro files so I used the file name lstlocal.cfg instead of listings.cfg and placed the file in my working directory. Worked like a charm, thanks Alan Munn. – patricktokeeffe Jun 5 '14 at 2:18
  • @pokeeffe That's actually a better route; I'll update the answer to show that. – Alan Munn Jun 5 '14 at 2:21
  • @AlanMunn I'm not familiar with your recommended approach. Does it have benefits over writing a custom package with both language and colour definitions? – jub0bs Jun 5 '14 at 10:07
  • @Jubobs The advantage mainly lies in the fact that you are using a uniform interface via listings to access the language, so a separate package is simply not needed. Since the listings package has set up this mechanism, it makes sense to use it (and it's a bit less work). – Alan Munn Jun 5 '14 at 14:10

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.