# Clever references to equations

In this question the author expressed the following desire

if Corollary 1.2 contains parts (a) and (b), I want \cref to output “Corollary 1.2(a)” when referring to that theorem part. Horever, in proofs and similar situations, I want to be able to refer to, say, “part (a)” without having “1.2” in front.

Note also that parts are numbered automatically (a), (b) rather than 1.2(a), 1.2(b).

I'd like to achieve the same effect, only with equations rather than with parts. In other words, I'd like the equations to be numbered without reference to the theorem, etc. in which they occur, and additionally I'd like to be able to refer to an equation by its simple number when the reference is in the same theorem, etc., but with its "full signature" when the reference is in a different theorem.

The current, undesired, state of affairs:

\documentclass{amsart}
\newtheorem{definition}{Definition}[section]
\newtheorem{theorem}[definition]{Theorem}
\numberwithin{equation}{definition}
\begin{document}

\section{The Theory}

\begin{definition}
We define $x$ by the following equation.
\begin{equation}\label{eq:1}
x = 1
\end{equation}
\end{definition}

\begin{theorem}
If $y = 1$, we have
\begin{equation}\label{eq:2}
y = x.
\end{equation}
\end{theorem}
\begin{proof}
By~\cref{eq:1}, $x = 1$. Since, by assumption $y = 1$, we obtain~\ref{eq:2}.
\end{proof}
\end{document} The desired state of affairs: Incidentally, as the "desired" example shows, I'd also like to get rid of the automatic eq. prefix in the clever reference, and I'd like the regular reference to enclose the number with parentheses.

P.S. Though the example above uses amsart as the document class, I'd like to know how to accomplish the same effect with the scrbook document class too.

• @RuixiZhang: Actually, it's the other way round: "By (1.1.1)" refers to x=1, whereas "obtain (1)" refers to y=x. – Evan Aad Sep 7 '18 at 15:43
• @RuixiZhang: The mixed system of referencing is already quite common with respect to theorem parts (and definition parts, etc.). See, for instance, the question I linked to in the beginning of my question. Why should it be more confusing when the references are to equations? – Evan Aad Sep 7 '18 at 15:47
• @RuixiZhang: According to the schema I desire, equations that are referenced in other sections (or even in other theorems, definitions, etc. in the same section), will be given full reference, e.g. (2.1.1) rather than simply (1). – Evan Aad Sep 7 '18 at 15:58
• I see. Thank you for clarifying. I’ll remove my comments then. – Ruixi Zhang Sep 7 '18 at 16:03

If you look at the aux file, you see that the information you want is simply not stored/available. This solution does not use cleveref at all.

\documentclass{scrbook}
\usepackage{amsmath,amsthm}

\usepackage{hyperref}

\newtheorem{definition}{Definition}[chapter]
\newtheorem{theorem}[definition]{Theorem}
\numberwithin{equation}{definition}

\renewcommand{\theequation}{\arabic{equation}}
\makeatletter
{\newcommand{\mylabel}{\refstepcounter{equation}%
\tag{\theequation}%
\label{#1}%
{\protected@write\@auxout{}{\string\newlabel{#1.def}%
{{\thedefinition.\@currentlabel}{\thedefinition}{\@currentlabelname}{\@currentHref}{}}}}}}%
{\newcommand{\mylabel}{\refstepcounter{equation}%
\tag{\theequation}%
\label{#1}%
{\protected@write\@auxout{}%
{\string\newlabel{#1.def}{{\thedefinition.\@currentlabel}{\thedefinition}}}}}}%
\makeatother
\newcommand{\myref}{(\ref{#1.def})}% \pageref{#1.def) returns \thedefinition
\begin{document}
\chapter{A Chapter}
\section{A Section}

\begin{theorem}
A theorem.
\end{theorem}
\begin{proof}
\begin{align}
a &= b\mylabel{eq:1}\\
&= c\mylabel{eq:2}
\end{align}
\end{proof}

\begin{definition}
See~\myref{eq:2}. \ref{eq:2}
\end{definition}
\end{document}

• Thanks. It looks like a great solution, however, it fails in the presence of package hyperref. If you replace the line that you commented out by \usepackage{hyperref}, an error is reported. Is there a way to solve this? – Evan Aad Sep 8 '18 at 7:19
• Hyperref changes the number of arguments used by \newlabel. \@ifpackageloaded may not be an elegant solution, but it works. Note, I now store the formatted value for \ref and \thedefinition for \pageref. – John Kormylo Sep 8 '18 at 14:48
• Thanks, John. It looks like you've nailed it as far as document class amsart goes. However, when I tried to apply the same solution with document class scrbook, the references didn't work as they should. I added an appendix to my original question demonstrating the problem. – Evan Aad Sep 8 '18 at 16:40
• I've accepted your answer, because you answered my original question. Nevertheless, it would be nice to know how to do it with scrbook too. – Evan Aad Sep 9 '18 at 14:36
• Actually, the problem has to do with amsmath which redefines \label so as to delay its expansion until later (after \stepcounter{equation}. The fix is to use \tag{\theequation} and do your own \refstepcounter{equation}. – John Kormylo Sep 9 '18 at 15:04 This solves the part that removes eq. prefix while still getting a cross-referencing that works with hyperref

Use the following code to define a new cross-referencing scheme for a special type of equations labels called equationX.

\crefformat{equationX}{#2(#1)#3}
\crefrangeformat{equationX}{#3(#1)#4 to #5(#2)#6}
\crefmultiformat{equationX}{#2(#1)#3}{ and #2(#1)#3}{, #2(#1)#3}{ and #2(#1)#3}
\crefrangemultiformat{equationX}{#3(#1)#4 to #5(#2)#6}{ and #3(#1)#4 to #5(#2)#6}{, #3(#1)#4 to #5(#2)#6}{ and #3(#1)#4 to #5(#2)#6}


For any equation that you would like to cite without eq. prefix, use the label \label[equationX]{<equation label>}

\documentclass{amsart}
\usepackage[x11names]{xcolor}

\usepackage{hyperref}

\newtheorem{definition}{Definition}[section]
\newtheorem{theorem}[definition]{Theorem}
\numberwithin{equation}{definition}

\crefformat{equationX}{#2(#1)#3}
\crefrangeformat{equationX}{#3(#1)#4 to #5(#2)#6}
\crefmultiformat{equationX}{#2(#1)#3}{ and #2(#1)#3}{, #2(#1)#3}{ and #2(#1)#3}
\crefrangemultiformat{equationX}{#3(#1)#4 to #5(#2)#6}{ and #3(#1)#4 to #5(#2)#6}{, #3(#1)#4 to #5(#2)#6}{ and #3(#1)#4 to #5(#2)#6}

\begin{document}

\section{The Theory}

\begin{definition}
We define $x$ by the following equation.
\begin{equation}\label[equationX]{eq:1}
x = 1
\end{equation}
\end{definition}

\begin{theorem}
If $y = 1$, we have
\begin{equation}\label{eq:2}
y = x.
\end{equation}
\end{theorem}
\begin{proof}
By~\cref{eq:1}, $x = 1$. Since, by assumption $y = 1$, we obtain~\ref{eq:2}.
\end{proof}
\end{document}


Although I do not recommend it for long documents, if you still want to change the representation of equations labels, remove \numberwithin{equation}{definition} To reset equations numbering at each definition or theorem environment, use

\usepackage{etoolbox}
\BeforeBeginEnvironment{definition}{\setcounter{equation}{0}}
\BeforeBeginEnvironment{theorem}{\setcounter{equation}{0}} \documentclass{amsart}
\usepackage{etoolbox}
\BeforeBeginEnvironment{definition}{\setcounter{equation}{0}}
\BeforeBeginEnvironment{theorem}{\setcounter{equation}{0}}

\usepackage[x11names]{xcolor}

\usepackage{hyperref}

\newtheorem{definition}{Definition}[section]
\newtheorem{theorem}[definition]{Theorem}

\crefformat{equationX}{#2(#1)#3}
\crefrangeformat{equationX}{#3(#1)#4 to #5(#2)#6}
\crefmultiformat{equationX}{#2(#1)#3}{ and #2(#1)#3}{, #2(#1)#3}{ and #2(#1)#3}
\crefrangemultiformat{equationX}{#3(#1)#4 to #5(#2)#6}{ and #3(#1)#4 to #5(#2)#6}{, #3(#1)#4 to #5(#2)#6}{ and #3(#1)#4 to #5(#2)#6}

\begin{document}

\section{The Theory}

\begin{definition}
We define $x$ by the following equation.
\begin{equation}\label[equationX]{eq:1}
x = 1
\end{equation}
\begin{equation}
e = m c^{2}
\label[equationX]{eq:e}
\end{equation}
\end{definition}

\begin{theorem}
If $y = 1$, we have
\begin{equation}\label{eq:2}
y = x.
\end{equation}
\end{theorem}
\begin{proof}
By~\cref{eq:1}, $x = 1$. Since, by assumption $y = 1$, we obtain~\ref{eq:2}.
\end{proof}

We can also refer to \ref{eq:e} as a test of the {\color{DodgerBlue3}hyperref} compatibility.

\end{document}


NOTE: The output above does not produce exactly what you want since you want the tag to have different form from the output of its cross-referencing. You can definitely change things like whether you want brackets or some prefixes or suffixes. However, changing the tag retroactively after you have cross-referenced the item is too complex. Here is why

The inputs #1 and #2 in the definitions of \cref formats represent the whole tags of what you are referring to. Thus, you cannot access what is inside #1 and #2 without too much of invasive modifications to the primitive codes of cleveref. My advice is to work around this by cross-referencing the equation then the theorem it exists in. For instance, you can write: we obtain \cref{<label of the equation>} in \cref{<label of the theorem that has the equation>}. Otherwise, you must be very careful and know exactly what you are doing to avoid some unexpected anomalies in cross-referencing.

• Thanks. However, I'd like the equations to be numbered starting from 1 in each theorem, definition, etc., just like theorem parts (resp. definition parts, etc.) are numbered starting from 1 in each theorem, definition, etc. – Evan Aad Sep 8 '18 at 8:44
• Thanks again for the edit. However, it still doesn't comply with my requirements, since when the reference is made from a different theorem/definition/etc. than the one where the label is defined, the numbering should include the number of the theorem/definition/etc. of origin. See the second screenshot I included in my original question. – Evan Aad Sep 8 '18 at 9:48