I get an annoying white space in my rendered graphics with lines that begin or end at the page's borders. I am using the "standalone" document class for actually not having those margins.

Let us assume the following code for my TikZ graphic.

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{color}

\begin{document}

    \begin{tikzpicture}
        %   simple gray box
        \fill[darkgray] (0, 0) rectangle (2, 2);
        %   green line beginning from the left
        \draw[green, line width=0.5cm] (0, 1) -- (1, 1);
    \end{tikzpicture}

\end{document}

What I get with the above TeX code is this image (zoomed in for better seeing what I mean):

unwanted white space on the left

By dragging that image around with your mouse you sure will mention that there is some white space between the image's left border and the gray box (where the image should actually begin).

And yes, I know that this white space would totally make sense if I had drawn the line like this:

\draw[green, line width=0.5cm] (0, 1.5) -- (0, 1) -- (1, 1);

…But I didn't.

In my case I only need to draw horizontal lines from left to right and not along the border. I also use dark boxes in the background like the gray one in the example above. So, that white space is really bothering me.

How can I get rid of that?

  • 1
    That's a line cap vs. bounding box computation issue which is discussed in tex.stackexchange.com/questions/130456/… – percusse Apr 2 '15 at 14:07
  • I already thought of something like that, but I didn't exactly know what to search for. I also tried the solution that has been made with the "Update 2013-08-29" and it worked fine. So, thanks for your comment! But I decided to clip the image instead as suggested in the first answer. Although that might be "probably dirty" it fits for me and so I'm fine with it. – Arvid Apr 2 '15 at 14:39
  • The white line is due to the empty line you have between begin{document} and `begin{tikzpicture}'. Delete that empty line and the problem goes away, see: tex.stackexchange.com/questions/437887/… – Johan Jun 26 at 8:57
up vote 1 down vote accepted

A (probably dirty) solution is to clip the picture along the borders you want it to fit, in this case those of the black rectangle.

\documentclass[tikz]{standalone}

\usepackage{tikz}
\usepackage{color}

\begin{document}

    \begin{tikzpicture}
        \clip (0, 0) rectangle (2, 2);
        \fill[darkgray] (0, 0) rectangle (2, 2); 
        \draw[green, line width=0.5cm] (0, 1) -- (1, 1);
    \end{tikzpicture}

\end{document}

enter image description here

  • Clipping is a superb idea, beacause it's such a simple solution. And it works great. Thanks! – Arvid Apr 2 '15 at 14:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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