假设我想使用一些不错的 Ti钾Z 特征,例如投下阴影或者其他什么。我可能想在 tikzpicture 环境内部或外部执行此操作。所以我想知道是否有一种(简单的)方法可以找出命令是否在 tikzpicture 环境中执行。更具体地说,考虑 MWE:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fadings}
\tikzset{CastShadow/.style={anchor=south,inner sep=0,font=\ttfamily\Huge}}
\newcommand{\CastShadowOutsideTikZ}[2][]{
\begin{tikzpicture}[baseline=(temp.base)]
\node[CastShadow,#1](temp){#2};
\node[scope fading=south,opacity=0.4,yscale=-1,CastShadow,#1]{#2};
\end{tikzpicture}
}
\newcommand{\CastShadowInsideTikZ}[2][]{
\node[CastShadow,#1]{#2};
\node[scope fading=south,opacity=0.4,yscale=-1,CastShadow,#1]{#2};
}
\begin{document}
\CastShadowOutsideTikZ{Quack} \CastShadowOutsideTikZ[scale=2]{Quack}
\begin{tikzpicture}
\CastShadowInsideTikZ[at={(0,0)},scale=2]{Feep}
\end{tikzpicture}
\end{document}
我想将命令统一\CastShadowOutsideTikZ
为\CastShadowInsideTikZ
一个命令\CastShadow
,其行为就像\CastShadowInsideTikZ
在 tizkpicture 中调用时一样,在\CastShadowOutsideTikZ
其他情况下也一样。
(我知道这个帖子),但我的问题是是否有更简单的方法。)
答案1
您应该通过在第四个参数中添加错误消息来使下面的代码更加健壮\apptocmd
,以防 TikZ 内部发生变化。
\documentclass{article}
\usepackage{tikz,etoolbox}
\usetikzlibrary{fadings}
\newtoggle{intikz}
\makeatletter
\apptocmd{\tikz@startup@env}{\toggletrue{intikz}}{}{}
\makeatother
\tikzset{CastShadow/.style={anchor=south,inner sep=0,font=\ttfamily\Huge}}
\newcommand{\CastShadowOutsideTikZ}[2][]{
\begin{tikzpicture}[baseline=(temp.base)]
\node[CastShadow,#1](temp){#2};
\node[scope fading=south,opacity=0.4,yscale=-1,CastShadow,#1]{#2};
\end{tikzpicture}
}
\newcommand{\CastShadowInsideTikZ}[2][]{
\node[CastShadow,#1]{#2};
\node[scope fading=south,opacity=0.4,yscale=-1,CastShadow,#1]{#2};
}
\newcommand\CastShadow{\iftoggle{intikz}{\CastShadowInsideTikZ}{\CastShadowOutsideTikZ}}
\begin{document}
\CastShadow{Quack} \CastShadowOutsideTikZ[scale=2]{Quack}
\begin{tikzpicture}
\CastShadow[at={(0,0)},scale=2]{Feep}
\end{tikzpicture}
\end{document}