我在代码中标记了所有节点,有些节点的标签中有十进制数字。我想在命令中使用这些标签作为参数,\draw
但我一直收到此错误
! Missing \endcsname inserted.
下面是一个最小工作示例。
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
%Putting node and clipping them to their respective positions
\foreach \x in {1/2,1,10/3,37/3,15.5}
\foreach \y in {1,1.25,1.5,3,3.5,,4.5}
{
\node [circle,draw=white,fill=white,inner sep=0pt,minimum size=1.5mm] (u\x\y) at (\x,\y) {};
}
%Drawing arrows
\foreach \y in {1.5,3.5}
{
\draw[semithick,->] (u1/2\y) -- (u1\number\numexpr\y-0.5\relax) ;
}
\end{tikzpicture}
\end{document}
答案1
正如不应在 C 程序中使用 x[2.5] 一样,应避免使用带有浮点数(转换为文本)的 Tikz 名称。但可以使用 Tikz 数组(不要与 LaTeX 数组混淆)获得所需的效果。
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
%Putting node and clipping them to their respective positions
\def\xval{{1/2,1,10/3,37/3,15.5}}
\def\yval{{1,1.25,1.5,3,3.5,4,4.5}}
\foreach \x in {0,1,2,3,4}
\foreach \y in {0,1,2,3,4,5,6}
{
\node [circle,draw=white,fill=white,inner sep=0pt,minimum size=1.5mm] (u\x*\y) at (\xval[\x],\yval[\y]) {};
}
%Drawing arrows
\foreach \y in {2,4}
{
\draw[semithick,->] (u1*\y) -- (u1*\number\numexpr\y-1\relax) ;
}
\end{tikzpicture}
\end{document}