TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


Is there any workaround?

share|improve this question
up vote 8 down vote accepted

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:

Escaping in \lstinline

\usepackage{listings}% http://ctan.org/pkg/listings
\section{foo} \label{foo}

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.

share|improve this answer
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:

\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
\patchcmd{\lsthk@TextStyle}{\let\lst@DefEsc\@empty}{}{}{\errmessage{failed to patch}}

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

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

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


Illustration of output of code

share|improve this answer
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

Your Answer


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.