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.

I want to be able to have inline code (printed in typewriter or something) where I can also use LaTeX commands like \myinlinecode{here comes a backslash: <\textbackslash>}. So it should be possible to use non-verbatim parts inside the code command.

I read that it should be possible with the listings package but I could't get it to work. What I tried was:

\documentclass{article}

\usepackage{listings}

\begin{document}

\lstset{escapebegin=x, escapeend=y}
\lstinline|x\textbraceleft y|

\lstset{escapebegin={x}, escapeend={y}}
\lstinline_x\textbraceleft y_

\lstset{escapebegin={\textbackslash}, escapeend={\textbackslash}}
\lstinline_\\textbraceleft \_

\end{document}
share|improve this question

migrated from stackoverflow.com May 2 '13 at 17:56

This question came from our site for professional and enthusiast programmers.

    
AFAIK listings' possibilities for escaping to LaTeX are only available for the environment and not for the inline command. Do you need verbatim? Otherwise there's of course \texttt{}... –  cgnieder May 2 '13 at 18:05
    
Judging from the listings manual, the answer in the linked question seems to be incorrect. The correct key should be escapechar, or escapeinside. –  T. Verron May 2 '13 at 18:08
    
@T. Verron both work fine for me with \lstlisting but I couldn't get them to work with \lstinline. I also couldn't get escapebegin to work like you said not even with \lstlisting so I upvoted your comment in the linked question. –  user764754 May 2 '13 at 19:50
1  
See also Escaping in \lstinline –  cyberSingularity Oct 11 '13 at 13:09
add comment

1 Answer 1

up vote 4 down vote accepted

The following syntax makes it easier to use "escaped" (or LaTeX) content within \lstinline, and might be what you're after. There seems to be no particular need to use specially-assigned characters (like x and y) and the usual mathescape option works (escaping between $...$):

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{mathescape,basicstyle=\ttfamily}% Allow escaping to LaTeX inside $..$
\begin{document}

\lstinline|x$\lbrace$y|

\lstinline_x$\mbox{\textbraceleft}$y_

\lstinline!x{$\partial$}y!
\end{document}
share|improve this answer
    
This is the real deal, thanks a lot. –  user764754 May 2 '13 at 19:33
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.