pgf icon indicating copy to clipboard operation
pgf copied to clipboard

Feature Contribution: better automatic naming for tikz externalization

Open JSlote opened this issue 3 months ago • 3 comments

Hi TikZ community, in my recent papers I've been heavily using tikz externalize because there are tons of quantum circuit figures. I have encountered a general useability problem with the tikz externalize package though, and wanted to offer the solution that I've been using which seems to work quite well.

Problem: while using tikz-externalize, the automatic naming feature is unstable; it labels graphics by their order in the file, so if you are shuffling figures around they always get recompiled simply because the names get swapped--even if the contents of the figures stay the same. On the other hand, one can manually name each graphic but that can be quite exhausting.

Solution: give the figures a name derived from a hash of the code defining the figure. This has the dual advantages of (a) not requiring any manual intervention and (b) allows the cached version to continue to be reused even after reordering figures in the source.

Usage:

\autoname{
  \begin{tikzpicture}
  % drawing code here
  \end{tikzpicture}
}

Code:

\ExplSyntaxOn
% One scratch tl for the diagram code, one for the hash
\tl_new:N  \l__autotikz_code_tl
\tl_new:N  \l__autotikz_hash_tl

% \AutoTikz{ <code> }
\NewDocumentCommand \autoname { m }
 {
   % 1. Store the raw argument *verbatim* (no expansion)
   \tl_set:Nn \l__autotikz_code_tl { #1 }

   % 2. Compute MD5 on a *detokenized* snapshot (one expansion only)
   \tl_set:Nx \l__autotikz_hash_tl
     { \pdfmdfivesum { \detokenize \expandafter { \l__autotikz_code_tl } } }

   % 3. Tell TikZ to use that hash as the filename
   \tikzsetnextfilename { tikz-\l__autotikz_hash_tl }

   % 4. Finally typeset the diagram
   #1
 }
\ExplSyntaxOff

I would be very happy to contribute an (improved) version of the above as a naming option to tikz. Please let me know if and how I should do this. For example, I would guess the above actual implementation is quite amateurish and would be happy to make it better before sending a PR.

Thanks for such a wonderful software package.

JSlote avatar Oct 02 '25 18:10 JSlote

The external library is currently unmaintained and it is unlikely that any bugs are going to be fixed in the future, due to the inherent defective design of the library. However, there are other third-party libraries for externalization which have learned from these past mistakes and which seem to be quite usable:

  • https://github.com/sasozivanovic/memoize / https://ctan.org/pkg/memoize (same author as the venerable forest)
  • https://github.com/leo-colisson/robust-externalize / https://ctan.org/pkg/robust-externalize

Both are included in TeX Live 2025.

hmenke avatar Oct 03 '25 06:10 hmenke

Your proposed \autoname also has significant drawbacks, namely that it tokenizes its argument. This will fail to compile

\autoname{
  \begin{tikzpicture}
    \matrix[matrix of nodes] {
      1 & 2 \\
      3 & 4 \\
    };
  \end{tikzpicture}
}

It's necessary to use ampersand replacement here.

hmenke avatar Oct 03 '25 06:10 hmenke

See also https://github.com/pgf-tikz/pgf/issues/758

hmenke avatar Oct 03 '25 06:10 hmenke