# Awkward conflict between lastpage, hyperref, amsthm and tikz-external

The following code cannot correctly produce the external .pdf file of the tikzpicture. Removing any of \usepackage{amsthm}, \usepackage{lastpage}, \usepackage{hyperref} solves the issue: the auxiliary .pdf file is correctly produced. Removing \tikzexternalize also solves the problem, because no externalization process is involved.

\documentclass{article}
\usepackage{amsthm}
\usepackage{lastpage}
\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\begin{document}
\begin{tikzpicture}
\draw (0,0) --(1,1);
\end{tikzpicture}
\end{document}


The .log file of the external compilation contains the following

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


I've also tried to swap the order of the packages, with no result. What can be causing this problem?

• I put hyperref last and got no errors (other than never having gotten \tikzexternalize to work on my system). – John Kormylo Nov 28 '17 at 15:05
• What's the exact order you used? I can't make it work :( – Rackbox Nov 28 '17 at 15:09
• I just moved hypperref after \tikzexternalize. BTW, of the packages used \Hy@temp only shows up in hyperref (of course) and lastpage. – John Kormylo Nov 28 '17 at 15:12
• I can't make it work... Are you sure you deleted the previously created external .pdf? If a previous compilation successfully created the file, then the next compilations will re-use it without trying to generate it again. – Rackbox Nov 28 '17 at 15:24
• I use standalone and \includegraphics instead. Externalize will never be any faster, just more convenient. – John Kormylo Nov 28 '17 at 16:58

Some packages are just incompatible with externalization, see section 50.8.2 in the TikZ/PGF manual.

\documentclass{article}
\usepackage{amsthm}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize

\tikzifexternalizing{}{
\usepackage{lastpage}
\usepackage{hyperref}
}

\begin{document}

\begin{tikzpicture}
\draw (0,0) --(1,1);
\end{tikzpicture}

\end{document}