# Cross reference is not properly anchored

I've got a written code like this

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,hmargin=2cm,vmargin={2cm,2.5cm}]{geometry}
\hypersetup{
pdftitle={...},
pdfauthor={...},
pdfsubject={...},
pdfkeywords={...},
}
\usepackage{enumitem}
\usepackage{chngcntr}
\usepackage{cleveref}
\usepackage{etoolbox,cancel,mathtools,physics}

\counterwithin*{equation}{section}
\counterwithin*{equation}{subsection}
\counterwithin*{equation}{enumi}

\newif\ifinenumerate
\AtBeginEnvironment{enumerate}{\inenumeratetrue}

\makeatletter
\renewcommand\theequation{%
\ifnum\value{subsection}>0 \thesubsection.\else
\ifnum\value{section}>0 \thesection.\fi\fi
\ifinenumerate \theenumi\fi
\arabic{equation}}
\makeatother

\begin{document}
\section{Test 1}
\subsection{Test 1.1}
\begin{enumerate}[label=\alph*.]
\item item a
\begin{itemize}
\item item a case 1
\begin{equation*}
a = b
\end{equation*}
\begin{itemize}
\item item a case 1 subcase 1
$$a = b \label{eq:Eq1.1.a.1}$$
\end{itemize}
\end{itemize}
$$a = b \label{eq:Eq1.1.a.2}$$
\end{enumerate}
\subsection{Test 1.2}
$$\underbrace{ \cancel{\pdv{t}\delta n} }_{ \mathclap{\substack{\text{stationary} \\ \text{case}}} } + \underbrace{ \cancel{\mu^*\va{E}\grad_{\va{r}}{\var n}} }_{ \substack{\mu^* = 0 \\ \text{if} \\ n_0 = p_0 = n_i} } - D^*\laplacian_{\va{r}}\delta n - \underbrace{ \frac{\delta n}{\tau^*} }_{ \mathclap{\substack{\text{it vanishes for} \\ \text{lengths less than}\ L}} } - g_L = 0 \label{eq:AmbipEq}$$
\subsection{Test 1.3}
Cross reference to \eqref{eq:AmbipEq} which is not properly anchored
\end{document}


And I would like to know why eq:AmbipEq is not properly anchored, maybe that's due \mathclap or \substack use? thank you so much!

• Please clarify what you mean by "not properly anchored". – Mico Nov 16 '16 at 16:52
• When I do click in \eqref the document is not displaced to the right formula. – jfernandz Nov 16 '16 at 17:08

When testing your example, I got the line

\newlabel{eq:AmbipEq}{{1.2.1}{1}{Test 1.2}{equation.1.1}{}}


into the .aux-file.

This means that referencing the label eq:AmbipEq leads to "printing" the number 1.2.1 while linking to an anchor whose name is equation.1.1 .

The .log-file contains some warnings. One of them is:

pdfTeX warning (ext4): destination with the same identifier (name{equation.1.1}) has been already used, duplicate ignored


This means that for some reason there were two attempts at placing an anchor whose name is equation.1.1.
Every hyperlink does link to that place in the output-file/.pdf-file which corrresponds to the place in the .TeX-input-file/source-file where the attempt of placing that anchor took place the first time.

What is the reason for this?:

The macro \theequation is used for specifying how to "print" the value of the equation counter.

When you use the hyperref-package, there is — besides printing the value — another issue:

When a new instance of a numbered "item of rubrification" is placed into the output-file (pdf-file), i.e., a numbered sectioning-heading, or the caption of a numbered picture, or the caption of a numbered table, or the number of a formula/an equation, both the value of the corresponding counter is printed and an anchor for hyperlinks is placed. That anchor needs a name which is unique within the entire document.

The macro \theHequation is used for specifying the way in which anchor-names related to equation-numbers will be generated automatically when the hyperref-package is in use.

The macro \theHequation must be specified in a way which ensures uniqueness of anchor-names.

When you redefine/change \theequation, you probably need to adjust/redefine \theHequation as well in order to ensure uniqueness of anchor-names.

Therefore I suggest parallel to placing

\renewcommand\theequation{%
\ifnum\value{subsection}>0 \thesubsection.\else
\ifnum\value{section}>0 \thesection.\fi\fi
\ifinenumerate \csname theenum\romannumeral\the\@enumdepth\endcsname\fi
\arabic{equation}%
}


placing something like

\AtBeginDocument{%
\renewcommand\theHequation{%
\ifnum\value{subsection}>0 \theHsubsection.\else
\ifnum\value{section}>0 \theHsection.\fi\fi
\ifinenumerate \csname theHenum\romannumeral\the\@enumdepth\endcsname.\fi
\arabic{equation}%
}%
}


into the preamble of your document.

(For both code snippets @being made a letter (→ \makeatletter..\makeatother) is assumed.)

When doing this, I get into the .aux-file an entry

\newlabel{eq:AmbipEq}{{1.2.1}{1}{Test 1.2}{equation.1.2.1}{}}


which means that referencing the label eq:AmbipEq leads to "printing" the number 1.2.1 while linking to an anchor whose name is equation.1.2.1 .

The corresponding pdfTeX-warning about destination with same identifier doesn't occur within the .log-file.

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,hmargin=2cm,vmargin={2cm,2.5cm}]{geometry}
\hypersetup{
pdftitle={...},
pdfauthor={...},
pdfsubject={...},
pdfkeywords={...},
}
\usepackage{enumitem}
\usepackage{chngcntr}
\usepackage{cleveref}
\usepackage{etoolbox,cancel,mathtools,physics}

\counterwithin*{equation}{section}
\counterwithin*{equation}{subsection}
\counterwithin*{equation}{enumi}

\newif\ifinenumerate
\AtBeginEnvironment{enumerate}{\inenumeratetrue}

\newcommand{\prr}{\\[0.5cm]} %

\makeatletter
\renewcommand\theequation{%
\ifnum\value{subsection}>0 \thesubsection.\else
\ifnum\value{section}>0 \thesection.\fi\fi
\ifinenumerate \csname theenum\romannumeral\the\@enumdepth\endcsname\fi
\arabic{equation}%
}
\AtBeginDocument{%
\renewcommand\theHequation{%
\ifnum\value{subsection}>0 \theHsubsection.\else
\ifnum\value{section}>0 \theHsection.\fi\fi
\ifinenumerate \csname theHenum\romannumeral\the\@enumdepth\endcsname.\fi
\arabic{equation}%
}%
}
\makeatother

\begin{document}
\section{Test 1}
\subsection{Test 1.1}
\begin{enumerate}[label=\alph*.]
\item item a
\begin{itemize}
\item item a case 1
\begin{equation*}
a = b
\end{equation*}
\begin{itemize}
\item item a case 1 subcase 1
$$a = b \label{eq:Eq1.1.a.1}$$
\end{itemize}
\end{itemize}
$$a = b \label{eq:Eq1.1.a.2}$$
\end{enumerate}
\subsection{Test 1.2}
$$\underbrace{ \cancel{\pdv{t}\delta n} }_{ \mathclap{\substack{\text{stationary} \\ \text{case}}} } + \underbrace{ \cancel{\mu^*\va{E}\grad_{\va{r}}{\var n}} }_{ \substack{\mu^* = 0 \\ \text{if} \\ n_0 = p_0 = n_i} } - D^*\laplacian_{\va{r}}\delta n - \underbrace{ \frac{\delta n}{\tau^*} }_{ \mathclap{\substack{\text{it vanishes for} \\ \text{lengths less than}\ L}} } - g_L = 0 \label{eq:AmbipEq}$$
\begin{align}
n &= N_C e^{\left(\frac{E_F-E_C}{K_B T}\right)} \nonumber
\prr
\frac{N_D^+}{N_D^0} &= \frac{1}{g_D}e^{\left(\frac{E_D-E_F}{K_BT}\right)} \label{eq:EqToLabeling}
\end{align}
\subsection{Test 1.3}
Cross reference to \eqref{eq:AmbipEq} which is hopefully properly anchored

Cross reference to \eqref{eq:EqToLabeling} which is hopefully properly anchored as well.
\end{document}


By the way:

I think it is important to learn about the reading-apparatus of (La)TeX and about the circumstances under which (La)TeX will produce space-tokens (yielding horizontal space in the output file) when encountering a space-character in the input file.
E.g., a space-character in the input-file after an opening-brace-character or after a closing-brace-character will yield a space-token leading to horizontal space in the output file (if that space-token is encountered when (La)TeX is not in one of its vertical modes).
E.g., a line break after an opening-brace-character or after a closing-brace-character will yield a space-token as well. That's because when reading input, (La)TeX inserts a space-character at the end of each line — this has to do with the integer-parameter \endlinechar.

Consider using the comment-char % after opening-brace-characters at line-endings and after closing-brace-characters at line-endings , i.e.,{% or }%, in order to avoid unwanted horizontal space within the output-file/pdf-file.

\documentclass{article}

\newcommand\TeXA{\TeX{
}\TeX}

\newcommand\TeXB{\TeX{}
\TeX}

\newcommand\TeXC{\TeX{%
}\TeX}

\newcommand\TeXD{\TeX{}%
\TeX}

\begin{document}

\noindent There are subtle differences in spacing:

\noindent \TeXA

\noindent \TeXB

\noindent \TeXC

\noindent \TeXD

\end{document}

• But this not works properly when you use also align environment. – jfernandz Nov 16 '16 at 23:05
• @jfernan Please provide an example which people can have compiled in order to see your way of using the align environment and to reproduce errors/erroneous results of the usage of the align environment. – Ulrich Diez Nov 17 '16 at 2:26
• I get the following log error when I use your solution pastebin.com/wJpYrKbi – jfernandz Nov 17 '16 at 15:18
• You should be able to reproduce this isse with this code pastebin.com/B6mGLLs1 – jfernandz Nov 17 '16 at 17:29
• @jfernan I messed up in the first round: Due to the simple \let-assignment, things like \thesection, \thesubsection and \theenumi slipped into my redefinition of \theHequation. This was a big big mistake of mine. Should be \theHsection, \theHsubsection and \theHenumi. Therefore a \renewcommandis needed. Stupid me! Instead of \theenumiI resorted to the construct \csname theenum\romannumeral\the\@enumdepth\endcsname which - I hope - does take into account the possibility of nesting enumerate environments. I edited my answer accordingly. – Ulrich Diez Nov 18 '16 at 1:42