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.

How can I have an automatically numbered verbatim environment, just like equations? I tried putting it like this:

\begin{equation}
\begin{verbatim}
bla... bla... bla...
\end{verbatim}
\end{equation}

but it gives me an error. I am sure there must be a better way.

share|improve this question
    
You should be more specific with your request: For example, do you want to use equation numbers? You want the entire verbatim environment to have a single number (not line numbers)? –  Werner Feb 21 at 16:38
    
@Werner : I want the entire verbatim environment to have a single number. It doesn't matter if the numbering is the same of the other equations but it would be appreciated. –  Ariel Feb 21 at 16:43
1  
Welcome to TeX.SX! –  Adam Liter Feb 21 at 17:13

3 Answers 3

up vote 6 down vote accepted

This is the example file from the numberedblock package. It makes the labels slightly different than equation numbering, so that one doesn't confuse them if both are used in the document. However, label placement and style can be changed. The documentation is briefly covered in the .sty file itself, as well as this example file.

The parameter defaults, which are resettable, include:

\setlength\maxblocklabelsize{-0.4in}

\setlength\blockindent{0.2in} and

\newcommand\blocklabel[1]{[\textit{\arabic{#1}}]}

Here is the example from the package download, which has been EDITED, using Werner's great answer at How can one refer to a part of an equation?, to provide a hyperref-compatible label/reference capability. In the case of \numblock, you place a \nblabel{} inside the \numblock argument. In the case of the numVblock environment, you pass it an optional argument [\nbVlabel{}]:

\documentclass[10pt]{article}
\usepackage{numberedblock}

\makeatletter
\AtBeginDocument{\let\nb@label\label}% http://tex.stackexchange.com/q/9939/5764
\newcounter{nb@counter}
\newcommand{\nblabel}[1]{\def\@currentlabel{\theblocknum}\nb@label{#1}}
\newcommand{\nbVlabel}[1]{\setcounter{nb@counter}{\theblocknum}\stepcounter{nb@counter}%
  \def\@currentlabel{\thenb@counter}\nb@label{#1}}
\makeatother


\begin{document}

\parindent 0.3in
%\setlength\maxblocklabelsize{-.4in}
\setlength\blockindent{0.0in}

This is a test of the \textsf{numberedblock} style package, which is
specially designed to produce sequentially numbered BLOCKS of code (note
the individual code lines are not numbered, but the whole block gets a
single number, for later reference (much in the same way that equations
can get numbered in a document).  While specialized for numbering code
blocks, the commands can actually number other items, as well, in fact
anything that fits in a \LaTeX{} box.

If the code block contains no special characters, one can simply use the
command form, called \verb,\numblock,.  It cannot handle verbatim text,
but must use standard \LaTeX{} escape sequences (for line breaks,
contiguous spaces, special characters, etc.).  It puts the output in a
tt font , which is the same used in the verbatim environment:

\numblock{This text is the\\argument to the command\\where double
slashes have been\\used for line breaks\nblabel{vb:i}}

Most useful, however, there is also the \verb,numVblock, environment,
which handles verbatim text, as seen in the next example: referencing code block~\ref{vb:i}

\begin{numVblock}[\nbVlabel{vb:ii}]
This is the numVblock 
environment, which         (<--see contiguous spaces here)
succeeds in
incorporating verbatim text like
@##$%*$%$()||}{?><\\\
\end{numVblock}

Referencing code block~\ref{vb:ii}.
As envisioned the \verb,numVblock, environment would be ideally suited
for displaying small code blocks as part of documentation.  The code can
contain contiguous spaces and special characters:

\begin{numVblock}[\nbVlabel{vb:iii}]
      program test
      implicit none
      integer a, x
c$$$$$$$$$$$$$$$$$$$$$$$$$
      a = 0
      x = 1
   10 a = a + x
      if (a .eq. 100) stop
      goto 10
      end
\end{numVblock}

Referencing code block~\ref{vb:iii}.
Below, I test the \verb,\numblock, command with the argument as a
box, rather than as formatted text.

\numblock{\fbox{Testing, 1,2,3 testing a box}\nblabel{vb:iv}}

Don't forget, there are settable parameters to define the block
left-indent, the format of the label, and (if needed) the labels' max
width/placement. CB~\ref{vb:iv}

\end{document}

enter image description here

share|improve this answer
    
How can I add a label to a numVblock so I can refer to it further in the text? –  Ariel Feb 22 at 9:09
1  
@Ariel That wasn't considered (though it should have been) when I put the package together. If I figure out a way, I'll edit accordingly. –  Steven B. Segletes Feb 22 at 12:50
1  
@Ariel Please see revision. It can now support labeling. –  Steven B. Segletes Feb 22 at 13:27

Here's a start you can modify to meet other needs.

\documentclass{article}

\usepackage{amsthm}

\newtheorem{VerbatimText}{}

\begin{document}

\begin{VerbatimText} \label{v:test}
\begin{verbatim}
This is my numbered verbatim environment
\end{verbatim}
\end{VerbatimText}

I can refer to it with a reference: \ref{v:test}.
\end{document}

enter image description here

share|improve this answer
    
Is there a way to put the number on the right, just like the equation environment? –  Ariel Feb 21 at 16:58
    
@StevenBSegletes elegant answer seems to provide what you need, so no need to tinker with this one. –  Ethan Bolker Feb 21 at 17:08

I have 3 suggestions for it. Have a look:

The first one...

\documentclass{article}
\usepackage{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}

enter image description here

The second one...

\documentclass{article}
\usepackage{amsmath}
\usepackage{fancyvrb}
\begin{document}
\DefineShortVerb{\|}
\SaveVerb{VerbA}|f(x)=ax^2+bx+c|
\SaveVerb{VerbB}|g(x)=dx^2+ex+f|
\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}

enter image description here

The third one..

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

enter image description here

share|improve this answer

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.