5

Why the following document does not compile correctly? I would like an output drawing n consecutive semicircles. For n=4 an output like this:

enter image description here

Is there a way to fix my code?

\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}

\begin{document}

\begin{tikzpicture}
\def \n{4}
\foreach \i in {0,...,\n}
  {
  \tkzDefPoint(2*\i/\n,0){t_\i}
  \tkzDrawPoint(t_\i)
  }

\foreach \i in {0,...,\n/2}
{
\tkzDrawArc(t_(2*\i+1),t_(2*\i+2))(t_(2*\i))
}
\end{tikzpicture}

\end{document}

Here is a manual solution without foreach:

\begin{tikzpicture}
\tkzDefPoint(0,0){A0}
\tkzDefPoint(.25,0){B0}
\tkzDefPoint(.5,0){A1}  
\tkzDefPoint(.75,0){B1}  
\tkzDefPoint(1,0){A2}  
\tkzDefPoint(1.25,0){B2}  
\tkzDefPoint(1.5,0){A3}  
\tkzDefPoint(1.75,0){B3}  
\tkzDefPoint(2,0){A4}  
\tkzDrawSegments(A0,A4)
\tkzDrawArc(B0,A1)(A0)
\tkzDrawArc(B1,A2)(A1)
\tkzDrawArc(B2,A3)(A2)
\tkzDrawArc(B3,A4)(A3)
\end{tikzpicture}
5
  • Two problems. First is solved in tex.stackexchange.com/questions/133307/…, you need to load \usetkzobj{all}. The second is about parameters for the arc, they are names of points (t_0) and you use operations which are not converted into names. – Ignasi Dec 12 '17 at 16:25
  • @Ignasi You marked this question as duplicate. I look at the question you linked. They say to add \usetkzobj{all}. But even after adding it I get the same error. I guess there is a syntax error in my code. – Name Dec 12 '17 at 16:26
  • Yes, I was too fast. Excuse me. I've reopened it. – Ignasi Dec 12 '17 at 16:28
  • @Ignasi please do not hesitate to post an answer if you know how to fix the operations in the indices. – Name Dec 12 '17 at 16:29
  • I am trying to write an answer... Can you give a manual representation first without the foreach of what exactly you want to do? Because the logic I think in the foreach is missing something. After all, you are defining points from 0 to 4, but in the \foreach, you would need points 5 and 6 as well – Alex Recuenco Dec 12 '17 at 16:43
5

There are a couple things that were wrong.

  1. The error that it gives you is that the command tkzDrawArc is not defined. This usually means that there is some library that hasn't been loaded. Indeed, you were missing writing \usetkzobj{all}, that loads all the commands form the packages.

  2. When you write \n/2, it is not doing what you think.

    The \foreach command can be written using the /, which has a special meaning...

    For our intents and purposes with regards to this question, we will just say that you can't do math operations in the foreach command itself, you have to do the math before you place the result on the foreach structure.

    Therefore, we will do them outside of the \foreach with a \pgfmathtruncatemacro command and later we will use that macro inside our \foreach.

    \pgfmathtruncatemacro\nhalf{\n/2}
    ...
    \foreach {1, ..., \nhalf}
    
  3. Parenthesis don't collapse down to a name doing math. Hence, when you write t_(2*\i+1), it collapses to t_(2*0+1), which is not a name you have defined (You have defined t_1. Each character is taken as part of the name).

    Which means that we have to use the same trick again. We calculate the value separately, and then we use it.

    \pgfmathtruncatemacro{\first}{(2*\i)}
    \pgfmathtruncatemacro{\second}{(2*\i+1)}
    \pgfmathtruncatemacro{\last}{2*\i-1}
    \tkzDrawArc(t_\first,t_\second)(t_\last)
    
  4. There is a logical error: In your example, The last point that is needed to follow your logic would be t_6, and you have only defined up to t_4 in the first \foreach. In the spirit of modifying only minimally your initial intent, I will only add points up to t_6.

With all that in mind, this is what you get to remove those errors (I took the liberty to add a \tkzDrawLines command and remove the \tkzDrawPoint to resemble better the picture that you post as an example):

\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}

\begin{tikzpicture}
    \pgfmathtruncatemacro\n{6}
    \pgfmathtruncatemacro\nhalf{\n/2-1}
    \foreach \i in {0,...,\n}{
      \tkzDefPoint(2*\i/\n,0){t_\i}
      %\tkzDrawPoint(t_\i) % Commented out, since in your drawing there are lines, not points drawn
    };
    \tkzDrawLines(t_0,t_\n) % Drawing the line like you show in your picture


    \foreach \i in {0,...,\nhalf}
    {
    \pgfmathtruncatemacro{\first}{(2*\i+1)}
    \pgfmathtruncatemacro{\second}{(2*\i+2)}
    \pgfmathtruncatemacro{\last}{2*\i}
    \tkzDrawArc(t_\first,t_\second)(t_\last)

    }
\end{tikzpicture}

\end{document}

Solution

5
  • Notice how \n is the number of points, the number of arcs that you are drawing in this way is \n/2 – Alex Recuenco Dec 12 '17 at 17:19
  • Thanks for this very detailed answer. I will check it. I also provided a manual solution without foreach in my original question. – Name Dec 12 '17 at 20:46
  • 1
    I confirm that your solution works perfect. The only change to obtain the exact picture is to change tkzDrawLines with \tkzDrawSegments. Thank you again. – Name Dec 13 '17 at 5:55
  • 1
    In the next version, it's possible to draw directly a semi-circle – Alain Matthes Jan 1 '18 at 21:28
  • Alain. Do you mind giving which version number allows drawing a semi-circle? In case someone finds this on the future, where "next version" might not have the same meaning anymore – Alex Recuenco Jan 2 '18 at 9:43
4

Alex Recuenco already explained all errors that your code contains regarding use of tkz-euclide and Artificial Stupidity provided a pstricks solution, therefore I just have to provide a pure TikZ solution:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \i in {0,2,4,6}
\draw (\i,0) arc[start angle=180, end angle=0, radius=1cm];
\draw (0,0) -- (8,0);
\end{tikzpicture}

\end{document}

enter image description here

3

If you just want to get the same output regardless of the implementation details, the following is a PSTricks solution.

\documentclass[pstricks,border=12pt]{standalone}
\def\N{4}
\begin{document}
\begin{pspicture}(\N,.5)
    \multips(0,0)(1,0){\N}{\pscustom{\psarc(.5,0){.5}{0}{180}\closepath}}
\end{pspicture}
\end{document}

enter image description here

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.