# How to write math symbols in a verbatim?

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?

• You should consider the listings package which provides mathescape. This escapes to LaTeX within math, setting the symbols the way you want while still keeping the other content verbatim-like. Dec 12 '13 at 19:21
• @Werner ive installed it and listed the package. could you kindly give me quick sample syntax for using mathescape? Dec 12 '13 at 19:27

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}

• oh werner, this worked perfectly. thanks for the full answer, i have a deadline in 10 hours and ive been using latex only for an hour! thank you!! Dec 12 '13 at 20:02
• @garciaj then I think it's not worth it using Latex, unless it's (relatively) simple. Dec 12 '13 at 22:15
• @Manuel, Its definitely worth using Latex, it is THE best thing ive come across lately! and half my essay is filled with equations so couldnt have done it without it Dec 12 '13 at 23:18
• @garciaj Now it's the moment. But 10 hours before a deadline… it's not the optimum time. Dec 13 '13 at 12:40
• @jjmerelo: listings works in XeLaTeX. If not, you're doing something wrong. Dec 27 '16 at 21:07

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