[Tex/LaTex] LaTeX – Using commands inside inline code

inline()listings

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}

Best Answer

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}
Related Question