# Externalized pgf pictures are not centered in figure environment

I try to use pgf/tikz with externalized images as described in section 107 of the manual. I am not using the "external" tikz-library because I have problems with the required shell-escape functionality and I like the simplicity of using makefiles for regenerating my images.

Basically everything works. The only problem is that the images are not centered in the figure environment when externalized. Instead they are left aligned.

This is a minimal example (save as "tikz_debug.tex"):

\documentclass{article}
\usepackage{tikz}
\pgfrealjobname{tikz_debug}
\begin{document}
One line of text so we see the figure positioning relative to the text.
\begin{figure}
\centering
\beginpgfgraphicnamed{test}
\begin{tikzpicture}
\draw (0,0) rectangle (2,2);
\end{tikzpicture}
\endpgfgraphicnamed
\end{figure}
\end{document}

When compiling directly without a "test.pdf" being present the rectangle is centered as expected. If a "test.pdf" is present (create with "pdflatex --jobname=test tikz_debug") the rectangle is instead left aligned.

If I include the image with the command shown in the manual (107.4)

\long\def\beginpgfgraphicnamed#1#2\endpgfgraphicnamed{\includegraphics{#1}}

then the centering works. If I instead use "\usepackage{tikzexternal}" it does not. I think I have narrowed the problem down to tikz placing the "\includegraphics" into an "\hbox" but I am not well versed enough in TeX to understand the reasons and I fully expect me to do something wrong.

So my question is: how do I use "\beginpgfgraphicnamed" with centered figures?

• certainly an \hbox would not centre, you could use \leavevmode\beginpgfgraphicnamed probably which would allow an hbox to centre (untested) – David Carlisle May 9 '16 at 9:38
• @DavidCarlisle if you make that an answer I will accept it. Although I was hoping for some pgf-setting I missed your method works. And since it has been a week I doubt that there will be more answers. – Pathim May 17 '16 at 8:30