不知何故,此代码不起作用
\begin{tikzpicture}
\newcommand*{\xMax}{6}
\newcommand*{\yMax}{6}
\tikzmath{
\k = 0;
for \i in {0,0.5,...,\xMax} {
if \i == \xMax then {
\draw [very thin,gray] (\i,0) -- (\i,\yMax);
} else {
\draw [very thin,gray] (\i,0) -- (\i,\yMax) node[below=0.0cm] at ([xshift=0.275cm]\i,0) {$X_{\k}$};
}
\k = \k + 1;
}
for \i in {0,0.5,...,\yMax} {
if \i == \yMax then {
\draw [very thin,gray] (0,\i) -- (\xMax,\i);
} else {
\draw [very thin,gray] (0,\i) -- (\xMax,\i) node [left] at ([yshift=0.25cm]0,\i) {$\i$};
}
}
}
\end{tikzpicture}
但我还是没能找出原因。您有什么想法吗?
答案1
您可以\draw
在 内使用和公司tikzmath
。相关规则在 部分中有说明。59.7 在解析器外执行代码pgfmanual v3.1.5 的说明:
那么你需要更加小心地使用空格和;
。例如,在
for \i in {0,0.5,...,\yMax} {
}
和之间不能有空格{
。此外,每个if
and 循环都需要以 结束;
。如果你遵循这些规则,你会得到
\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}
\newcommand*{\xMax}{6}
\newcommand*{\yMax}{6}
\tikzmath{real \i;
int \k;
\k = 0;
for \i in {0,0.5,...,\xMax}{
if \i == \xMax then {
{\draw [very thin,gray] (\i,0) -- (\i,\yMax);};
} else {
{\draw [very thin,gray] (\i,0) -- (\i,\yMax) node[below=0.0cm] at
([xshift=0.275cm]\i,0) {$X_{\k}$};};
};
\k = \k + 1;
};
for \i in {0,0.5,...,\yMax}{
if \i == \yMax then {
{\draw [very thin,gray] (0,\i) -- (\xMax,\i);};
} else {
{\draw [very thin,gray] (0,\i) -- (\xMax,\i) node [left] at
([yshift=0.25cm]0,\i) {$\i$};};
};
};
}
\end{tikzpicture}
\end{document}