Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to define moretexcs lists outside of a lstdefinestyle macro because I want to load these definitions from a separate tex file. However inside lstset moretexcs is ignored:

\documentclass{article}

\usepackage{xcolor}    
\usepackage{listings}

\lstdefinestyle{lstStyleLaTeX}{%
   ,language = [LaTeX]TeX%
   % ,moretexcs={abc},
   ,keywordstyle=\color{red}
}
\lstset{%
   language=[LaTeX]TeX,
   moretexcs={abc},
}%

\begin{document}
\begin{lstlisting}[style=lstStyleLaTeX]
\abc{content}
\end{lstlisting}

\end{document}
share|improve this question
    
According to the listings documentation (section 4.18 Language definitions, p 42), "If used ‘standalone’ outside a language definition, the key might work only after selecting a nonempty language (and switching back to the empty language if necessary)." So, perhaps you need language=[LaTeX]TeX as part of \lstset, and lanuage={} afterwards... –  Werner Nov 25 '12 at 18:38
    
With your changes it does not give an error, but it simply has no effect. –  Matthias Pospiech Nov 26 '12 at 21:55
    
Why not say \lstset{style=lstStyleLaTeX, moretexcs={abc}} and omit the [style=lstStyleLaTeX] argument later? –  cgnieder Dec 15 '12 at 13:51
    
I changes styles in my document quite often because I use not only LaTeX as a language. –  Matthias Pospiech Dec 16 '12 at 14:19

1 Answer 1

up vote 5 down vote accepted

The following example defines macro \addmoretexcs. The optional argument specifies the dialect (default is common). The language is loaded if it is not yet available. Then the language definition, internally stored in \lstlang@<language>$<dialect>, is extended by setting the additional moretexcs list:

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings}

\lstdefinestyle{lstStyleLaTeX}{%
   ,language = [LaTeX]TeX%
    % ,moretexcs={abc},
   ,keywordstyle=\color{red}
}

\makeatletter
\newcommand*{\addmoretexcs}[2][common]{%
  \lowercase{\@ifundefined{lstlang@tex$#1}}{%
    \lstloadlanguages{[#1]TeX}%
  }{}%
  \lowercase{\expandafter\g@addto@macro\csname lstlang@tex$#1\endcsname}{%
    \lstset{moretexcs={#2}}%
  }%
}
\makeatother

\addmoretexcs[LaTeX]{abc}

\begin{document}

\begin{lstlisting}[style=lstStyleLaTeX]
\abc{content}
\end{lstlisting}

\end{document}

Result

share|improve this answer
    
Thanks very much. Now I can move all commands to a separate file that can be shared between different documents. –  Matthias Pospiech Jun 16 '13 at 15:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.