Reader small image

You're reading from  LaTeX Beginner's Guide - Second Edition

Product typeBook
Published inOct 2021
PublisherPackt
ISBN-139781801078658
Edition2nd Edition
Tools
Right arrow
Author (1)
Stefan Kottwitz
Stefan Kottwitz
author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz

Right arrow

Chapter 7: Using Cross-References

Our documents contain many numbered things, such as pages, sections, list items, figures, and tables. There's even more that we have not listed yet; for instance, if you would like to write mathematical text, you may use number equations, theorems, definitions, and many more.

We number things not just to count them but also to refer to them in other places of our document. For instance, in this chapter, if I wanted to point you towards the third figure, I would say "see Figure 7.3". LaTeX automatically enumerates figures for you. If you insert another figure, LaTeX will automatically adjust the numbering of all figures after it. But what happens with the references? Well, LaTeX can take care of all of our cross-references, which is the subject of this chapter.

In this chapter, we shall learn about the following:

  • Setting labels and references
  • Using advanced referencing
  • Referring to labels in other documents
  • Turning...

Technical requirements

You can use a local LaTeX installation, or you can compile the example code online at https://latexguide.org/chapter-07.

The code is also available on GitHub: https://github.com/PacktPublishing/LaTeX-Beginner-s-Guide-2nd-Edition-/tree/main/Chapter_07_-_Using_Cross-References.

In this chapter, we will use the cleveref package and the varioref package.

We will also get to know the fancyref, hyperref, and xr packages.

Setting labels and references

To be able to refer to a certain point, we have to mark it with a label. The name of that label will serve us afterward for referencing.

We will now typeset a list of the most used packages for papers, according to a survey on https://latex.org. Through the \label command, we will mark items that we can later refer to with the \ref command, as shown here:

  1. Create a new book document:
    \documentclass{book}
    \begin{document}
  2. Start a chapter and a section, and place a label for this section:
    \chapter{Statistics}
    \section{Most used packages by LaTeX.org users}
    \label{sec:packages}
  3. Continue with some text, including a footnote:
    The Top Five packages, used by LaTeX.org
    members\footnote{according to the 2021 survey on
    LaTeX.org\label{fn:project}}:
  4. Write an enumerated list and put a label on a few items for referring to them:
    \begin{enumerate}
      \item graphicx\label{item:graphicx}
      \item babel
      \item amsmath\label...

Using advanced referencing

LaTeX helps with automating all kinds of references. It's not only limited to numbering. LaTeX can even automate naming and phrasing. We will dig deeper into that here.

Producing intelligent page references

The varioref package offers a command to add on the preceding page, on the following page, or the page number to a reference, depending on the context.

We will use the varioref commands to introduce variable references, \vref, and \vpageref, to achieve enhanced reference texts:

  1. Open our current example from the Setting labels and references section. Add the varioref package to your preamble. Use the nospace package option, which ensures that varioref doesn't insert additional undesirable space before or after a reference:
    \usepackage[nospace]{varioref}
  2. Edit the content of the second chapter in our example code:
    \emph{amsmath}, on position \vref{item:amsmath}
    of the top list in section~\vref{sec:packages},
    is indispensable to...

Referring to labels in other documents

If you write several related documents that refer to each other, you might want to use references to labels of another document. The package with the name xr (standing for external references) implements this. First, load the xr package:

\usepackage{xr}

If you need to refer to sections or environments in an external document, say, doc.tex, insert this command into your preamble:

\externaldocument{doc}

This enables you to additionally refer to anything that has been given a label in doc.tex. You may do this for several documents. If you need to avoid conflicts when an external document uses the same \label as the main document, declare a prefix using the optional argument of \externaldocument, which you can use to add a prefix. For example, we can use D- as a prefix:

\externaldocument[D-]{doc}

This way, all references from doc.tex would be prefixed by D-, and you could write \ref{D-name} to refer to name in doc.tex. Instead of...

Summary

In this chapter, we learned how to reference chapters, sections, footnotes, and environments by their number or by the number of the corresponding page.

Using labels for referencing, we did not need to specify a number by ourselves; LaTeX determines the correct number of a page for us, or of a section, a footnote, or an environment.

We even got to know some clever ways of context-dependent referencing.

In the next chapter, we shall deal with lists, which consist mainly of references: tables of contents, lists of figures and tables, and bibliographies.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
LaTeX Beginner's Guide - Second Edition
Published in: Oct 2021Publisher: PacktISBN-13: 9781801078658
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime

Author (1)

author image
Stefan Kottwitz

Stefan Kottwitz studied mathematics in Jena and Hamburg. He works as a network and IT security engineer both for Lufthansa Industry Solutions and for Eurowings Aviation. For many years, he has been providing LaTeX support on online forums. He maintains the web forums LaTeX and goLaTeX and the Q&A sites TeXwelt and TeXnique. He runs the TeX graphics gallery sites TeXample, TikZ, and PGFplots, the TeXlive online compiler, the TeXdoc service, and the CTAN software mirror. He is a moderator of the TeX Stack Exchange site and matheplanet. He publishes ideas and news from the TeX world on his blogs LaTeX and TeX. Before this book, he authored the first edition of LaTeX Beginner's Guide in 2011, and LaTeX Cookbook in 2015, both published by Packt.
Read more about Stefan Kottwitz