Do you mean a TeX label, which you can reference elsewhere in the document with \ref
? One way is to use the listings
package instead of fancyvrb. Then, you can include your code in the document like this:
\begin{lstlisting}[caption={Cool code},label=cool]
c+=1;
\end{lstlisting}
As you would expect, the caption
option provides a caption under the code listing (à la "Listing 1: Cool code"), and the label option provides a label which can be referenced like \ref{cool}
. A caption must be provided for the label to work, at least in my installation.
A MWE:
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[caption={Cool code},label=cool]
c+=1;
\end{lstlisting}
\begin{lstlisting}[caption={Wow},label=wow]
c-=1;
\end{lstlisting}
As you can see in Listing~\ref{cool}, we can increment variables.
Having thought better of it, Listing~\ref{wow} allows us to retreat.
\end{document}
If, instead, you wish to reference a particular line number of a code listing, you can stick with the fancyvrb
package and do something like this example from the fancyvrb documentation:
\begin{Verbatim}[commandchars=\\\{\},numbers=left,numbersep=2pt]
First verbatim line.
Second line.\label{vrb:Important}
Third verbatim line.
\end{Verbatim}
As I previously showed in
line~\ref{vrb:Important}, it is...
You could make this simple to use by including the options in your \DeclareVerbatimEnvironment
command, like so:
\documentclass{article}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small,commandchars=\\\{\},numbers=left,numbersep=2pt}
\begin{document}
\begin{code}
c+=1;\label{cool}
c-=1;\label{wow}
\end{code}
As you can see on Line~\ref{cool}, we can increment variables.
Having thought better of it, Line~\ref{wow} allows us to retreat.
\end{document}
listings
may a worth try.