# 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?

-

## 2 Answers

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! –  Denis Bitouzé Sep 5 '12 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. –  An Cong Tran Mar 12 '14 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 '14 at 17:12

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. –  Philippe Goutet Oct 30 '14 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. –  cyberSingularity Oct 30 '14 at 22:38