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?
lstlocal.cfginstead oflistings.cfgand placed the file in my working directory. Worked like a charm, thanks Alan Munn. – patricktokeeffe Jun 05 '14 at 02:18listingsto access the language, so a separate package is simply not needed. Since thelistingspackage has set up this mechanism, it makes sense to use it (and it's a bit less work). – Alan Munn Jun 05 '14 at 14:10