当我定义 \pluse 宏并在 foreach 循环中使用它时,报告错误:代码:
\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\def\r{2}
\def\plus{+(0.1,0) -- +(0.2,0.1) -- +(0.3,-0.1) -- +(0.4,0)}
\coordinate (O) at (0,0);
\draw[red,semithick,dashed,name path=arcafter]
(O) ++(\r,0) arc(0:-180:\r);
\foreach \y in {0,...,4}
{
\draw[semithick,->,>=latex,name path=arrow]
(-\r,-\y*\r/4) -- (\r,-\y*\r/4);
\draw[name intersections={
of=arcafter and arrow,
name=M,
total=\t}]
foreach \i in {1,...,\t}
{
(M-\i) ++(-0.2,0) \pluse
};
}
\end{tikzpicture}
\end{document}
错误信息:
! Undefined control sequence.
\@next ... \tikz@scan@next@command \pgf@let@token
l.23 }
?
如果用实际代码替换 \pluse,它就可以正常工作。不确定我的宏是否有问题!
答案1
您的代码很好。只有一个拼写错误。您定义了\plus
但使用了\pluse
。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\def\r{2}
\def\pluse{+(0.1,0) -- +(0.2,0.1) -- +(0.3,-0.1) -- +(0.4,0)}
\coordinate (O) at (0,0);
\draw[red,semithick,dashed,name path=arcafter]
(O) ++(\r,0) arc(0:-180:\r);
\foreach \y in {0,...,4}
{
\draw[semithick,->,>=latex,name path=arrow]
(-\r,-\y*\r/4) -- (\r,-\y*\r/4);
\draw[name intersections={
of=arcafter and arrow,
name=M,
total=\t}]
foreach \i in {1,...,\t}
{
(M-\i) ++(-0.2,0) \pluse
};
}
\end{tikzpicture}
\end{document}