# Is there a (simple) way to find out if a command is executed in a tikzpicture environment?

Assume I want to use some nice TikZ feature, such as casting a shadow, or something else. I may want to do that inside or outside a tikzpicture environment. So I am wondering if there is a (simple) way to find out if a command is executed in a tikzpicture environment. To be more specific, consider the MWE:

\documentclass{article}
\usepackage{tikz}
\begin{tikzpicture}[baseline=(temp.base)]
\end{tikzpicture}
}
}

\begin{document}

\begin{tikzpicture}
\end{tikzpicture}
\end{document}


I'd like to unify the commands \CastShadowOutsideTikZ and \CastShadowInsideTikZ to a single command \CastShadow, which behaves like \CastShadowInsideTikZ when called within a tizkpicture and like \CastShadowOutsideTikZ otherwise.

(I am aware of this post), but my question is whether there is a simpler way.)

You should make the code below more robust by adding an error message in the fourth argument of \apptocmd, just in case TikZ internals were to change.

\documentclass{article}
\usepackage{tikz,etoolbox}
\newtoggle{intikz}
\makeatletter
\apptocmd{\tikz@startup@env}{\toggletrue{intikz}}{}{}
\makeatother
\begin{tikzpicture}[baseline=(temp.base)]
\end{tikzpicture}
}
}
\begin{document}

\begin{tikzpicture}
\end{tikzpicture}
\end{document}

• Of course, all bets are off if you place a non-tikz environment inside your tikz environment! (It could be handled too, with a bit more work. You would just have to toggle intikz off on entry to every environment.) – Harald Hanche-Olsen Jan 27 '18 at 20:12