Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

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.

share|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. –  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
add comment

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

share|improve this answer
    
Thanks for the comment! –  Peteris Krumins Apr 29 '11 at 12:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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