\llabel and \lref

Local labels

The following code defines two commands, \llabel and \lref. \llabel, when used inside an athm or aproof environment creates a label that is local to this theorem/proof and which can be referenced inside the athm or corresponding aproof environment using \lref. This way, the same labels can be used in different athm/aproof pairs.

Code for the \llabel and \lref commands
\newcommand{\llabel}[1]{\label{\loc.#1}}

\ExplSyntaxOn

\clist_new:N \l_localreflist
\clist_new:N \l_reflist

\NewDocumentCommand{\lref} { m } {
  \clist_set:No \l_localreflist {#1}
  \clist_clear:N \l_reflist
  \clist_map_inline:Nn \l_localreflist { \clist_put_right:Nn \l_reflist {\loc.##1} }
  \cref{\l_reflist}
}

\NewDocumentCommand{\Lref} { m } {
  \clist_set:No \l_localreflist {#1}
  \clist_clear:N \l_reflist
  \clist_map_inline:Nn \l_localreflist { \clist_put_right:Nn \l_reflist {\loc.##1} }
  \Cref{\l_reflist}
}

\NewDocumentCommand{\itref}{ m m }{
  \clist_set:No \l_localreflist {#2}
  \clist_clear:N \l_reflist
  \clist_map_inline:Nn \l_localreflist { \clist_put_right:Nn \l_reflist {#1.##1} }
  \cref{\l_reflist}~in~\cref{#1}
}

\NewDocumentCommand{\Itref}{ m m }{
  \clist_set:No \l_localreflist {#2}
  \clist_clear:N \l_reflist
  \clist_map_inline:Nn \l_localreflist { \clist_put_right:Nn \l_reflist {#1.##1} }
  \Cref{\l_reflist}~in~\cref{#1}
}

\ExplSyntaxOff

Like the \cref command, \lref can take a comma separated list of labels and there is also an \Lref version, analogous to \Cref, for use at the start of a sentence.

Internally, the \llabel command creates a label of the form [label of the current theorem environment].[local label]. The label can be referenced outside of the athm/aproof pair by using this full form.

Here is an example that illustrates how these commands work:

\begin{athm}{lemma}{lem:sumrule}
  Let $f,g\in C^1(\R,\R)$. Then
  \begin{enumerate}[(i)]
      \item \llabel{it:C1}
      it holds that $f+g\in C^1(\R,\R)$,
      \item \llabel{it:sumderivative}
      it holds for all $x\in\R$ that
      \begin{equation}
          (f+g)'(x) = f'(x) + g'(x),
      \end{equation}
      and
      \item \llabel{it:diffderivative}
      it holds for all $x\in\R$ that
      \begin{equation}
          (f-g)'(x) = f'(x) - g'(x).
      \end{equation}
  \end{enumerate}
\end{athm}
\begin{aproof}
  ...
  This \proves[ep] \lref{it:C1}.
  ...
  This \proves[ep] \lref{it:derivative,it:diffderivative}.
\end{aproof}

\begin{athm}{thm}{thm:main}
  Let $f\colon\R\to\R$ satisfy for all $x\in\R$ that $f(x) = \sin(x) + x^2$.
  Then
  \begin{enumerate}[(i)]
      \item \llabel{it:C1}
      it holds that $f\in C^1(\R,\R)$ and
      \item \llabel{it:derivative}
      it holds for all $x\in\R$ that
      \begin{equation}
          f'(x) = \cos(x) + 2x.
      \end{equation}
  \end{enumerate}
\end{athm}
\begin{aproof}
  \Nobs that 
      \cref{lem:sumrule.it:C1,lem:sumrule.it:sumderivative} in \cref{lem:sumrule} 
  \prove
      \lref{it:C1,it:derivative}.
\end{aproof}
Lemma 1. Let $f,g\in C^1(\mathbb R,\mathbb R)$. Then
  • (i) it holds that $f+g\in C^1(\mathbb R,\mathbb R)$,
  • (ii) it holds for all $x\in\mathbb R$ that $$ (f+g)'(x) = f'(x) + g'(x), \tag{1} $$ and
  • (iii) it holds for all $x\in\mathbb R$ that $$ (f-g)'(x) = f'(x) - g'(x).\tag{2} $$
Proof of Lemma 1.
...
This proves item (i).
...
This establishes items (ii) and (iii). The proof of Lemma 1 is thus complete.
Theorem 2. Let $f\colon\mathbb R\to\mathbb R$ satisfy for all $x\in\mathbb R$ that $f(x) = \sin(x) + x^2$. Then
  • (i) it holds that $f\in C^1(\mathbb R,\mathbb R)$ and
  • (ii) it holds for all $x\in\mathbb R$ that $$ f'(x) = \cos(x) + 2x.\tag{3} $$
Proof of Theorem 2. Note that items (i) and (ii) in Lemma 1 imply items (i) and (ii). The proof of Theorem 2 is thus complete.

There is also a special command, \itref/\Itref, for expressions of the form item (i) in Lemma 2. It takes two arguments, the label of the theorem and the local label. In the above example, we could have used \itref{lem:sumrule}{it:C1,it:sumderivative} to output items (i) and (ii) in Lemma 1.

Unless otherwise noted, all LaTeX snippets on this site are released under the Zero-Clause BSD (0BSD) license. You may copy them into your documents without attribution or including any license. No warranty.