# What's the best way to reference examples?

I am writing a programming book on AWK in Latex and I have hundreds of code examples, for example,

awk '1; { print "" }'


I am curious how can I reference the example later in the book. At the current stage it's example #3, but if I add one before it, I'd like the numbering to update automatically, and I'd also like to make a clickable link in the final pdf, that would bring the user to the example.

Thanks!

-

You will probably want to use a package to help with pretty-printing the code snippets. Two popular choices for the job are the listings and minted packages.

Both packages provide a listing environment that can be cross-referenced similar to a table, figure or equation. The names of all listings used in your document can also be gathered together in a big list using \listoflistings, similar to a table of contents.

See the package documentation for more details.

-
And then if you want to make it fancy, tocloft can improve LoL just like it can improve ToC. –  ℝaphink Apr 29 '11 at 5:54
I found listings better than minted, since minted doesn't work with MikTex on Windows. –  Peteris Krumins Apr 29 '11 at 11:41
@Peteris: minted should work on Windows if you install Pygments. Some people report installing it successfully. Can't say I've tried it though. –  ℝaphink Apr 29 '11 at 12:28
@Petris You just need to install Python and Pygments. Just make sure that the environment variables PATH and PYTHONPATH are set correctly. Admittedly, not as easy of a job as on UNIX-like systems. –  Sharpie Apr 29 '11 at 19:21

In short you tag a reference with

\ref{type:Name}


and you reference that tag with

\label{type:Name}


For more info have a look at the appropriate page at the wikibook. There are also more specialized packages for referencing, but the concept is always the same.

As you have hundreds of examples, try to find a suitable, naming convention for your examples.

## Update

For inter-document linking check the hypperef package

-
Thanks for the comment! –  Peteris Krumins Apr 29 '11 at 12:24