Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 7 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

\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.

share|improve this answer
1  
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 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 at 17:12
add comment

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}

Illustration of output of code

share|improve this answer
add comment

Your Answer

 
discard

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.