18

Arrow heads from the following MWE are cropped when externalized. What am I doing wrong?

MWE:

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

\begin{document}
  \begin{tikzpicture}
     \draw[->,ultra thick] (0,0) -- (0,5);
     \draw[->,ultra thick] (0,0) -- (5,0);
  \end{tikzpicture}
\end{document}

enter image description here

4
  • 3
    You're not doing anything wrong. Arrowheads aren't taken into account when computing the bounding box of a tikzpicture. You can explicitly enlargen the bounding box if this cropping occurs, or there may be a sneakier way to do it automatically which I'm unaware of (but would love to know about). Commented Dec 11, 2012 at 13:18
  • Is it a duplicate of tex.stackexchange.com/q/71299/19356? Commented Dec 12, 2012 at 0:58
  • The newest update for TikZ CVS does now support the inclusion of arrowheads into the bounding box. See tex.stackexchange.com/a/134047!
    – LaRiFaRi
    Commented Sep 19, 2013 at 14:13
  • 1
    Just loading arrows.meta library solves the problem. See: tex.stackexchange.com/a/321854/1952
    – Ignasi
    Commented Aug 1, 2016 at 8:40

2 Answers 2

20

That is nasty, indeed. At least with pdf output (the resulting bounding box of the image is also used to clip the image). It might work better with ps output (I do not know).

I guess the only solution is to increase the size of the bounding box manually. In your example, you could use

\useasboundingbox (-0.2,-0.2);

as last statement (otherwise it would overwrite the entire bounding box). In our case, we simply "append" the (-0.2,-0.2) coordinate to the picture's bounding box.


This alters your image's bounding box, of course. That is probably what you want. If not, you might be interested in the following part of the answer.

A solution which keeps the current bounding box would be to rely on trim left, trim right, and baseline to undo the effects of \useasboundingbox.

Here is a possible approach:

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

\begin{document}
\thispagestyle{empty}

\fboxsep=0pt
\dotfill
\fbox{%
  \begin{tikzpicture}[trim left=0pt,trim right=5cm,baseline]
     \draw[->,ultra thick] (0,0) -- (0,5);
     \draw[->,ultra thick] (0,0) -- (5,0);

    \useasboundingbox (-0.2,-0.2);
  \end{tikzpicture}%
}
\dotfill
\end{document}

enter image description here

as you guessed correctly, the \fboxsep, \dotfill, and \fbox stuff is just syntactical sugar to verify that it works. The key idea is trim left,trim right,baseline combined with \useasboundingbox.

The first two keys allow to modify the bounding box of a picture and the third key controls vertical displacement. However, unlike the "tight" bounding box produced by the external lib, the resulting image has a larger bounding box than the trim values. The trim values are implemented by means of negative \hspaces and are also applied by the external lib. The baseline thing simply moves the complete image vertically; that appears to be enough.

1
  • Perhaps it's more "tikzien" to use ` \draw (current bounding box.south west) rectangle (current bounding box.north east);` instead of \fbox. Commented Apr 16, 2013 at 19:33
15

Here is a simple method to add some margin (2mm in this example) to your tikzpicture:

\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{external,fit}
\tikzexternalize

\begin{document}
  \begin{tikzpicture}
     \draw[->,ultra thick] (0,0) -- (0,5);
     \draw[->,ultra thick] (0,0) -- (5,0);

     \node[fit=(current bounding box),inner sep=2mm]{};
  \end{tikzpicture}
\end{document}
2
  • Sorry for the corpse reviving, but is there a method to add the \node{...} command automatically before every \end{tikzpicture} in the document? I haven't found anything in the tikz texdoc.
    – Fabian A.
    Commented Aug 2, 2019 at 16:22
  • 1
    @FabianA. Try to use the execute at end picture key in the every picture style (pp.127-128, pgfmanual, v3.1.4a). Something like \tikzset{every picture/.style={execute at end picture={\node[fit=(current bounding box),inner sep=2mm]{};}}}. Commented Aug 2, 2019 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .