交叉引用未正確錨定

交叉引用未正確錨定

我有這樣的書面代碼

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,hmargin=2cm,vmargin={2cm,2.5cm}]{geometry}
\usepackage[colorlinks=false]{hyperref}
\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       
        \begin{equation}
        a = b \label{eq:Eq1.1.a.1}
        \end{equation}
        \end{itemize}
    \end{itemize}
    \begin{equation}
    a = b \label{eq:Eq1.1.a.2}
    \end{equation}
\end{enumerate}
\subsection{Test 1.2}
\begin{equation} 
\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}
\end{equation}
\subsection{Test 1.3}
Cross reference to \eqref{eq:AmbipEq} which is not properly anchored 
\end{document}

我想知道為什麼eq:AmbipEq沒有正確錨定,也許是由於\mathclap\substack使用?太感謝了!

答案1

當測試你的例子時,我得到了這條線

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

進入 .aux 文件。

這意味著引用標籤eq:AmbipEq會導致「列印」數字,1.2.1同時連結到名稱為 的錨點equation.1.1

.log 檔案包含一些警告。其中之一是:

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

這意味著由於某種原因,有兩次嘗試放置名稱為 的錨點equation.1.1
每個超連結確實連結到輸出檔案/.pdf-檔案中的那個位置,該位置對應於.TeX-輸入檔案/來源檔案中第一次嘗試放置該錨點的位置。

這是什麼原因?

此巨集\theequation用於指定如何「列印」方程式計數器的值。

當您使用 hyperref-package 時,除了列印值之外,還有另一個問題:

當將編號的「紅化項目」的新實例放入輸出檔案(pdf 檔案)時,即編號的章節標題,或編號圖片的標題,或編號表格的標題,或公式/方程式的數量,對應計數器的值被列印,並且放置超連結的錨點。該錨點需要一個在整個文件中唯一的名稱。

此巨集\theHequation用於指定使用 hyperref-package 時自動產生與方程式編號相關的錨點名稱的方式。

巨集的\theHequation指定方式必須確保錨點名稱的唯一性。

當您重新定義/更改時\theequation,您可能還需要調整/重新定義\theHequation以確保錨點名稱的唯一性。

因此我建議平行放置

\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}%
  }%
}

放入文檔的序言中。

(對於兩個程式碼片段,假設@都使用字母 (→ )。)\makeatletter..\makeatother

執行此操作時,我會進入 .aux 檔案一個條目

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

這意味著引用標籤eq:AmbipEq會導致「列印」數字,1.2.1 同時連結到名稱為 的錨點equation.1.2.1

關於具有相同識別符的目標的相應 pdfTeX 警告不會出現在 .log 文件中。

\documentclass[8pt,a4paper,dvipsnames]{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,hmargin=2cm,vmargin={2cm,2.5cm}]{geometry}
\usepackage[colorlinks=false]{hyperref}
\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
        \begin{equation}
        a = b \label{eq:Eq1.1.a.1}
        \end{equation}
        \end{itemize}
    \end{itemize}
    \begin{equation}
    a = b \label{eq:Eq1.1.a.2}
    \end{equation}
\end{enumerate}
\subsection{Test 1.2}
\begin{equation}
\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}
\end{equation}
\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}

順便一提:

我認為了解 (La)TeX 的閱讀裝置以及 (La)TeX 在輸入中遇到空格字元時產生空格標記(在輸出檔案中產生水平空格)的情況非常重要文件。
例如,輸入檔案中左大括號字元之後或右大括號字元之後的空格字元將產生一個空格標記,導致輸出檔案中的水平空格(如果在下列情況下遇到該空格標記): La)TeX 不處於其垂直模式之一)。
例如,左大括號字元後或右大括號字元後的換行符也會產生空格標記。這是因為在讀取輸入時,(La)TeX 在每行末尾插入一個空格字元 — 這與整數參數有關\endlinechar

%考慮在行結束處的左大括號字符之後以及在行結束處的右大括號字符之後使用註釋字符,即{%}%,以避免輸出文件/pdf 文件中出現不需要的水平空間。

\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}

相關內容