16

We can notice in the circle by zooming a little bit that the path of the triangle exceeds the path of the square. How to correct this ?

  \documentclass[border=10pt]{standalone}
  \usepackage{tikz}
  \usetikzlibrary{calc}
  \begin{document}
  \begin{tikzpicture}[x=0.8cm,y=0.8cm, thick,line join=miter]
  \coordinate [label=below left : $D$] (D) at (0,0);
  \coordinate [label=below right : $C$](C) at (5,0);
  \coordinate [label=above right : $B$](B) at (5,5);
  \coordinate [label=above left : $A$](A) at (0,5);
  \coordinate [label=below : $E$](E) at (3,5);
  \coordinate [label=left : $F$](F) at (5,3);

  \filldraw[fill=green,draw=green!10!black] (D)--(E)--(F)--cycle;
  \draw  (D)--(C)--(B)--(A)--cycle;
  \draw[red] (0,0) circle (0.5 cm);
  \node [below] at (2.5,0){1};
  \node [left] at (0,2.5){1};
  \end{tikzpicture}
  \end{document}

enter image description here

15

You could use line join=bevel.

\documentclass[border=10pt]{standalone}
  \usepackage{tikz}
  \usetikzlibrary{calc}
  \begin{document}
  \begin{tikzpicture}{x=0.8cm,y=0.8cm, thick}
  \coordinate [label=below left : $D$] (D) at (0,0);
  \coordinate [label=below right : $C$](C) at (5,0);
  \coordinate [label=above right : $B$](B) at (5,5);
  \coordinate [label=above left : $A$](A) at (0,5);
  \coordinate [label=below : $E$](E) at (3,5);
  \coordinate [label=left : $F$](F) at (5,3);

  \filldraw[fill=green,draw=green!10!black,line join=bevel] (D)--(E)--(F)--cycle;
  \draw  (D)--(C)--(B)--(A)--cycle;
  \draw[red] (0,0) circle (0.5 cm);
  \node [below] at (2.5,0){1};
  \node [left] at (0,2.5){1};
  \end{tikzpicture}
  \end{document}

enter image description here

21

First, you didn't zoom in far enough. The same problem appears at points E and F. And second, the options to your tikzpicture are not used, because they are given in braces ({}), but it should be brackets ([]).

In TikZ (and any vector graphic software I know) lines extend by half the line width to both sides of the path (which has zero width). With the default line join miter, line tips are extended until the outer borders meet (see point D in the picture below), which can be really annoying sometimes. This can be solved with the answers given by erik, Ulrike Fischer and marmot. Additionally there is line join = round (see point A).

With clipping, stuff is cut in the middle of the line from \draw[clip]. If the clipped stuff is drawn in the same or a similar color, this is not an issue. But with different colors, this can lead to unwanted artifacts, i.e. the line from \draw[clip] is half overdrawn with other lines (see point B). Of course the other solutions also have artifacts, but not that severe.

The option miter limit has the same result as line join = bevel, if the factor is set correctly. It switches to bevel, if the distance of the tip of the path to the tip of the line is greater then the factor times half the line width. With a factor of 1, the line join will always be set to bevel. For greater values, there is a maximum angle up to which miter is used. E.g. for a factor of sqrt(2)miterwill be used for angles of 90° or larger andbevel for smaller angles. There is an example in the code, where the factor is set to slightly below or above sqrt(2) for the outer square.

Edit:


To make the changes work for the whole picture, line join or miter limit can be given as an option to the tikzpicture and for the square the same option needs to be set, to get the default appearence:

Ulrikes answer:

\begin{tikzpicture}[...,miter limit=1,...]
...
\draw[...,miter limit=10,...]  (D)--(C)--(B)--(A)--cycle;

eriks answer:

\begin{tikzpicture}[...,line join= bevel,...]
...
\draw[...,line join=miter,...]  (D)--(C)--(B)--(A)--cycle;

It's the same for line join=round.

marmots answer doesnt require an option to the tikspicture, so it's just:

\draw[clip]  (D)--(C)--(B)--(A)--cycle;

Note: for \draw[clip] TikZ doen't allow additional options. So, for example, the line width must be set up before this command, e.g. given as an option to tikspicture.


In the picture below, the thin lines show the path, while I made the lines really thick to better show, how things work.

enter image description here

The code:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[thick,line join=miter]
  \coordinate [label=below left : $D$] (D) at (0,0);
  \coordinate [label=below right : $C$](C) at (5,0);
  \coordinate [label=above right : $B$](B) at (5,5);
  \coordinate [label=above left : $A$](A) at (0,5);
  \coordinate [label=above : $E$](E) at (2.5,5);
  \coordinate [label=right : $F$](F) at (5,2.5);
  \coordinate [label=below : $G$](G) at (2.5,0);
  \coordinate [label=left : $H$](H) at (0,2.5);

  % 90° -> tip(path) to tip(line) = 0.5 * linewidth * sqrt(2)
  % miter limit > sqrt(2) -> line join: miter
  \draw[line width=2mm, miter limit=1.4143]  (D)--(C)--(B)--(A)--cycle;
  % miter limit < sqrt(2) -> line join: bevel
  %\draw[line width=2mm, miter limit=1.4142]  (D)--(C)--(B)--(A)--cycle;

  \filldraw[line width=2mm,fill=green!50,draw=green] (D)--(E)--(F)--cycle;
  \draw[very thin] (D)--(E)--(F)--cycle;

  \begin{scope}
    \clip (5,0) circle (1cm);
    \filldraw[line width=2mm,fill=green!50,draw=green, line join=bevel] (C)--(H)--(E)--cycle;
    \draw[very thin] (C)--(H)--(E)--cycle;
  \end{scope}

  \begin{scope}
    \clip (0,5) circle (1cm);
    \filldraw[line width=2mm,fill=green!50,draw=green, line join=round] (A)--(F)--(G)--cycle;
    \draw[very thin] (A)--(F)--(G)--cycle;
  \end{scope}

  \begin{scope}
    \clip (B) -- (5,4) arc (270:180:1cm) -- cycle;
    \filldraw[line width=2mm,fill=green!50,draw=green] (B)--(G)--(H)--cycle;
    \draw[very thin] (B)--(G)--(H)--cycle;
  \end{scope}

  \draw[red, very thin]  (D)--(C)--(B)--(A)--cycle;

  \node[yshift= 0.5cm, anchor=south] at (A) {\texttt{line join = round}};
  \node[yshift= 0.5cm, anchor=south,align=center] at (B) {\textsl{clipped}\\(marmots answer)};
  \node[yshift=-0.5cm, anchor=north,align=center] at (C) {\texttt{line join = bevel}\\(eriks answer)\\\texttt{miter limit = 1}\\Ulrikes answer)};
  \node[yshift=-0.5cm, anchor=north,align=center] at (D) {\texttt{line join = miter}};

\end{tikzpicture}
\end{document}
  • @ Mike "because they are given in braces ({}), but it should be brackets ([])." Oops ! Thank you for all these explanations. – Fabrice May 5 '18 at 7:24
  • @ Mike "First, you didn't zoom in far enough" I added as an image the code output. When I talked about zooming, I meant to enlarge the image with Adobe reader for example. In fact, I do not know how to do like erik or ulrike: what code to have an exit like theirs ? – Fabrice May 5 '18 at 7:48
  • @Fabrice I meant zooming in with a PDF viewer too. And the others probably did the same, made a screen shot and croped it (that's how I would do it). But if you ever want to show a zoomed in part of a picture within that picture, the spy library is there for you. – Mike May 5 '18 at 13:16
  • @marmot see edit – Mike May 5 '18 at 20:17
13

You could set miter limit to a smaller value, e.g. miter limit=1:

\documentclass[border=10pt]{standalone}
  \usepackage{tikz}
  \usetikzlibrary{calc}
  \begin{document}
  \begin{tikzpicture}{x=0.8cm,y=0.8cm, thick,line join=miter}
  \coordinate [label=below left : $D$] (D) at (0,0);
  \coordinate [label=below right : $C$](C) at (5,0);
  \coordinate [label=above right : $B$](B) at (5,5);
  \coordinate [label=above left : $A$](A) at (0,5);
  \coordinate [label=below : $E$](E) at (3,5);
  \coordinate [label=left : $F$](F) at (5,3);

  \filldraw[fill=green,draw=green!10!black,miter limit=1] (D)--(E)--(F)--cycle;
  \draw  (D)--(C)--(B)--(A)--cycle;
  \draw[red] (0,0) circle (0.5 cm);
  \node [below] at (2.5,0){1};
  \node [left] at (0,2.5){1};
  \end{tikzpicture}
  \end{document}

enter image description here

10

Really just for fun and completeness: Why not use clip?

  \documentclass[border=10pt]{standalone}
  \usepackage{tikz}
  \usetikzlibrary{calc} %<- are you using calc?
  \begin{document}
  \begin{tikzpicture}{x=0.8cm,y=0.8cm, thick,line join=miter}
  \coordinate [label=below left : $D$] (D) at (0,0);
  \coordinate [label=below right : $C$](C) at (5,0);
  \coordinate [label=above right : $B$](B) at (5,5);
  \coordinate [label=above left : $A$](A) at (0,5);
  \coordinate [label=below : $E$](E) at (3,5);
  \coordinate [label=left : $F$](F) at (5,3);
  \begin{scope}
  \draw[clip]  (D)--(C)--(B)--(A)--cycle;
  \filldraw[fill=green,draw=green!10!black] (D)--(E)--(F)--cycle;
  \end{scope}
  \draw[red] (0,0) circle (0.5 cm);
  \node [below] at (2.5,0){1};
  \node [left] at (0,2.5){1};
  \end{tikzpicture}
  \end{document}

enter image description here

  • Probably \clip[preaction=draw] ... is better to not have only half of the line width displayed. It is strange that on your image we can't see the line thickness difference. – Kpym May 5 '18 at 15:25
  • @KpymThanks! I do not understand your second point. I think that the clip is active after the draw. If I would first clip, and then draw, of course only 50% of the line would be visible. – marmot May 5 '18 at 15:34
  • @Kpym \clip[preaction=draw] does the same as \draw[clip]: draw first, then clip. \clip[postaction=draw] clips before drawing, and thus cuts the line in half. – Mike May 6 '18 at 0:55
  • @Mika @marmot This is really strange : I have TikZ version 2015/08/07 v3.0.1a (rcs-revision 1.151) and when I compile this MWE I see only the half of the line. And on my installation \draw[clip] clip and then draw ! – Kpym May 6 '18 at 5:56
  • @Mika @marmot I came to learn something : if you do \draw[clip] you ask for both "draw and clip" and the pdf reader should (but this is not the case for Sumatra on Windows) first draw and then clip. And we can reed in the manual First, a path is filled, then drawn, and then clipped (although it took Apple two mayor revisions of their operating system to get this right...). So it is safer to force the order by doing \clip[preaction=draw] :( – Kpym May 6 '18 at 6:26

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.