How can I have numbered verbatim environments?
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}% https://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}
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}