Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
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? –  Who is crazy first 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

3 Answers 3

up vote 16 down vote accepted

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.

share|improve this answer
    
to me it looks like a bug in TikZ that the arrow ends are drawn without updating the bb –  Herbert 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

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

share|improve this answer
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. –  Herbert Dec 16 '12 at 20:38

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.

share|improve this answer
1  
Thanks submitting the feature request! –  AlexG Sep 19 '13 at 8:37
    
easy task. Thanks to the guys, programming that stuff. –  LaRiFaRi Sep 19 '13 at 8:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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