Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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{equation}
\begin{Verbatim}
... hack hack hack ...
\end{Verbatim}
\end{equation}
share|improve this question
    
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
add comment

3 Answers 3

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!...!:

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{equation}
  \verb!f(x)=ax^2+bx+c! \label{eq:1}
\end{equation}
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.

enter image description here

\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.

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\begin{document}
\begin{equation}
  \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}
\end{equation}
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.

share|improve this answer
    
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
add comment

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{equation}
 \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{equation}
\end{document}
share|improve this answer
add comment

Try

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

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

\begin{texteq}
    \begin{verbatim}
    ...
    \end{verbatim}
\end{texteq}
share|improve this answer
1  
Welcome to TeX.sx! –  Kurt Nov 1 '12 at 10:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.