Using custom text when using \ref in latex

I couldn’t find how to do this easily, but perhaps this is because I used rubbish search terms.

I eventually found my answer on http://en.wikibooks.org/wiki/LaTeX/Labels_and_Cross-referencing (which ended up telling me lots of useful things about the hyperref package I didn’t know)

First source the hyperref package in the preamble

\usepackage{hyperref}

You’ll probably want to provide some options to make it look nicer. See the manual linked from the ctan page: http://www.ctan.org/pkg/hyperref

You can then add references choosing the text yourself with a command of the format

\hyperref[label-name]{link-text}

It helps to illustrate this with an example. In my case I have a figure 4, composed of 3 sub-figures 4a, 4b and 4c (though these are simply part of the same image, not specified as separate figures in latex). My figure is labelled ‘SEM’ and I want to reference figure 4c including a hyperlink to the figure it appears in. I can do this using:

\hyperref[fig:SEM]{\ref*{fig:SEM}c}

This sends the link to the SEM figure, and puts as the hyperlinked text ‘4c’. Using \ref in the curly brackets ensures the figure number is updated if it changes from 4, which is the usual behaviour we desire.

Another thing I came across on the wikibooks page was the \autoref command provided by hyperref. This looks like a better idea than using \ref and constantly typing figure, and could straightforwardly be included in the above example by changing \ref to \autoref

Advertisements
1 comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s