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:

bla... bla... bla...

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
Welcome to TeX.SX! –  Adam Liter Feb 21 at 17:13
add comment

3 Answers

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\blockindent{0.2in} and


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{}]:


\AtBeginDocument{\let\nb@label\label}% http://tex.stackexchange.com/q/9939/5764


\parindent 0.3in

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}

This is the numVblock 
environment, which         (<--see contiguous spaces here)
succeeds in
incorporating verbatim text like

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:

      program test
      implicit none
      integer a, x
      a = 0
      x = 1
   10 a = a + x
      if (a .eq. 100) stop
      goto 10

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}


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
@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
@Ariel Please see revision. It can now support labeling. –  Steven B. Segletes Feb 22 at 13:27
add comment

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





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

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

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
add comment

I have 3 suggestions for it. Have a look:

The first one...

  \verb!f(x)=ax^2+bx+c! \label{eq:1}
In order to typeset $f(x)=ax^2+bx+c$, see~\eqref{eq:1}.

enter image description here

The second one...

  \UseVerb{VerbA} \label{eq:1} \\
  \UseVerb{VerbB} \label{eq:2}
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$.

enter image description here

The third one..

\usepackage{amsmath}% http://ctan.org/pkg/amsmath
    \verb!f(x)!&\verb!=ax^2+bx+c! \\
  \end{array}\right\} \label{eq:input}
One could use~\eqref{eq:input} as input.

enter image description here

share|improve this answer
add comment

Your Answer


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.