[Tex/LaTex] How to write math symbols in a verbatim

math-modeverbatim

Basically I have a section that is describing my algorithm in a pseudo code kind of way, and so I want that text to look like the verbatim text except I still want to be able to use math symbols. Is there any way to do this? Or any other hacky way to get the result?

Best Answer

listings provides a mathescape option to escape to LaTeX within math mode (between $...$). There are other escapable options as well, details of which are contained within the listings documentation (section 4.14 Escaping to LaTeX, p 39).

Here is a quick example:

enter image description here

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\lstset{
  basicstyle=\ttfamily,
  mathescape
}
\begin{document}
Here is some text.
\begin{lstlisting}
Some verbatim text and $f(x)=ax^2+bx+c$.
\end{lstlisting}
Here is some more text.
\begin{verbatim}
Some verbatim text and $f(x)=ax^2+bx+c$.
\end{verbatim}
Some final text.
\end{document}