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.



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.

| improve this answer | |
  • 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. – Peter 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


and you reference that tag with


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.


For inter-document linking check the hypperef package

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.