What's causing extra white space to the left of my standalone TikZ image?

I have extra whitespace to the left of a TikZ image produced in a standalone environment:

I don't know why this is occurring. I thought perhaps the left projector bounding box, but the right projector is a mirror of the first using \begin{scope}[xscale=-1], and it doesn't produce the same whitespace to the right.

I can probably crop this fairly easily, but I'd like to know why it's happening to prevent it in the future.

Comments on my code are welcome; I'm new to TikZ. My working example:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}

\pgfmathsetmacro{\cubex}{1}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{1}

\def\projector[#1:#2:#3] {
% Angle and place the projector
\begin{scope}[rotate around={#1:(#2,#3)}]
% Projector and lens
\draw [red] (#2, #3) rectangle (#2 + 1, #3 + .5);
\draw [red] (#2 + 1, #3 + .175) rectangle (#2 + 1.25, #3 + .325);
% Field-Of-View annotation
\draw ( #2 + 1.25, #3 + .325 ) -- ( #2 + 4, #3 + 2);
\draw ( #2 + 1.25, #3 + .175 ) -- ( #2 + 4, #3 - 1);
\end{scope} }

\begin{tikzpicture}
% Draw a cube.
\draw[fill=blue] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,  0) -- cycle;
\draw[fill=green] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,      \cubez) -- cycle;
\draw[fill=red] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,   0) -- cycle;
% Draw a projector.
\projector[-40:-4.5:2]
% Second projector is mirrored
\begin{scope}[xscale=-1]
\projector[-40:-3.5:2]
\end{scope}

\end{tikzpicture}

\end{document}

• The white space is caused by line breaks. If you add % after each of the three \pgfmathsetmacro statements and at the end of the \end{scope} } line, the space disappears (or you could move those things before \begin{document}).
– Jake
Aug 6, 2013 at 4:02
• @Jake I should have picked up on that. :( Thanks, that solves my problem. Aug 6, 2013 at 4:15
• For this reason it's best to put definitions in the preamble, where line breaks are ignored. Aug 6, 2013 at 5:02
• See for example tex.stackexchange.com/questions/7453/… and tex.stackexchange.com/questions/19922/… (dupe of latter?) Aug 6, 2013 at 5:54
• @Jake: Could you post this as an answer. It should be useful for others using standalone, even if it's a general TeX thing. Aug 6, 2013 at 6:49

The white space is caused by line breaks. If you add % after each of the three \pgfmathsetmacro statements and at the end of the \end{scope} } line, the space disappears (or you could move those things before \begin{document})