![在 tikz 部分计算中使用预定义常量时遇到问题](https://linux22.com/image/370145/%E5%9C%A8%20tikz%20%E9%83%A8%E5%88%86%E8%AE%A1%E7%AE%97%E4%B8%AD%E4%BD%BF%E7%94%A8%E9%A2%84%E5%AE%9A%E4%B9%89%E5%B8%B8%E9%87%8F%E6%97%B6%E9%81%87%E5%88%B0%E9%97%AE%E9%A2%98.png)
我正在尝试绘制一个图表,其中的节点都是使用部分计算确定的。我做了一个定义,用来\def\x{.35}
存储部分距离(因为我想在一系列计算中使用相同的距离)。(特别是,我想绘制一些在线边上等距但有偏移的节点。)
当我尝试在计算中使用 \x 值(下面注释掉的行)时,我也使用 let 语句(以便我可以进行模块化运算),它给了我错误Argument of \x has an extra }. Inserted text \par
。
如果我在 \path 命令中将 \x 替换为其值 .35,则所有内容都会编译。下面的 MWE 中注释掉了有问题的行。
(我尝试将 \x 括在括号中;没有用。我尝试将它放在 let 语句中,但let \n1 = ..., \n2 = {\x} in ...
也没有用。)
与以前的命令相比,TikZ 不喜欢这个命令的什么地方?
一个最小的工作示例:
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\def\z{.1}
\def\x{.35}
\begin{tikzpicture}[vtx/.style={draw, circle, gray, font=\tiny, inner sep = 1 pt}, flag/.style={draw, circle, font=\tiny, inner sep = 1 pt}, scale = 3]
\node[vtx] (v0) at (0,0){$0$};
\foreach \i in {0, ..., 5}{
\node[vtx] (v1 \i) at (360*\i/6:1){$1_{\i}$};
\path let \n1 = {int(mod(\i+1, 6))} in node[vtx] (v4 \i) at ($(360*\n1/6:1)+(360*\n1/6-30:1)$){$4_{\i}$};
}
\foreach \i in {0, ..., 5}{
\node[flag] (0 \i) at ($(v0)!\x!(v1 \i)!\z!90:(v1 \i)$){};
\node[flag, fill=red] (b \i) at ($(v1 \i)!\x!(v4 \i)!\z!90:(v4 \i)$){};
%\path let \n1 = {int(mod(\i+1, 6))} in node[flag] (8 \i) at ($(v4 \i)!\x!({v1 \n1})!\z!90:({v1 \n1})$){};
}
\end{tikzpicture}
\end{document}
答案1
我已经更改了一些选项calc
命令evaluate
,并在最后一个循环中更改为\n1
,\ni
并且它起作用了(没有错误,但我不知道这是否是期望的结果):
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\def\z{.1}
\def\x{.35}
\begin{tikzpicture}[vtx/.style={draw, circle, gray, font=\tiny, inner sep = 1 pt}, flag/.style={draw, circle, font=\tiny, inner sep = 1 pt}, scale = 3]
\node[vtx] (v0) at (0,0){$0$};
%\foreach \i in {0, ..., 5}{
% \node[vtx] (v1 \i) at (360*\i/6:1){$1_{\i}$};
% \path let \n1 = {int(mod(\i+1, 6))} in node[vtx] (v4 \i) at ($(360*\n1/6:1)+(360*\n1/6-30:1)$){$4_{\i}$};
%}
\foreach \i [evaluate=\i as \angle using 60*\i
] in {0, ..., 5}{
\node[vtx] (v1 \i) at (\angle:1){$1_{\i}$};
\node[vtx] (v4 \i) at ($(\angle+60:1)+(\angle+30:1)$){$4_{\i}$};
}
\foreach \i [evaluate=\i as \ni using {int(mod(\i+1,6))}] in {0,...,5}{
\node[flag] (0 \i) at ($(v0)!\x!(v1 \i)!\z!90:(v1 \i)$){};
\node[flag, fill=red] (b \i) at ($(v1 \i)!\x!(v4 \i)!\z!90:(v4 \i)$){};
\node[flag] (8 \i) at ($(v4 \i)!\x!(v1 \ni)!\z!90:(v1 \ni)$){};
}
\end{tikzpicture}
\end{document}