我正在尝试为孩子们准备数学练习。为此,我准备了以下最简的乌龟骑行:
\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}
得出:
我想做的是用中的数字注释水平部分\xs
。使用 turtle 时可以吗?
答案1
您可以说node[above,xshift=-4em]
,其中4em
是 的一半turtle/distance
。或者,您也可以不使用 执行相同操作turtle
。现在使用命名节点。
\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}
答案2
我的做法略有不同,我输入了方向\xs
而不是数字。然后,您可以在每条线的两端设置坐标(下面的黄色圆圈)。这些可用于标记线条(下面的绿色圆圈)。顺便说一句,您应该使用standalone
而不是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}