TikZ 能够使用诸如 之类的参数定义参数化样式#1
。如果在\newcommand
或中使用\newenvironment
,它们会与命令的参数混淆。影响非常严重,以至于以下示例甚至无法编译,但会导致 TeX 停滞。有没有办法防止命令参数扩展并延迟 TikZ 处理它?
\documentclass{standalone}
\usepackage{tikz}
\newenvironment{fancypicture}[1]{
\begin{tikzpicture}[
fancy/.style={circle, draw, fill=#1}
% The line above here poses the problem.
% Instead of \newenvironment, this #1 shall be left to be handled by TikZ.
]
\node [fancy=red] at (0, 2) {#1};
}{
\end{tikzpicture}
}
\begin{document}
\begin{fancypicture}
\node [draw] {s};
\end{fancypicture}
\end{document}
我知道将样式声明放在顶层会有所帮助\tikzset
。但这会使它对文档中的所有图片都具有全局性,而我想避免这种情况。
答案1
这在
\documentclass{article}
\usepackage{tikz}
\newenvironment{fancypicture}[1][]{
\begin{tikzpicture}[
fancy/.style={circle, draw, fill=##1}
]
\draw[style=help lines] (0cm,0cm) grid[step=1cm] (2cm,2cm);
\node [fancy=red] at (0,1cm) {#1};
}{
\end{tikzpicture}
}
\begin{document}
\begin{fancypicture}[A]
\node[draw] at (1cm,1cm) {s};
\end{fancypicture}
\end{document}