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?

  • 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 Sep 12 '12 at 17:18
  • @percusse: Should we update the bounding box manually? Is it cumbersome for us? – kiss my armpit Sep 12 '12 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 Sep 12 '12 at 17:24
  • @percusse: Seems like an answer to me. – Peter Grill Sep 12 '12 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. – Martin Scharrer Sep 12 '12 at 17:35

4 Answers4

26

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

  • 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 Dec 16 '12 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. –  Dec 16 '12 at 20:38
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.

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

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

Ignasi
  • 136,588
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.

LaRiFaRi
  • 43,807