3

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:

left and right are white spaces

  • 2
    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
0

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

The lines normally are extended a bit to take into account the "caps" (see why in
Schrödinger's cat's answer here).

You can see what's happening if you use:

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

and you'll obtain:

enter image description here

The manual explains this:

enter image description here

You can use rect, but the output is funny:

enter image description here

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}

enter image description here

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

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.