Vladar's Blog

LaTeX links

Contents

One of the advantages of working with LaTeX is the possibility of referencing different parts of your document without worrying if they change their page in the future.

A label id is declared by invoking a simple command \label{key}. Now you can reference it by calling \ref{key} and get its current page with \pageref{key}.

Label declaration:

\chapter{Chapter}

\section{Section}

\label{my_label}

Label referencing:

See section \ref{my_label} on page \pageref{my_label}.

Which will produce something like See section 2.1 on page 9.

nameref

The nameref package1 provides a single command \nameref that prints out the name of the object (e.g. the section's title) being referenced.

cleveref

The cleveref package2 provides some additional commands, including \cref and \cpageref which automatically detect the type of link being referenced.

The code below will produce the same output without manually writing "section" or "page" in the text.

See \cref{my_label} on \cpageref{my_label}.

varioref

The \vref command of the varioref package3 simplifies the process even more, allowing you to write:

See section \vref{my_label}.

Moreover, the command is aware of the proximity of the link being referenced, changing the output accordingly:

fancyref

Additional customization of the reference output is possible with the fancyref package4, but you will need to prefix your links properly with \label{sec:my_label} (for the link to the section, for example).

See \fref{sec:my_label}.

\Fref{sec:my_label} --- capitalized reference.

Here's the list of pre-defined prefixes:

You can define your own prefixes (or re-define the existing ones) by following the instructions given in the package's documentation, e.g.:

\newcommand*{\fancyrefmaplabelprefix}{map}

\fancyrefaddcaptions{english}{
	\providecommand*{\frefmapname}{map}
	\providecommand*{\Frefmapname}{Map}
}

\frefformat{plain}{\fancyrefmaplabelprefix}{\frefmapname\fancyrefdefaultspacing#1}
\Frefformat{plain}{\fancyrefmaplabelprefix}{\Frefmapname\fancyrefdefaultspacing#1}

\frefformat{vario}{\fancyrefmaplabelprefix}{\frefmapname\fancyrefdefaultspacing#1#3}
\Frefformat{vario}{\fancyrefmaplabelprefix}{\Frefmapname\fancyrefdefaultspacing#1#3}

And now your \fref{map:my_map} and \Fref{map:my_map} references to the \label{map:my_map} will print out map 2.1 on page 10 or Map 2.1 on page 10 accordingly.

And, finally, let's make all of these links clickable. We are making a PDF after all!

You can easily achieve this by loading the hyperref package5, which will hyperlink all of your references. This includes not only the ones made with nameref, cleveref, varioref, or fancyref packages, but even your table of contents and lists of tables and figures.

The package provides some additional commands to create custom-named references to links, such as \hyperref[label]{text} that works similarly to \ref{label}. Linking to an arbitrary text is also possible with a pair of \hypertarget{name}{text} and \hyperlink{name}{text} commands, the latter pointing to the former. Note that the text argument in the \hypertarget command can be empty.

And, of course, you can create URLs:

\url{https://vladar.bearblog.dev/}

\href{https://vladar.bearblog.dev/}{blog}

The default styling of the links (red rectangles) is not very aesthetically pleasing but can be changed by setting some options in the \hypersetup command.

\hypersetup{
	pdfborderstyle={/S/U/W 1},% underlined links
	linkbordercolor={0 0.5 0},% dark green links
	urlbordercolor={0 0.5 0.5}% teal URL links
}

As you can see, the colors are declared as RGB values in the 0 to 1 range. The border style definition is more cryptic and requires studying the PDF specification's6 section 12.5.4 "Border Styles". In brief, the following keys are supported by most PDF viewers:

Discuss this post on Reddit

inktober2023-shallow.png

  1. https://www.ctan.org/pkg/nameref

  2. https://www.ctan.org/pkg/cleveref

  3. https://www.ctan.org/pkg/varioref

  4. https://www.ctan.org/pkg/fancyref

  5. https://www.ctan.org/pkg/hyperref

  6. https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf

#gamedev #guide #latex #software #ttrpg