1

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
2

Certainly an \hbox would not centre as centering works by setting teh paragraph settings but a primitive box does not start a paragraph it is just stacked vertically below the previous material (in vertical mode)

You could use

\leavevmode\beginpgfgraphicnamed

where \leavevmode is a macro that does start a paragraph (all latex box commands such as \parbox or \mbox or \includegraphics use \leavevmode in this way so they work naturally in environments such as center or \flushright

  • Any speculation on why this is not documented in the PGF manual? I run into it every time. – Mankka Apr 18 '18 at 7:21
  • The \leavevmode command shifts the picture ever so slightly to the right. Why is this? – Mankka Apr 18 '18 at 7:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.