# How to get rid of white space when drawing lines in TikZ with documentclass=standalone?

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

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?

• 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 '18 at 8:57

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

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