5

It seems that, when using Tikz, nodes and/or the tikzpicture environment add space around a drawing. An example is shown in the following MWE, where a simple square is drawn three times. First it is drawn in just a single tikzpicture, then in a nested tikzpicture, and finally in a nested tikzpicture with the inner sep adjusted. This last image makes me suspect there are some widths that need to be set to zero.

\documentclass[tikz]{standalone}
\standaloneenv{tikzpicture} % New page for every image
% The code for the square
\newcommand\Square[1][]{
    \begin{tikzpicture}[#1]
        \draw (0,0)--(1,0)--(1,1)--(0,1)--cycle;
    \end{tikzpicture}
    }
\begin{document}
% Image with correct cropping
\Square[x=1.5cm,y=1.5cm]
% Image with a lot of space
\begin{tikzpicture}
    \node at (0,0){
        \Square[x=1.5cm,y=1.5cm]
        };
\end{tikzpicture}
% Image with some cropping
\begin{tikzpicture}
    \node[inner sep=0pt] at (0,0){
        \Square[x=1.5cm,y=1.5cm]
        };
\end{tikzpicture}
\end{document}

The three output images (or pages) are the following.

enter image description here

How does one eliminate this spacing? Nesting these environments is necessary for the specific problem I'm working on, and \includegraphics[clip]{} doesn't work in combination with \standaloneenv{tikzpicture} (which was tried just in case the standalone failed at cropping).

Betohaku
  • 1,637

1 Answers1

7

You have spurious spaces in the definition of \Square:

\newcommand\Square[1][]{% <--- a space was here
    \begin{tikzpicture}[#1]
        \draw (0,0)--(1,0)--(1,1)--(0,1)--cycle;
    \end{tikzpicture}% <--- a space was here
    }

Here's what I get after changing the definition:

enter image description here

The first of your squares came out good because TeX is in "vertical mode" and so it ignores the first space; then it does \par and so also the final one gets ignored. In the other two cases, the spaces are already inside a tikzpicture, so they aren't ignored any more.

egreg
  • 1,121,712
  • This is fantastic. Hours of attempting different cropping packages and documentclasses (standalone, preview, crop..), changing parameters of node and tikzpicture etc, and then it turns out to be a white space. Thanks! – Betohaku Apr 28 '13 at 15:35