DEV Community

 Carsten Rösnick-Neugebauer
Carsten Rösnick-Neugebauer

Posted on • Originally published at carstenrn.com on

On Auto-Updating References in LaTeX Documents

I am using LaTeX 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 and 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 solves this problem elegantly:

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

becomes

% 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}.
Enter fullscreen mode Exit fullscreen mode

which produces

Thm. 1.4 is based on Lem. 1.1 and Prop. 1.2, and specializes Eqn. (1).
Enter fullscreen mode Exit fullscreen mode

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

\big\{ x \in \mathbb{R} \middle| \int^x_{-1} f(x) dx < 1 \big\}
Enter fullscreen mode Exit fullscreen mode

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

% 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}
Enter fullscreen mode Exit fullscreen mode

Using 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:

\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}}$.
Enter fullscreen mode Exit fullscreen mode

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

... and $x \in \clb(1)[\infty]{0}{2^{-n}}$.
Enter fullscreen mode Exit fullscreen mode

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay