为什么在使用 tikz externalize 时,`\usepackage{cleveref}` 会修复 (new) hyperref、amsthm 和 lastpage 之间的冲突?

为什么在使用 tikz externalize 时,`\usepackage{cleveref}` 会修复 (new) hyperref、amsthm 和 lastpage 之间的冲突?

这不是此内容的重复问题

为什么在外化过程中,添加可以解决、和\usepackage{cleveref}之间的冲突?lastpagehyperrefamsthm

已知的回答建议\tikzifexternalizing在不进行外部化时有条件地加载包,但是,我无法这样做,因为包是由我需要使用的类加载的。显然,解决方案可以做任何事情,但不能改变类untouchable。它是 MDPI 类模板,但我将其简化为untouchable最小示例中的类,如下所示:

\begin{filecontents}{untouchable.cls}
\LoadClass{article}
% This is a model of a class file that requires the packages
% that impedes the pgf-tikz externalization.
% I tested this example on Texlive 2016, 2018 and 2020.
% It works on 2016.
% It fails on 2018 and 2020 but you can make it work if
% only one of the three following lines is commented out.
% Remember to remove existing untouchable.cls!
\RequirePackage{amsthm}
\RequirePackage{lastpage}
\RequirePackage{hyperref}
% It works with 2018 and 2020 when i downgrade to the
% hyperref from 2016, by moving it into the ~/texmf.
\end{filecontents}

\documentclass{untouchable}

% The workaround is to include cleveref. A hunch i had,
% after diffing the hyperref.sty from 2016 and 2018 near
% lines 7335. Uncomment the following to make it work:
%\usepackage{cleveref}

\usepackage{tikz}
\usetikzlibrary{external}
% The lastpage warning about the aux file can be silenced by
% switching to make externalization, but this does not solve
% the issue. You can test it by uncommenting the next line:
%\tikzset{external/mode=list and make}

\tikzexternalize

% This implementation of the current answer from Ulrike Fischer
% does not seem to patch anything, as indicated by the tracingpatches output:
\usepackage{etoolbox}
\tracingpatches
\makeatletter
\tikzifexternalizing{%
    \patchcmd\AtEndDocument{\lastpage@fileswtest{\thepage}{\lastpage@lastpage}}{}{\fail}
    \patchcmd\AtEndDocument{\lastpage@fileswtestHy}{}{\fail}}{}
\makeatother

\begin{document}
The document must include at least one tikzpicture:
\begin{tikzpicture}
\draw (0,0) rectangle (2,3);
\end{tikzpicture}
\end{document}

它以相同的方式失败并出现错误:

! Argument of \Hy@temp has an extra }.
<inserted text>
                \par
l.20 \end{document}

有趣的是,我发现当我使用 Texlive 2016 中的 hyperref 时,一切似乎都很好。 diffhyperref.sty给了我一个想法,即包含cleveref似乎可以解决问题的内容。但为什么呢?有人可以检查代码并排除这是一个错误吗?

相关内容