5

Is it really not possible to define drop shadow (with or w/o options) as a macro?

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{shadows}
\newcommand{\test}{drop shadow={shadow xshift=1ex}}
\begin{document}
\begin{tikzpicture}
 \node[rectangle,fill=red,drop shadow={shadow xshift=1ex}] at (0,0) {test};
 \node[rectangle,fill=green,\test] at (5,0) {test};
\end{tikzpicture}
\end{document}

When I test this file I get the message 'pgfkeys Error: I do not know the key ...'. I regularly use other options of \node with such macros without problems so far.

1 Answer 1

5

No, you can't do it, due to expansion issues. The command \node receives \test rather than its contents, and it obviously doesn't know it.

There are two basic solutions: (1) Use \tikzset (or \tikzstyle):

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{shadows}
\tikzset{ test/.style = {drop shadow={shadow xshift=1ex}} }
\begin{document}
\begin{tikzpicture}
 \node[rectangle,fill=red,drop shadow={shadow xshift=1ex}] at (0,0) {test};
 \node[rectangle,fill=green,test] at (5,0) {test};
\end{tikzpicture}
\end{document}

(2) Use dirty trick with \edef to expand \test into its contents before \node is expanded. However, this goes with a large PLEASE DO NOT DO THAT flag:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{shadows}
\newcommand{\test}{drop shadow={shadow xshift=1ex}}
\begin{document}
\begin{tikzpicture}
 \node[rectangle,fill=red,drop shadow={shadow xshift=1ex}] at (0,0) {test};
 \edef\xyz{\noexpand\node[rectangle,fill=green,\test]}
 \xyz at (5,0) {test};
\end{tikzpicture}
\end{document}
1
  • Yes! This (1) does the trick and your solution is of course far more elegant and tikz-like than my trial. Thanks a lot for the quick help.
    – Jürgen
    Commented Jan 21, 2015 at 8:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .