2

Here is a small MWE which creates a warning:

\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{enumitem}
\usepackage{hyperref}

\declaretheorem[name=Theorem,refname={theorem,theorem},Refname={Theorem,Theorem}]{theorem}

\begin{document}

\chapter{Test chapter}

\begin{theorem}
    \label{ABCtheorem}
    \begin{enumerate}
        \item s
    \end{enumerate}
\end{theorem}

Theorem~\ref{ABCtheorem}

\end{document}

Compiling with Overleaf, the log file states that

pdfTeX warning (dest): name{theorem.1} has been referenced but does not exist, replaced by a fixed one

This is confusing. The only reference that I have is ABCtheorem which is exactly the same in \label{ABCtheorem} as it is in \ref{ABCtheorem}. According to the answer by Poeh, adding \hspace{2em} after \label removes the warning (and indeed it does). As does adding (any) text such as xyz after \label. And here, only ASCII characters are used.

But I would prefer to avoid adding unjustified snippets if possible. Is the problem that enumerate and \label are the only things inside theorem, and that this is 'incorrect'?


Addendum

Replacing \label with \hypertarget and \ref with \hyperlink also gives no warning.

\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{hyperref}

\declaretheorem[name=Theorem,refname={theorem,theorem},Refname={Theorem,Theorem}]{theorem}

\begin{document}

\chapter{Test chapter}

\begin{theorem}
    \hypertarget{ABCtheorem}{}
    \begin{enumerate}
        \item s
    \end{enumerate}
\end{theorem}

Theorem~\hyperlink{ABCtheorem}{1}

\end{document}

1 Answer 1

4

amsthm delays some code to the begin of the paragraph by putting it into \everypar. hyperref extends this code to create the anchor.

The problem is that enumerate sets \everypar too, and if the list is directly at the begin it overwrites the amsmath code before the anchor could be set.

The problem is known since a long time, see e.g. https://github.com/latex3/hyperref/issues/48.

I think with the new para hooks in LaTeX it is possible to resolve the problem along the following lines, I will check if one can update hyperref to do this:

\documentclass[12pt]{book}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage{enumitem}
\usepackage{hyperref}

\makeatletter
\AtBeginDocument
 {
    \def\@thm#1#2#3{%
      \ifhmode
        \unskip\unskip\par
      \fi
      \normalfont
      \trivlist
      \let\thmheadnl\relax
      \let\thm@swap\@gobble
      \let\thm@indent\indent % indent
      \thm@headfont{\scshape}% heading font small caps
      \thm@notefont{\fontseries\mddefault\upshape}%
      \thm@headpunct{.}% add period after heading
      \thm@headsep 5\p@ plus\p@ minus\p@\relax
      \thm@space@setup
      #1% style overrides
      \@topsep \thm@preskip               % used by thm head
      \@topsepadd \thm@postskip           % used by \@endparenv
      \def\dth@counter{#2}%
      \ifx\@empty\dth@counter
        \def\@tempa{%
          \@oparg{\@begintheorem{#3}{}}[]%
        }%
      \else
        \H@refstepcounter{#2}%
        \hyper@makecurrent{#2}%
        \let\Hy@dth@currentHref\@currentHref
        \AddToHookNext{para/begin}{\MakeLinkTarget*{\Hy@dth@currentHref}}%
        \def\@tempa{%
          \@oparg{\@begintheorem{#3}{\csname the#2\endcsname}}[]%
        }%
      \fi
      \@tempa
    }%
  \dth@everypar={%
    \@minipagefalse \global\@newlistfalse
    \@noparitemfalse
    \if@inlabel
      \global\@inlabelfalse
      \begingroup \setbox\z@\lastbox
       \ifvoid\z@ \kern-\itemindent \fi
      \endgroup
      \unhbox\@labels
    \fi
    \if@nobreak \@nobreakfalse \clubpenalty\@M
    \else \clubpenalty\@clubpenalty \everypar{}%
    \fi
  }%
}

\declaretheorem[name=Theorem,refname={theorem,theorem},Refname={Theorem,Theorem}]{theorem}
%\newtheorem{theorem}{Theorem}
\begin{document}


\chapter{Test chapter}
\begin{theorem}
    \label{ABCtheorem}
    \begin{enumerate}
        \item s
    \end{enumerate}
\end{theorem}

\begin{theorem}
    xxx\label{ABCtheorem2}
    \begin{enumerate}
        \item s
    \end{enumerate}
\end{theorem}


Theorem~\ref{ABCtheorem} \ref{ABCtheorem2}

\end{document}
1
  • Thank you for the answer, the reference to an earlier known instance of the problem, and the kind willingness to check if a solution can be integrated into the package hyperref! Feb 15, 2023 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .