25

The arrow tail at the bottom left corner is not properly cropped.

enter image description here

\documentclass[tikz,border=0pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=3,line width=3pt]
\draw[red] (0,0) grid (1,1);
\draw[blue,|->] (0,0) -- (1,1);
\end{tikzpicture}
\end{document}

Without changing the border option, how to make a smarter cropping with standalone or preview packages?

5
  • 1
    Arrow shapes don't update the bounding box of the TikZ pictures and line joins too. Add \draw[line join=miter] (1,1) -- (0,0.5) -- (1,0); to see that. You have to update the bounding box.
    – percusse
    Commented Sep 12, 2012 at 17:18
  • @percusse: Should we update the bounding box manually? Is it cumbersome for us? Commented Sep 12, 2012 at 17:20
  • 1
    I guess so unless you hack into arrow drawing scheme and add the bounding box computation. Otherwise you can use something like \useasboundingbox ([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]current bounding box.south west) -- (current bounding box.north east);
    – percusse
    Commented Sep 12, 2012 at 17:24
  • @percusse: Seems like an answer to me. Commented Sep 12, 2012 at 17:30
  • There is nothing standalone or preview can do here automatically, because they don't know about the actual content. Making TikZ increase the BB like @percusse stated is the way to go. Commented Sep 12, 2012 at 17:35

4 Answers 4

18

As Martin Scharrer pointed out, preview or standalone classes see the Tikz picture as black boxes with some size reported by the TikZ bounding box. For this reason, it's only up to TikZ internals to compute the correct box.

However, arrows and line join artifacts don't contribute to bounding box calculations and I don't know a quick hack to fix this. Example:

\documentclass[tikz,border=0pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=3,line width=3pt]
\draw[red] (0,0) grid (1,1);
\draw[blue,|->] (0,0) -- (1,1);
\draw[line join=miter] (1,1) -- (0,0.5) -- (1,0);
\end{tikzpicture}
\end{document}

enter image description here

You can manually change the bounding box to include the missing details for example with

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[scale=3,line width=3pt]
\draw[red] (0,0) grid (1,1);
\draw[blue,|->] (0,0) -- (1,1);
\draw[line join=miter] (1,1) -- (0,0.5) -- (1,0);
\useasboundingbox ([shift={(-0.5\pgflinewidth,-0.5\pgflinewidth)}]current 
bounding box.south west) -- (current bounding box.north east);
\end{tikzpicture}
\end{document}

enter image description here

Note that the shift amount should be changed for each custom picture, I've just used a shortcut here.

2
  • to me it looks like a bug in TikZ that the arrow ends are drawn without updating the bb
    – user2478
    Commented Sep 12, 2012 at 18:16
  • @Herbert That might certainly be true. I didn't check the code but I know it only from experimentation.
    – percusse
    Commented Sep 12, 2012 at 18:37
26
+50

do not use the standalone documentclass. Create a default pdf output and then run pdfcrop on that file. The created <file>-crop.pdf has a correct bounding box. The example needs two pdflatex -shell-escape <file> runs:

\documentclass{minimal}
\usepackage{graphicx,tikz}
\pagestyle{empty}
\begin{document}

\IfFileExists{\jobname-crop.pdf}
  {\typeout{Delete \jobname-crop.pdf}%
   \immediate\write18{rm -f \jobname-crop.pdf}}
  {\IfFileExists{\jobname.pdf}
    {\immediate\write18{pdfcrop \jobname}}
    {\typeout{we need one more pdflatex run with option -shell-escape!}}
  }
\IfFileExists{\jobname-crop.pdf}
  {\frame{\includegraphics{\jobname-crop.pdf}}}
  {%
   \begin{tikzpicture}[scale=3,line width=3pt]
   \draw[red] (0,0) grid (1,1);
   \draw[blue,|->] (0,0) -- (1,1);
   \end{tikzpicture}%
}
\end{document}

enter image description here

2
  • 1
    If you want a .png out of the end of the day, ImageMagick's convert utility will crop intelligently. Cf. 'convert -trim -bordercolor White -density 180 $1 -border 8x8 converted.png'.
    – Mohan
    Commented Dec 16, 2012 at 19:20
  • 1
    that makes no sense here, because it will also convert the image to a bitmap but the cropped pdf is still a vector graphic.
    – user2478
    Commented Dec 16, 2012 at 20:38
8

Till Tantau explains that just including arrows.meta library solves the problem. Bounding box is adjusted to included arrow tips and tails.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\begin{document}
\begin{tikzpicture}[scale=3,line width=3pt]
\draw[red] (0,0) grid (1,1);
\draw[blue,|->] (0,0) -- (1,1);
\draw[line join=miter] (1,1) -- (0,0.5) -- (1,0);
\end{tikzpicture}
\end{document}

enter image description here

3

The newest update for TikZ CVS does now support the inclusion of arrowheads into the bounding box. Thus, smart cropping of TikZ pictures with standalone is now possible.

According to the feature request, it is not yet documented but available.

1
  • easy task. Thanks to the guys, programming that stuff.
    – LaRiFaRi
    Commented Sep 19, 2013 at 8:48

You must log in to answer this question.

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