在编写一个小型库来自动化一些路径绘制函数时,我注意到使用定义的参数替换硬编码的 TikZ 路径选项会返回错误。由于我使用该\def
命令在函数之间共享用户提供的通用参数,因此这对我来说相当麻烦。
这是一个小的 MWE:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[circle,draw] (example1) at (0,0) {1};
\node[circle,draw] (example2) at (1,0) {2};
% this works as expected
\draw (example1) to [out=20,in=160] (example2);
% this doesn't work
\def\pathArguments{out=340,in=200}
\typeout{\pathArguments} % returns 'out=340,in=200', as expected
\draw (example1) to [\pathArguments] (example2);
\end{tikzpicture}
\end{document}
预期输出:
实际产量:
错误:line 12: Package pgfkeys Error: I do not know the key '/tikz/out=340,in=200' and I am going to ignore it. Perhaps you misspelled it. ... (example1) to [\pathArguments] (example2)
我甚至添加了 typeout 来检查是否可能\pathArguments
以任何方式进行修改,但如果只是将 typeout 的结果复制到方括号中而不是\pathArguments
,则输出将符合预期。
答案1
改用 a style
。
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}[pathArguments/.style={out=340,in=200}]
\node[circle,draw] (example1) at (0,0) {1};
\node[circle,draw] (example2) at (1,0) {2};
% this works as expected
\draw (example1) to [out=20,in=160] (example2);
% this works as well
\draw (example1) to [pathArguments] (example2);
\end{tikzpicture}
\end{document}