# Include/exclude section number in equation references depending on where the reference appears?

I'd like to have references to an equation exclude the section number when the equation is referenced within the same section, but include it when it's referenced in a different section.

Here's an example of what I'm attempting to obtain: Here's what I've tried, and I have a pretty good idea of what's wrong with it:

\documentclass{article}
\usepackage[noabbrev]{cleveref}

\renewcommand{\thesection}{\Alph{section}}
\creflabelformat{equation}{#2(\thesection.#1)#3}

\begin{document}
\section{The first section}\label{first}

This is the some disussion in \cref{first}. This is the first equation:
$$\label{A:1} 1+1 = 2$$
This is some more disussion. Here is a reference to \cref{A:1}.

\section{The second section}\label{second}
\setcounter{equation}{0}

This is some discussion in \cref{second} which references \cref{first}.
This is another equation:
$$\label{B:1} 2+2 = 4$$
This is some discussion of \cref{B:1}, and a brief comment that it is
easily derived from equation \cref{A:1}.

\end{document}


Unfortunately, I don't know how get the result I'm looking for.

• As a reader, I must confess this format of numbering is extremely confusing. I remembered a textbook on probability theory written in this fashion and I was so confused that I decided to switch to another book altogether. You’re basically asking the readers to decide whether a section number should be prepend to an equation number, which makes your complicated math even more difficult to understand. – Ruixi Zhang Jul 30 '18 at 3:57
• – John Kormylo Jul 30 '18 at 13:22

## 2 Answers

I can only agree with the comment from Ruixi Zhang: This format often is confusing for the reader and I strongly advice against using it.

If you still want to implement it, you have to modify theequation instead of setting creflabelformat because creflabelformat is only evaluated when the label is referenced. At this point, the original section the equation was in is already lost.

To skip the section if it is the same as the section containing the reference, I defined a small macro. This currently only works for pdfTeX but \pdfstrcmp could easily be replaced by expl3's \str_if_eq_x:nnF to be compatible with more engines.

\documentclass{article}
\usepackage[noabbrev]{cleveref}
\usepackage{etoolbox}

\DeclareRobustCommand\skipifequals[2]{%
\ifnum\pdfstrcmp{#1}{#2}=0
\else
#2%
\fi
}
\newcommand\skipifcurrent[1]{\protecting{\skipifequals{#1}}{#1}}
\renewcommand{\thesection}{\Alph{section}}
\creflabelformat{equation}{#2(#1)#3}
\renewcommand\theequation{\skipifcurrent{\thesection.}\arabic{equation}}

\begin{document}
\section{The first section}\label{first}

This is the some disussion in \cref{first}. This is the first equation:
$$\label{A:1} 1+1 = 2$$
This is some more disussion. Here is a reference to \cref{A:1}.

\section{The second section}\label{second}
\setcounter{equation}{0}

This is some discussion in \cref{second} which references \cref{first}.
This is another equation:
$$\label{B:1} 2+2 = 4$$
This is some discussion of \cref{B:1}, and a brief comment that it is
easily derived from equation \cref{A:1}.

\end{document}


• I haven't found a reference to \str_compare_x:nnF anywhere in my current expl3 documentation. Presumably you mean \str_if_eq_x:nnF? – Derek Aug 1 '18 at 22:32

I ended up using an expl3 solution based on Marcel Krüger's answer:

\ExplSyntaxOn
\cs_new_protected:Npn \ifsection #1#2#3 {
\str_if_eq_x:nnTF{\thesection}{#1}{#2}{#3}
}
\cs_new:Npn \ifcurrentsection #1#2 { \ifsection{\thesection}{#1}{#2} }

\renewcommand\theequation{
\ifcurrentsection{}{\thesection.}\arabic{equation}
}
\ExplSyntaxOff


This allowed me to be a little more flexible and change the behavior depending whether the reference appears in the current section or a different one.