![线宽关键字似乎会导致 TikZ 错误](https://linux22.com/image/444920/%E7%BA%BF%E5%AE%BD%E5%85%B3%E9%94%AE%E5%AD%97%E4%BC%BC%E4%B9%8E%E4%BC%9A%E5%AF%BC%E8%87%B4%20TikZ%20%E9%94%99%E8%AF%AF.png)
我正在尝试一种简单的格式,这种格式效果很好,直到我在下面的命令中使用线宽关键字\draw
。关键字可以是thick
或thin
或line width=2pt
。运行代码和 thick
给出这个:
例如,TikZ 表示:No shape named S-3 is known.
注释掉thick
会产生预期的图形,如下所示:
是的,我知道我可以用它|-
来绘图,但为了简单起见并最大化米的米我选择了一些更直接的东西。
\documentclass{article}
\usepackage[]{geometry}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{positioning}
%% |=====8><----| %%
\newcounter{stepnum}
\newlength{\myxincr}
\newlength{\myyincr}
%% |=====8><-----| %%
\NewDocumentEnvironment{test}{m}{%
\begin{tikzpicture}
\coordinate (A) at (0,0);
\setlength{\myxincr}{\dimexpr0.5in/#1\relax}
\setlength{\myyincr}{\dimexpr-5in/#1\relax}
\pgfmathsetmacro{\incr}{1/#1}
\pgfmathparse{#1-1}
\draw[
%% Uncomment the next line and this fails -- any line width keyword causes failure
% thick, %% <<<---
red %% color is ok, seemingly
] (A) foreach \ss in {1,...,\pgfmathresult}
{ -- node[fill=white,name=S-\ss]{\ss} ++(0,\myyincr) -- ++(\myxincr,0)}
--node[fill=white,name=S-#1]{#1} ++(0,\myyincr) -- ++(\myxincr,0);
}{%
\end{tikzpicture}
}
\NewDocumentCommand{\foo}{+m}{%
\stepcounter{stepnum}
\node[right=6pt of S-\thestepnum] {%
#1
};%
}
\parindent=0pt
\begin{document}
\begin{test}{4}
\foo{First}
\foo{Second}
\foo{Third}
\foo{Fourth}
\end{test}
\end{document}
答案1
该\pgfmathresult
宏暂时设置为计算结果,在调用其他宏后不能保证其值不变。
例如:
\pgfmathparse{1-1}
\mymacro
\pgfmathresult
仅当不进行计算时这里\pgfmathresult
才会为 0 。\mymacro
对于您的 MWE,您可以通过使用以下命令保存结果来修复此问题\let
:
\documentclass{article}
\usepackage[]{geometry}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{positioning}
%% |=====8><----| %%
\newcounter{stepnum}
\newlength{\myxincr}
\newlength{\myyincr}
%% |=====8><-----| %%
\NewDocumentEnvironment{test}{m}{%
\begin{tikzpicture}
\coordinate (A) at (0,0);
\setlength{\myxincr}{\dimexpr0.5in/#1\relax}
\setlength{\myyincr}{\dimexpr-5in/#1\relax}
\pgfmathsetmacro{\incr}{1/#1}
\pgfmathparse{#1-1}
\let\RES\pgfmathresult % Save result in \RES
\draw[
%% Now this is fine
thick, %% <<<---
red %% color is ok, seemingly
] (A) foreach \ss in {1,...,\RES}
{ -- node[fill=white,name=S-\ss]{\ss} ++(0,\myyincr) -- ++(\myxincr,0)}
--node[fill=white,name=S-#1]{#1} ++(0,\myyincr) -- ++(\myxincr,0);
}{%
\end{tikzpicture}
}
\NewDocumentCommand{\foo}{+m}{%
\stepcounter{stepnum}
\node[right=6pt of S-\thestepnum] {%
#1
};%
}
\parindent=0pt
\begin{document}
\begin{test}{4}
\foo{First}
\foo{Second}
\foo{Third}
\foo{Fourth}
\end{test}
\end{document}
\pgfmathparse
实现/组合的等效方法\let
是使用\pgfmathsetmacro{\RES}{expression}
。