# tikzscale not working with externalize using named only

I found a weird behavior when using tikzscale in combination with the externalize-library:

When I configure externalize with only named = true, but do not set /pgfsetnextfilename for an image, this image is not being scaled anymore.

MWE:

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[
only named=true,
]
\usepackage{tikzscale}
\usepackage{filecontents}

\begin{document}

\begin{filecontents}{simple.tikz}
\begin{tikzpicture}[show background rectangle]
\draw (1,0) -- (0,1);
\end{tikzpicture}
\end{filecontents}

\tikzsetnextfilename{simple}  % no scaling is happening once this line is commented out
\includegraphics[width=10cm]{simple.tikz}

\end{document}


Is this an expected behavior, or did I miss something/ do something wrong?

EDIT: As expected, putting a tikzexternaldisable before the \includegraphics solves this problem.

• Not externalizing is not a solution :D Let's wait for someone to give more information and/or a solution. ;) – Enrico Maria De Angelis Jan 21 '16 at 22:08
• @EnricoMariaDeAngelis Actually, not externalising is the solution. That is precisely what is wanted. The problem is how to do this automagically given that tikzscale manages switches between externalisation and non-externalisation by relying on patches to the specific commands \tikzexternalenable and \tikzexternaldisable, which doesn't take account of other ways of enabling/disabling it - at least, not all other ways. – cfr Jan 22 '16 at 1:22
• But if the picture is big.. it would be time consuming to compile it every time, wouldn't it be? – Enrico Maria De Angelis Jan 22 '16 at 7:33
• @cfr, you are right, not externalising is the desired solution. – Dux Jan 22 '16 at 9:12
• @EnricoMariaDeAngelis That's up to the document author ;). The code as written wants the picture not externalised but scaled. If externalisation was wanted, it would be named or another option would be used for externalisation which did not require naming ;). – cfr Jan 22 '16 at 13:40

I do not understand all of the details, but I think that the problem is that tikzscale patches the commands \tikzexternalenable and \tikzexternaldisable but not \tikzsetnextfilename. This means that when externalisation is enabled, due to \tikzsetnextfilename, we get the patched commands which tikzscale has provided. When it is disabled because the picture is not named, the original, unpatched commands are in place and no scaling is done.

In contrast, if you switch using explicit \tikzexternalenable and \tikzexternaldisable, then everything works fine. But if you rely on implicit enabling and disabling via \tikzsetnextfilename, things don't work as expected.

The real fix for this is probably in the hands of tikzscale's author or, if that's not practical, in the hands of the authors of TikZ/PGF and tikzscale.

As a workaround, you can patch \tikzsetnextfilename yourself to enable externalisation and set \tikzexternaldisable by default. However, I cannot currently work out how to issue \tikzexternaldisable automatically afterwards, so below I've issued this manually, which is not altogether satisfactory.

But perhaps this will be of some help, at least.

\begin{filecontents}{siml.tikz}
\begin{tikzpicture}[show background rectangle]
\draw (1,0) -- (0,1);
\end{tikzpicture}
\end{filecontents}
\documentclass{article}
\usepackage{tikz}
\usepackage{showframe}
\usetikzlibrary{external,backgrounds}
\tikzexternalize
[
only named=true,
]
\tikzexternaldisable
\usepackage{tikzscale}
\pretocmd{\tikzsetnextfilename}{\tikzexternalenable}{\typeout{Patch OK!}}{\typeout{Oh dear!}}
\begin{document}
\includegraphics[width=5cm]{siml.tikz}

\tikzsetnextfilename{siml}  % no scaling is happening once this line is commented out
\includegraphics[width=5cm]{siml.tikz}

\tikzexternaldisable
\includegraphics[width=5cm]{siml.tikz}
\end{document}


I reduced the scaling a bit to fit things on one page:

• I don't quite get what you are saying. Suppose externalize is switched off because \tikzsetnextfilename was NOT used: why should tikzscale use any of the makros of externalize then? Shouldn't the whole process of plotting and scaling the tikz-picture then be similar to the one where I did not load externalize in the first place? Maybe something to point out to the authors of tikzscale indeed. – Dux Jan 22 '16 at 9:18
• @Dux There are complications. Even when a particular picture is not externalised, it isn't always quite as if you were not externalising at all. And tikzscale patches the commands TikZ uses at the beginning of the document and then each time \tikzexternalenable and \tikzexternaldisable is used. I'm guessing that after the picture following a named picture, TikZ restores the original definitions without using \tikzexternaldisable directly, so that the patched commands are no longer active. – cfr Jan 22 '16 at 13:38
• You might be right, indeed. Thanks for clarifying – Dux Jan 22 '16 at 17:50
• @Dux I think it would be worth emailing the maintainer of tikzscale to report the problem, maybe asking for this method of externalisation to be supported or, failing that, for the documentation to make clear that the package requires externalisation to be handled in specific ways. – cfr Jan 22 '16 at 23:08