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}
6
  • 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 6, 2013 at 4:02
  • @Jake I should have picked up on that. :( Thanks, that solves my problem.
    – simont
    Aug 6, 2013 at 4:15
  • 1
    For this reason it's best to put definitions in the preamble, where line breaks are ignored.
    – Ryan Reich
    Aug 6, 2013 at 5:02
  • See for example tex.stackexchange.com/questions/7453/… and tex.stackexchange.com/questions/19922/… (dupe of latter?)
    – Joseph Wright
    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

1 Answer 1

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.

1
  • 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, 2016 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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