[Tex/LaTex] Typesetting LaTeX Source Code with the listings package

highlightinglistingssyntax

I am writing some documentation for a LaTeX package, in which I am going to be including some example LaTeX code. What is the best way to typeset that code? I know I could just use verbatim, but it would be nice to get syntax highlighting. I realize that this is a duplicate of this question, in which the consensus was to use the listings package. The problem with listings is that it seems to only have a driver for TeX, not LaTeX. For example, when I typeset something like this:

\begin{lstlisting}[language=TeX,
                   keywordstyle=\bfseries,
                   identifierstyle=\itseries,
                   stringstyle=\ttfamily]
\begin{minipage}{\hsize}
  This is a test!
\end{minipage}
\end{lstlisting}

it results in something like this:

\begin{minipage}{\hsize}
  This is a test!
\end{center}

which is obviously messed up.

Is there a LaTeX-specific driver for listings? If not, is there another package that does syntax highlighting for LaTeX code?

Best Answer

You need to give the 'sub-language' for TeX:

language = [LaTeX]{TeX}

As you are writing a package, you might also want to use the moretexcs key to add 'new' LaTeX commands. For example, in my siunitx I have very long list:

  moretexcs    = 
    {
      addto,
      ampere,
      ang,
      angstrom,
      ...

to cover all of the new macros it adds, plus ones from other packages that are in the documentation and are not known to listings.