2

This is not a duplicate of this question.

Why does adding \usepackage{cleveref} solve the conflict between lastpage, hyperref, and amsthm during externalization?

The known answer proposes \tikzifexternalizing to conditionally load the packages when not externalizing, however, i cannot do this, since the packages are loaded by a class that i am required to use. Obviously, the solution can do everything but to change the untouchable class. It is the MDPI class template but i reduced it to the untouchable class in the minimal example as follows:

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

It fails the same way with the error:

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

Interestingly, i found that when i use the hyperref from Texlive 2016 everything seems fine. A diff on hyperref.sty gave me the idea to include cleveref which seems to solve it. But Why? Can some one insoder check the code and rule out that this is a bug?

  • In the log file for the externalized tikzpicture there is a complaint that lasitpage wasn't allowed to write on the aux file. However, what seems to work is \usepackage{zref-lastpage,zref-user} with which you can access the last page via \zpageref{LastPage}. – user194703 Apr 15 at 15:41
  • @Schrödinger's cat I think the warning can be ignored because \tikzset{external/mode=list and make} resolved the warning but not the error. – hefe Apr 15 at 19:19
  • 3
    I hate it if people change a question so that it makes an answer invalid and then not even have the decency to notify the one who answered. I will delete my answer and ignore your question. – Ulrike Fischer Apr 22 at 8:58
  • @Ulrike Fischer Sorry for that, i did not know that i am supposed to notify any author manually. In my defense, your answer did not work for the original question i couldn't accept in that state. Hope you are not ignoring me, but only the question. – hefe Apr 22 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.