\cfload, \cfout, and related commands
Automatic referencing of definitions
The following code defines a number of commands for the automatic referencing of definitions
(i.e., the (cf. Definitions 1.2, 1.3, 2.4, and 2.8) at the end of sentences), the most
important of which are
\cfclear,
\cfadd,
\cfload, and
\cfout.
\ExplSyntaxOn
\seq_new:N \g_cflist_loaded
\seq_new:N \g_cflist_pending
\NewDocumentCommand{\cfadd} { m } {
\seq_if_in:NnF \g_cflist_loaded { #1 } {
\seq_if_in:NnF \g_cflist_pending { #1 } {
\seq_gput_right:Nn \g_cflist_pending { #1 }
}
}
}
\NewDocumentCommand{\cfconsiderloaded} { m } {
\seq_gput_right:Nn \g_cflist_loaded {#1}
}
\NewDocumentCommand{\cfremove} { m } {
\seq_gremove_all:Nn \g_cflist_pending { #1 }
}
\NewDocumentCommand{\cfload} { o } {
\seq_if_empty:NTF \g_cflist_pending {\unskip\IfValueT{#1}{\ignorespaces}} {
(cf.\ \cref{\seq_use:Nn \g_cflist_pending {,}})\IfValueTF{#1}{#1~}{\unskip}
\seq_gconcat:NNN \g_cflist_loaded \g_cflist_loaded \g_cflist_pending
\seq_gclear:N \g_cflist_pending
\IfValueT{#1}{\ignorespaces}
}
}
\NewDocumentCommand{\cfclear} {} {
\seq_gclear:N \g_cflist_loaded
\seq_gclear:N \g_cflist_pending
}
\NewDocumentCommand{\cfout} { o } {
\seq_if_empty:NTF \g_cflist_pending {\unskip\IfValueT{#1}{\ignorespaces}} {
(cf.\ \cref{\seq_use:Nn \g_cflist_pending {,}})\IfValueTF{#1}{#1~}{\unskip}
\seq_gclear:N \g_cflist_pending
\IfValueT{#1}{\ignorespaces}
}
}
\NewDocumentCommand{\ifnocf} { m } {
\seq_if_empty:NT \g_cflist_pending { #1 }
}
\ExplSyntaxOffThe commands are used in the following way:
\cfclear.\cfadd{label of the definition}
(usually, a command should be defined for any notation used and the \cfadd should
be put in the command definition, see the example below).\cfload, unless it is the Then ... sentence in a theorem/lemma/etc., where
\cfout should be used.\cfload and \cfout will expand to (cf. ...), referencing all
the definitions that have been used since the last time
\cfclear was called and which have not yet been referenced in another call to
\cfload
(if there are no such definitions, the commands will not output anything).
%% In the preamble
\newcommand{\evens}{\mathbf{E}\cfadd{def:evens}}
\newcommand{\odds}{\mathbf{O}\cfadd{def:odds}}
...
%% In the main document
\begin{definition}
\label{def:evens}
We denote by $\evens$ the set which satisfies
$\evens = \{2n \colon n\in\Z\}$.
\end{definition}
\begin{definition}
\label{def:odds}
We denote by $\odds$ the set which satisfies
$\odds = \{2n+1 \colon n\in\Z\}$.
\end{definition}
\cfclear
\begin{lemma}
Let $a,b\in\odds$ \cfload.
Then $a+b\in\evens$ \cfout.
\end{lemma}
...Definition 1. We denote by $\mathbf E$ the set which satisfies $\mathbf E = \{2n \colon n\in\mathbb Z\}$.
Definition 2. We denote by $\mathbf O$ the set which satisfies $\mathbf O = \{2n+1 \colon n\in\mathbb Z\}$.
Lemma 1. Let $a,b\in\mathbf O$ (cf. Definition 2). Then $a+b\in\mathbf E$ (cf. Definition 1).
...\cfconsiderloaded
takes a label as argument and excludes that label from being referenced in any
\cfout or \cfload
call until the next \cfclear. This is primarily useful when \cfload is
used inside definitions, to make sure that the definition does not reference itself, as in the
following example:
%% In the preamble
\newcommand{\evens}{\mathbf{E}\cfadd{def:evens}}
\newcommand{\odds}{\mathbf{O}\cfadd{def:odds}}
...
%% In the main document
\begin{definition}
\label{def:evens}
We denote by $\evens$ the set which satisfies
$\evens = \{2n \colon n\in\Z\}$.
\end{definition}
\cfclear
\begin{definition}
\label{def:odds}
\cfconsiderloaded{def:odds}
We denote by $\odds$ the set which satisfies
$\odds = \{a+1 \colon a\in\evens\}$
\cfload.
\end{definition}Definition 1. We denote by $\mathbf E$ the set which satisfies $\mathbf E = \{2n \colon n\in\mathbb Z\}$.
Definition 2. We denote by $\mathbf O$ the set which satisfies $\mathbf O = \{a+1 \colon a\in\mathbf E\}$ (cf. Definition 1).
Without the call to \cfconsiderloaded in the example above, the
\cfload in Definition 2 would result in (cf. Definitions 1 and 2).
\cfload even if one is not sure whether any new
definitions need to be referenced, since this command (as well as \cfout) will simply
output nothing in this case. However, \cfload is often called just after a maths
display at the end of a sentence. In this case, if there are no references to load, the period
.
needs to go inside the display, whereas it needs to go after the (cf. ...) otherwise.
Here, one can use the command \ifnocf, which outputs its single argument if there are
no definitions to reference and nothing otherwise, in conjunction with an optional argument on the
\cfload, as in the following example:
%% In the preamble
\newcommand{\evens}{\mathbf{E}\cfadd{def:evens}}
...
%% In the main document
\begin{definition}
\label{def:evens}
We denote by $\evens$ the set which satisfies
$\evens = \{2n \colon n\in\Z\}$.
\end{definition}
\cfclear
\begin{lemma}
...
\end{lemma}
\begin{proof}
...
This shows that
\begin{equation}
n\in\evens
\ifnocf.
\end{equation}
\cfload[.]
Hence, we obtain that
...
\end{proof}Definition 1. We denote by $\mathbf E$ the set which satisfies $\mathbf E = \{2n \colon n\in\mathbb Z\}$.
Lemma 1. ...
Proof.