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.

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}
  • 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 Jan 21 '15 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.