[Tex/LaTex] Display math mode code in lstlisting

listingsmath-mode

I have the following listing with 3 titles:

\begin{figure}
\begin{lstlisting}[mathescape=true]
Enhancing Access Privacy of Range Retrievals over $(\mathbb{B}^+)$-Trees.
Enhancing Access Privacy of Range Retrievals over \$({\rm B}^+)\$ -Trees
Enhancing Access Privacy of Range Retrievals Over B+-trees
\end{lstlisting}
\label{title_example}
\caption {example title}
\end{figure}

I would like the first title to use math mode and the second to not use math mode. I tried escaping the dollar signs but that removes everything after the first.

Best Answer

Just choose a different escaping sequence/character:

enter image description here

\documentclass{article}

\usepackage{listings,amsfonts}

\begin{document}

\begin{figure}
\begin{lstlisting}[escapeinside=`']
Enhancing Access Privacy of Range Retrievals over `$(\mathbb{B}^+)$'-Trees.
Enhancing Access Privacy of Range Retrievals over $(\mathrm{B}^+)$ -Trees
Enhancing Access Privacy of Range Retrievals Over B+-trees
\end{lstlisting}
\label{title_example}
\caption{example title}
\end{figure}

\end{document}
Related Question