[Tex/LaTex] lstlisting: mathescape and Erlang

listingsmath-mode

I am trying to combine to use mathescape together with the Erlang syntax highlighting in the package lstlisting, as described in Section 4.14 of its manual:

\documentclass{article}

\usepackage{listings}
\lstset{
    language=Erlang,
    mathescape=true
}

\begin{document}
\lstinline{$n_1$.lookup(k)}
\end{document}

This fails with:

! Extra }, or forgotten \endgroup.
<recently read> \egroup 

l.10 \lstinline{$n_1$.lookup(k)}

The code works fine for other languages, e.g. C. Does the style for Erlang miss something to allow this?

Best Answer

The listings package allows the syntax

\lstinline{...}

but marks it as “experimental”, see p. 26 of the manual, section 4.2. Section 5.1 talks about using \lstinline in the argument of another command; the end of section 5.1 tells, however, that this is not safe.

In particular, mathescape turns out not be compatible with the experimental syntax, so you should use something like

\lstinline|$n_1$.lookup(k)|

(use, as a delimiter, a character that doesn't appear in the string to be typeset, just like for the \verb command).