![使用 TikZ 路径的预定义参数会导致错误](https://linux22.com/image/349688/%E4%BD%BF%E7%94%A8%20TikZ%20%E8%B7%AF%E5%BE%84%E7%9A%84%E9%A2%84%E5%AE%9A%E4%B9%89%E5%8F%82%E6%95%B0%E4%BC%9A%E5%AF%BC%E8%87%B4%E9%94%99%E8%AF%AF.png)
在编写一个小型库来自动化一些路径绘制函数时,我注意到使用定义的参数替换硬编码的 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}