Standalone graphic has still borders left and right

I am working with small graphics which must not have any border. Surprisingly for me this code produces small (1-2mm) borders left and right. Could you enlighten me?

This code...

\documentclass[tikz=true,border=0pt]{standalone}
\begin{document}%
\begin{tikzpicture}[x=1mm, y=1mm]%
\draw [line width=5mm, color=green] (0,0) -- +(5,0) {};
\draw [line width=5mm, color=red] (5,0) -- +(5,0) {};
\draw [line width=5mm, color=green] (10,0) -- +(5,0) {};
\draw [line width=5mm, color=red] (15,0) -- +(5,0) {};
\end{tikzpicture}%
\end{document}


..produces this output:

• I don't have an answer as to why you obtain the borders left and right. However, if you replace your draw commands by \draw [fill = green, color = green] (0, 0) rectangle +(5, 5); and giving them the right color and the right starting point, you get rid of the side borders. – KersouMan Oct 23 '19 at 7:08
• Thanks @KersouMan. That works for me! Could you move your comment as an answer? I would accept that. – texpiet Oct 23 '19 at 7:28
• What you are seeing is the line caps. See my answer... ;-) – Rmano Oct 23 '19 at 11:08

As asked by the OP, here is a fix to get rid of the border lines that however does not tell why they were there in the first place:

\documentclass[tikz=true,border=0pt]{standalone}
\begin{document}%
\begin{tikzpicture}[x=1mm, y=1mm]%
\draw [fill=green, color=green] (0,0) rectangle +(5,5);
\draw [fill=red, color=red] (5,0) rectangle +(5,5);
\draw [fill=green, color=green] (10,0) rectangle +(5,5);
\draw [fill=red, color=red] (15,0) rectangle +(5,5);
\end{tikzpicture}%
\end{document}

• Just use the command \fill: \fill[green] (0,0) rectangle +(5,5); – AndréC Oct 23 '19 at 8:56
• You forget, that OP use linewidth=5mm. Using it your workaround gives strange result ... – Zarko Oct 23 '19 at 11:03
• @Zarko yes --- the "problem" here is line caps behavior. – Rmano Oct 23 '19 at 11:09

The lines normally are extended a bit to take into account the "caps" (see why in

You can see what's happening if you use:

\begin{tikzpicture}[x=1mm, y=1mm, line cap=round]%


and you'll obtain:

The manual explains this:

You can use rect, but the output is funny:

add transparency to see what's happening):

\documentclass[tikz=true,border=0pt]{standalone}
\begin{document}%
\begin{tikzpicture}[x=1mm, y=1mm, line cap=rect]%
\draw [line width=5mm, color=green, opacity=0.4] (0,0) -- +(5,0) {};
\draw [line width=5mm, color=red, opacity=0.4] (5,0) -- +(5,0) {};
\draw [line width=5mm, color=blue, opacity=0.4] (10,0) -- +(5,0) {};
\draw [line width=5mm, color=yellow, opacity=0.4] (15,0) -- +(5,0) {};
\end{tikzpicture}%
\end{document}


• That is correct answer! +1 – Zarko Oct 23 '19 at 11:24