3

I have a latex code for a computer program code like as specified in here. I want to write a label to this code. I searched for it but was unable to find how to add label for the source code. Any help will be great!


Cite from linked source, contents of link as of 2013-03-20 20:53:13:

To write a code in latex, you can use \begin{verbatim} \end{verbatim} environment. A similar approach is by doing this

\usepackage{fancyvrb}
\DefineVerbatimEnvironment{code}{Verbatim}{fontsize=\small}
\DefineVerbatimEnvironment{example}{Verbatim}{fontsize=\small}

and then you can type your code in the body

\begin{code}
c+=1;
\end{code}
1
  • 1
    I think I didn't understand properly. But listings may a worth try.
    – user11232
    Commented Mar 20, 2013 at 23:03

1 Answer 1

6

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}
2
  • Since it is tagged with cross-refencing I think you are right.
    – Speravir
    Commented Mar 20, 2013 at 23:11
  • 2
    Hey, with your edit you stole my answer !!! ;-) The label could, of course, also set in the first place of fancyvrb, and so the functionality should be very similar to listings then.
    – Speravir
    Commented Mar 20, 2013 at 23:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .