Text FormattingReferencing a Code

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
frodojedi
Posts: 20
Joined: Fri Feb 18, 2011 1:23 am

Referencing a Code

Post by frodojedi »

Hi all,
I would like to make a reference to a code listed inside the \begin{verbatim}...\begin{verbatim} environment.

How can I do it? Of course I cannot place the \label command inside the verbatim environment.

Any suggestion?

Thanks in advance
Last edited by frodojedi on Mon Feb 21, 2011 1:28 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Referencing a Code

Post by Stefan Kottwitz »

Hi,

for referencing you probably would like to give a number to that environment. Numbered environments can be produced by using theorem environments.

For code listings I would use the listings package which is able to produce numbered code listings with captions.

Stefan
LaTeX.org admin
Frits
Posts: 169
Joined: Wed Feb 02, 2011 6:02 pm

Referencing a Code

Post by Frits »

If you're refering to a particular line in your verbatim environment, here's how:

Code: Select all

\begin{Verbatim}[commandchars=\\\{\},numbers=left,numbersep=2pt]
First verbatim line.
Second line.\label{vrb:Important}
Third verbatim line.
\end{Verbatim}

As I previously shown line~\ref{vrb:Important}, it is...
Note that the environment is not verbatim but Verbatim. You'll need the fancyvrb package
howtoTeX.com - Your LaTeX resource site (Tips, Tricks, Templates and more!)
Follow howtoTeX on twitter
frodojedi
Posts: 20
Joined: Fri Feb 18, 2011 1:23 am

Referencing a Code

Post by frodojedi »

Hello,
thanks a lot. I have a problem with the caption when using also a frame.
Actually I am using a template: http://openwetware.org/wiki/LaTeX_templ ... PhD_thesis

When I put the following code I get that the frame is upon the caption.

Code: Select all

\begin{lstlisting}[caption={Example 1},label=lst:example1,frame=single, rulecolor=\color{red}]
\documentclass{article}


\begin{document}

....

\end{document}

\end{lstlisting}



I am using both \usepackage{listings} and \usepackage{fancyvrb}. When I compile in a new document without the template I don´t get that behavior.



Is there a way to overcome this problem?


Thanks in advance
frodojedi
Posts: 20
Joined: Fri Feb 18, 2011 1:23 am

Re: Referencing a Code

Post by frodojedi »

Hi I found the solution, I just added \bigskip in the caption after the text
Post Reply