How to write math symbols in a verbatim?
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:
\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}
In XeLaTeX, which admits typing the Unicode symbols directly, the only thing you need is to select a mono font that can display them.
\usepackage{unicode-math}
\setmonofont{DejaVu Sans Mono}
And then
\begin{verbatim}
sin(π/3)² + cos(π/3)²
\end{verbatim}
will be rendered as