我想在装饰内部设置标志,然后在装饰外部进行检查。我设置了标志(调用\toggletrue{}
或\togglefalse{}
),但当我检查该标志时,结果始终为 false。
对于 MWE,我定义了一个新的装饰,除了设置标志外什么都不做。将此装饰应用于圆圈。然后检查标志。
\documentclass{standalone}
\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{decorations}
\makeatletter
\newtoggle{toggletest}
\pgfdeclaredecoration{custom decoration}{initial}{%
\state{initial}[
width=+0pt,
next state=final,
persistent precomputation={%
\toggletrue{toggletest}
}
]{}
\state{final}{}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\draw [
postaction={%
decorate,
decoration={%
custom decoration
}
}
] (0,0) circle (2cm);
\iftoggle{toggletest}{%
\draw [green] (0,0) circle (1cm);
}{%
\draw [red] (0,0) circle (1cm);
}
\end{tikzpicture}
\end{document}
如果为真,则应绘制绿色圆圈toggletest
,否则绘制红色圆圈。但它总是绘制红色圆圈。
我做错了什么?
答案1
使用全局设置:
\pgfdeclaredecoration{custom decoration}{initial}{%
\state{initial}[
width=+0pt,
next state=final,
persistent precomputation={%
\global\toggletrue{toggletest}
}
]{}
\state{final}{}
}
不过,这里简单说一下\newif
就足够了:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}
\newif\iftoggletest \toggletestfalse
\pgfdeclaredecoration{custom decoration}{initial}{%
\state{initial}[
width=+0pt,
next state=final,
persistent precomputation={%
\global\toggletesttrue
}
]{}
\state{final}{}
}
\begin{document}
\begin{tikzpicture}
\draw [
postaction={%
decorate,
decoration={%
custom decoration
}
}
] (0,0) circle (2cm);
\iftoggletest
\draw [green] (0,0) circle (1cm);
\else
\draw [red] (0,0) circle (1cm);
\fi
\end{tikzpicture}
\end{document}