# Escaping in \lstinline

There are several options for escaping LaTeX code in the lstlisting-environment. But this doesn't seem to work in \lstinline. I'd like to do something like this:

\lstinline[escapechar=§]{§\textlangle§foo§\textrangle§}


Is there any workaround?

Escaping via mathescape from listings works in \lstinline just the same as it does in the lstlisting environment. Here's a minimal example showing how to use it:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\section{foo} \label{foo}
\lstinline[mathescape]!abc$ijk$xyz$\ref{foo}$23!
\end{document}


In the above example, ijk is set in math mode, since mathescape is true, while \ref{foo} is also, but prints as a reference to the first section. Note the delimiter choice ! rather than {...}, although the MWE also works with this.

• Many thanks, Werner, for the tip! But, to be in math mode when escaping is not always desirable: \documentclass{article} \usepackage{listings} \lstset{mathescape} \begin{document} \lstinline+$Hello world!$+ \end{document} Unfortunately, I don't see another solution! Sep 5, 2012 at 10:11
• if I remove 'abc' inside the \lstinline, the above code does not work. This means the escape char $cannot be the first character in the listing. It is so weird. I do need to have a math symbol at the beginning of the formula and thus the escape char$ must be the first char. Mar 12, 2014 at 17:09
• @AnCongTran: I have no problem when I remove abc since the delimiters !...! still remain intact. Yes, using \lstinline[mathescape]$...$ will be weird, but it still works (using my MWE).
– Werner
Mar 12, 2014 at 17:12
• When using the \text{ijk} macro from amsmath package, you can also include normal text into math mode. So this answer can be used in a more general way, like \lstinline[mathescape]!abc$\text{ijk}$xyz!.
– John
May 1, 2018 at 15:02

From reading the code of the listings package, it is apparently deliberate but undocumented behaviour that escapes cannot be used in lstinline, with the exception of mathescape. Presumably escapes are considered an 'unsafe feature' in TextStyle contexts (of which lstinline is one), but it is not clear why or when it might be unsafe.

It is possible to re-enable the feature, as done below, but beware in case it causes any problems:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings

% http://tex.stackexchange.com/q/43526
% fix the apparently deliberate but undocumented behaviour of disabling escapes other than mathescape in TextStyle (used by \lstinline)
% there may be a good reason why this is disabled by default, so beware in case it causes any problems
\usepackage{etoolbox}
\makeatletter
\patchcmd{\lsthk@TextStyle}{\let\lst@DefEsc\@empty}{}{}{\errmessage{failed to patch}}
\makeatother

% for the example:
\usepackage{textcomp}% for \textlangle, \textrangle

\begin{document}
Experimental brace syntax: \lstinline[escapechar=§]{§\textlangle§foo§\textrangle§}

Standard delimiter syntax: \lstinline[escapechar=§]!§\textlangle§foo§\textrangle§!

\end{document}


• Here's one example of problem: when inside a {tabular}, the width of the escaped text is not taken into account which causes overlaps with the text which follows. For example, in \begin{tabular}{l}\lstinline[escapechar=§]{foo§bar§} text\end{tabular}, the words bar and text overlap. Oct 30, 2014 at 16:50
• @PhilippeGoutet: Thanks, good spot. Feel free to add your example to the answer. I wonder if it's worth trying to find a more satisfactory workaround for issues like that. Oct 30, 2014 at 22:38
• I really like this approach. However, I ran into trouble when using it within a macro. I posted my problem here: tex.stackexchange.com/questions/543286/… It would be nice if you could check it out. May 9, 2020 at 23:13