0

I'm still new to LaTeX and I can't wrap my head around the foreach loop.

I want to draw a grid and some semi-circles on some chosen points.

Here's my minimal Non working example :

\documentclass[10pt, a4paper]{article}
\usepackage{tkz-euclide}

\begin{document}
\begin{tikzpicture}
         \draw[very thick] (0,0) grid (10,8);
         \foreach \i in { (1,0.5), (5,0.5)} { \tkzDrawSemiCircle(\i, \i+(0,-0.5)) };    
\end{tikzpicture}
\end{document}

As I understand it, \i+(0,-0.5)) means the point situated 0.5 units below the point defined by \i at the moment. But when I try to run it, I get "Package pgf error : No shape named '(1,0.5)+(0,-0.5' is known. I suppose my syntax must be at fault but I can't find my mistake.

I read tkz-euclide documentation and I wonder if I don't have to have already defined and named the points I want to use in \tkzDrawSemiCircle. In that case, how can I do ti using \foreach ?

Any idea where I went wrong ? Thanks in advance !

0

1 Answer 1

3
  1. Coordinate calculations like (1, 2) + (3, 4) only work with the calc library and its $ syntax.

  2. The \tkzDrawSemiCircle (like all of the \tkz… commands) expect named coordinates in its arguments.

    You define them beforehand with \tkzDefPoint or \tkzDefPoints.

  3. But \tkzDefPoint won't accept coordinate calculations at all.

  4. TeX is greedy and doesn't match parentheses.

In this case, the easiest approach seems to be to define the coordinates with plain TikZ and use \tkzDrawSemiCircle with them then.

Since you use \i with () you will need to use

\expandafter\tkzDefPoint\i{origin}
% or
\path \i coordinate (origin);
% or
\coordinate[at=\i](origin);

since

\coordinate (origin) at \i;

won't work.

The calc library doesn't like it either if one of the coordinates including the () is in a macro so ($\i+(0,-0.5)$) doesn't work either but the argumentative

\coordinate (through) at ([shift=\i]0,-0.5);

does. The shift transformation is the same as addition.

Then you can draw the semi circle with

\tkzDrawSemiCircle(origin,through)

Code

\documentclass[tikz]{standalone}
%\documentclass[10pt, a4paper]{article}
\usepackage{tkz-euclide}
\begin{document}
\begin{tikzpicture}
\draw[help lines] (0,0) grid (10,8);
\foreach \i in {(1,0.5), (5,0.5)}{
%  \expandafter\tkzDefPoint\i{origin}
%  \path \i coordinate (origin);
  \coordinate[at=\i](origin);
  \coordinate (through) at ([shift=\i] 0,-0.5);
  \tkzDrawSemiCircle(origin,through)
}
\end{tikzpicture}
\end{document}

Output

enter image description here

1
  • Thanks a lot it works ! If I understand correctly, \coordinate[at=\i](origin); defines a point named "origin" at the coordinates specified by \i and then it changes the coordinates but not the name at the next loop. That's quite clever !
    – Kawahiem
    Commented Apr 10, 2023 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .