On Auto-Updating References in LaTeX Documents
Tags: latex, academia
Published: 2023-01-21 11:38:13

---
title: On Auto-Updating References in LaTeX Documents
published: true
date: 2013-10-28 00:00:00 UTC
tags:
  - latex
  - academia
canonical_url: https://carstenrn.com/2013/10/latex-packages/
---

I am using [LaTeX](http://www.latex-project.org/) for almost all of my documents since my third semester, but I just recently (1-2 months ago) stumbled upon two packages I definitely would not like to miss anymore (besides the usual ones for writing math texts, of course): [cleveref](http://www.ctan.org/pkg/cleveref) and [xparse](http://www.ctan.org/pkg/xparse).

## Auto-updating your references

When writing up mathematical statements I am sometimes unsure if it should be a theorem, proposition, lemma or claim. A proposition would certainly be renamed to a claim if I do not plan to prove it; or a lemma if it turns out later that I do need it part way to prove a theorem. It is a painful and laborious task to go through the document and change all uses of `proposition`, `Proposition \ref` or `Prop.~\ref` to the respective `claim` or `Claim \ref`. Package [cleveref](http://www.ctan.org/pkg/cleveref) solves this problem elegantly:

```latex
Thm.~\ref{s:A} is based on Lem.~\ref{s:B} and Prop.~\ref{s:C},
and specializes Eqn.~(\ref{eqn:D}).
```

becomes

```latex
% Part of the preamble
\usepackage[capitalise]{cleveref}
\crefname{theorem}{Thm.}{Thm.}
\crefname{lemma}{Lem.}{Lem.}
\crefname{proposition}{Prop.}{Prop.}
\crefname{equation}{Eqn.}{Eqn.}
\creflabelformat{equation}{#2(#1)#3}

% New code
\Cref{s:A} is based on \cref{s:B,s:C}, and specializes \cref{eqn:D}.
```

which produces

```latex
Thm. 1.4 is based on Lem. 1.1 and Prop. 1.2, and specializes Eqn. (1).
```

## Customizing `newcommand`

Writing up the math you have discovered is certainly important—but I think, just like in programming, you want to spend your time thinking and solving problems, and on explaining them well to your readers, but certainly _not_ typing. Take sets for example: Instead of

```latex
\big\{ x \in \mathbb{R} \middle| \int^x_{-1} f(x) dx < 1 \big\}
```

we could introduce a new command for sets thus saving us a few keystrokes **and** giving us a highly customizable command through

```latex
% Shortcut for proper scalings of braces
\newcommand{\sd}[2] {
	\ifthenelse{\equal{#1}{0}}{}{
		\ifthenelse{\equal{#1}{1}}{\big}{
			\ifthenelse{\equal{#1}{2}}{\Big}{
				\ifthenelse{\equal{#1}{3}}{\bigg}{
					\ifthenelse{\equal{#1}{4}}{\Bigg}{#2}}}}}
}

% Shorthand for sets
\newcommand{\st}[3][auto]{
	\sd{#1}{\left}\{ #2 \middle| #3 \sd{#1}{\right}\}
}

% Usage
\st[1]{x \in \IR}{\int^x_{-1} f(x) dx < 1}
```

Using [xparse](http://www.ctan.org/pkg/xparse) we can even add (optional) parameters at almost any position. This is very handy for things when you have many annotations to add to a mathematical expression, but want to have the freedom to change your notation later on without having to go through all of your document and alter each call one by one:

```latex
\usepackage{xparse}
% Closed balls with optional annotation
\NewDocumentCommand{\clb}{D(){auto} D[]{\empty} m m}{%
	\overline{\mathrm{ball}}_{#2} \sd{#1}{\left}( #3, #4 \sd{#1}{\right})
}

% Usage
... and $x \in \clb[\infty]{0}{2^{-n}}$.
```

producing the output `\overline{\mathrm{ball}}_\infty\big( 0, 2^{-n} \big)`—which is the same as if we had wrote

```latex
... and $x \in \clb(1)[\infty]{0}{2^{-n}}$.
```