[Tex/LaTex] Use dollar sign as well as the math mode in lstlisting

listingssymbols

Is it ever possible? I want display the dollar sign correctly in the beginning in the bracket, then I want to display a right arrow and display another bracket.

\begin{lstlisting}
//basic instruction
["\$p.red"] $\rightarrow$ ["!jQuery", ".red"]

\end{lstlisting}

Best Answer

If you're willing to play around a bit, yes:

enter image description here

\documentclass{article}

\usepackage{listings}
\lstset{
  mathescape = true,
  basicstyle = \ttfamily}
\newcommand{\dollar}{\mbox{\textdollar}}

\begin{document}

\begin{lstlisting}
//basic instruction
["$\dollar$p.red"] $\rightarrow$ ["!jQuery", ".red"]
\end{lstlisting}

\end{document}

We escape inside math mode and insert \dollar, which is a text version of $.