8

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

Extra whitespace to the left of this image

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}
Martin Scharrer
  • 262,582
simont
  • 2,740
  • 3
  • 22
  • 32
  • 4
    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 06 '13 at 04:02
  • @Jake I should have picked up on that. :( Thanks, that solves my problem. – simont Aug 06 '13 at 04:15
  • 1
    For this reason it's best to put definitions in the preamble, where line breaks are ignored. – Ryan Reich Aug 06 '13 at 05:02
  • See for example http://tex.stackexchange.com/questions/7453/what-is-the-use-of-percent-signs-at-the-end-of-lines and http://tex.stackexchange.com/questions/19922/where-are-the-necessary-places-to-be-appended-with-to-remove-unwanted-spaces?lq=1 (dupe of latter?) – Joseph Wright Aug 06 '13 at 05: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. – Martin Scharrer Aug 06 '13 at 06:49
  • For anyone who wants a quick fix for this problem without modifying the tikz code: the utility pdfcrop is very helpful. – japreiss Apr 14 '22 at 17:20
  • You can use \documentclass[varwidth]{standalone} to remove the empty space on the left. – zyy Aug 27 '23 at 05:05

1 Answers1

8

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})

As Jake has submitted in his comment.

masu
  • 6,571
  • Thanks. This also seemed to fix my issue of a mysterious white space left of the y-axis, after moving my labels to the top of the axis. – Roald Jul 28 '16 at 11:27