# Have verbatim code as an equation

I have a little snippet of program code / input that needs to be rendered with the exact layout as typed in (verbatim). Is it possible to have this as an equation? I tried to nest the Verbatim environment in an equation without success. Any help?

$$\begin{Verbatim} ... hack hack hack ... \end{Verbatim}$$

-
Why do you need it in an equation? Do you just want to have it numbered? How many lines of "code" do you have in the equation? – Werner Oct 29 '11 at 23:24
@Werner Yeah. I want to have it numbered, centered and formatted just like an equation. There are about seven lines of code. – FUZxxl Oct 29 '11 at 23:26
Are the seven lines of code in the same equation, or in separate equations? – Werner Oct 29 '11 at 23:38
@Werner they should all go in one equation. – FUZxxl Oct 29 '11 at 23:43

If you're only after typesetting a single equation that has some verbatim text in it, you could use the short form of verbatim, as in \verb!...!:

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
$$\verb!f(x)=ax^2+bx+c! \label{eq:1}$$
In order to typeset $f(x)=ax^2+bx+c$, see~\eqref{eq:1}.
\end{document}


amsmath provides \eqref{<lab>} that puts brackets ( ) around the equation reference. And, of course, these equation environments can be intermixed with the text as needed.

The above is a quick-and-easy way of typesetting verbatim in an equation. If you want multiple verbatim content in an aligned equation environment (like amsmath's align), you could use the fancyvrb package to capture your verbatim content and then use it in the align environment. Using verbatim directly in align does not work.

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{fancyvrb}% http://ctan.org/pkg/fancyvrb
\begin{document}
\DefineShortVerb{\|}
\SaveVerb{VerbA}|f(x)=ax^2+bx+c|% First equation verbatim
\SaveVerb{VerbB}|g(x)=dx^2+ex+f|% Second equation verbatim
\begin{align}
\UseVerb{VerbA} \label{eq:1} \\
\UseVerb{VerbB} \label{eq:2}
\end{align}
In order to typeset $f(x)=ax^2+bx+c$, see~\eqref{eq:1}. There is also~\eqref{eq:2} that shows you how to typeset $g(x)=dx^2+ex+f$.
\end{document}


Edit: Grouping these into a single "equation" is similar to the original equation environment, and merely using an array environment for "collection" of the content.

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
$$\left.\begin{array}{r@{}l} \verb!f(x)!&\verb!=ax^2+bx+c! \\ \verb!g(x)+2f(x)!&\verb!=y! \end{array}\right\} \label{eq:input}$$
One could use~\eqref{eq:input} as input.
\end{document}


Although not entirely necessary, the above example uses a zero-width column separation @{} to align the equal signs = of the \verb equation parts.

-
Yeah... that looks better, though I want to have all those lines as one equation. (See them as an ascii-art representation of a matrix that serves as an input to another program and thus mustn't be typeset differently) – FUZxxl Oct 29 '11 at 23:54
@FUZxxl: Using an array you could just combine the separate equations with a single equation number. If that's what you're after, see my answer edit. Or are you after something else? – Werner Oct 30 '11 at 0:05

a multi-lined equation with a single equation number can be obtained by using one of the "subsidiary" environments from amsmath. each line needs to have \verb applied individually:

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{aligned} &\verb|f(x)=ax^2=bx+c|\\ &\verb|g(x)=dx^2=ex+f|\\ &\verb|h(x)=kx^2=mx+n| \end{aligned}
\end{document}

-

Try

\newenvironment{texteq}{%
$$% \begin{minipage}{0.9\linewidth}% }{% \end{minipage}%$$%
\ignorespacesafterend%
}


You will still have to start a verbatim environment inside your texteq environment:

\begin{texteq}
\begin{verbatim}
...
\end{verbatim}
\end{texteq}

-
Welcome to TeX.sx! – Kurt Nov 1 '12 at 10:29