6

I'm trying to prepare math exercises for kids. To that end, I have the following minimal turtle ride:

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{turtle}

\def\xs{1,2,3}

\begin{document}

\begin{tikzpicture}[turtle/distance=8em]
  \draw [thick][turtle={home,right}]
  \foreach \i in \xs
       {
           [turtle={forward,left}] node[above]{\i}
           [turtle={forward,right}]
       };
\end{tikzpicture}
\end{document}

This yields:

enter image description here

What I'm trying to do is to annotate the horizontal pieces with the numbers in \xs. Is it possible when using turtle?

Dror
  • 22,613

2 Answers2

7

You could say node[above,xshift=-4em], where 4em is half the turtle/distance. Or you could do the same without turtle. Now with named nodes.

enter image description here

\documentclass[border=5mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{turtle}

\def\xs{1,2,3}

\begin{document}

\begin{tikzpicture}[turtle/distance=8em]
  \draw [thick,turtle={home,right}]
  \foreach \i in \xs
       {
           [turtle={forward,left}] node[above,xshift=-4em] (n\i) {\i}
           [turtle={forward,right}]
       };
  \draw [red,-stealth] (n1) to[out=90,in=135] (n2);
  \draw [red,-stealth] (n2) to[out=90,in=135] (n3);
\end{tikzpicture}

\begin{tikzpicture}[x=8em,y=8em]
  \draw [thick] (0,0)
  \foreach \i in \xs
       {
           -|  node[above,pos=0.25] (n\i) {\i}  (\i,\i)
       };


  \draw [red,-stealth] (n1) to[out=90,in=135] (n2);
  \draw [red,-stealth] (n2) to[out=90,in=135] (n3);
\end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688
  • For the second version, how can I record the node (as coordinates) and use them outside of the loop? – Dror May 09 '17 at 20:31
  • @Dror Same as any other node I suppose, node[above,pos=0.25] (n\i) {\i}, and then you can use n1, n2 and n3. Would be the same in the turtle version. – Torbjørn T. May 09 '17 at 20:32
  • @Dror See updated answer for complete example. – Torbjørn T. May 09 '17 at 20:37
  • After drawing the steps, how can I refer to their corners? – Dror May 13 '17 at 20:27
  • 1
    @Dror For the non-turtle version? Modify the line in the loop to be -| node[above,pos=0.25] (n\i) {\i} coordinate[midway] (crnr\i) (\i,\i), and use e.g. (crnr1). In a -| path, pos=0.5 (which is the same as midway) is always at the corner of the path. If you want named coordinates at the (\i,\i) corners as well, use e.g. -| node[above,pos=0.25] (n\i) {\i} coordinate[midway] (crnrA-\i) (\i,\i) coordinate (crnrB\i) – Torbjørn T. May 13 '17 at 20:43
6

I did it a bit differently and put the directions in \xs instead of just numbers. Then you can set coordinates at each end of a line (yellow circles below). These can be used to label the lines (green circles below). BTW, you should use standalone instead of minimal.

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{turtle,calc}
\begin{document}

\def\xs{right,left,right,left,right,left,right,left,left,,left,right,left,right,left}
\begin{tikzpicture}[turtle/distance=8em]
  \draw [thick][turtle={home}]coordinate(pos0)
  \foreach \dir [count=\ind] in \xs {
    [turtle={\dir,forward}]coordinate(pos\ind)
  };
  %% yellow circles; number the end points
  \node[draw,circle,fill=yellow] at (pos0){0};
  \foreach \dir [count=\ind] in \xs {
    \node[draw,circle,fill=yellow] at (pos\ind){\ind};
  }
  %% green circles; number the paths
  \foreach \dir [count=\ind,evaluate=\ind as \indminus using int(\ind-1)] in \xs {
    \node[draw,circle,fill=green] at ($(pos\indminus)!0.5!(pos\ind)$){\indminus};
  }
\end{tikzpicture}
\end{document}

enter image description here

StefanH
  • 13,823