6

For some reason I want to mix tikz with commands, but sometimes it doesn't work.

What works

If I use \newcommand{\mycommand}{ultra thick} it compiles.

\documentclass[border=3pt]{standalone}
\usepackage{tikz}

\newcommand{\mycommand}{ultra thick}

\begin{document}
\begin{tikzpicture}
    \draw [\mycommand, draw=black, fill=yellow, fill opacity=0.2]
       (0,0) -- (0,1) -- (1,1) -- cycle;
\end{tikzpicture}
\end{document}

What doesn't work

If I use \newcommand{\mycommand}{fill=yellow} it doesn't compile.

\documentclass[border=3pt]{standalone}
\usepackage{tikz}

\newcommand{\mycommand}{fill=yellow}

\begin{document}
\begin{tikzpicture}
    \draw [ultra thick, draw=black, \mycommand, fill opacity=0.2]
       (0,0) -- (0,1) -- (1,1) -- cycle;
\end{tikzpicture}
\end{document}

Can somebody help me to fix the second example?

Maybe there's even a better solution.

  • 1
    The information on this question could be useful https://tex.stackexchange.com/questions/52372/should-tikzset-or-tikzstyle-be-used-to-define-tikz-styles – Cragfelt Dec 23 '17 at 13:53

1 Answers1

10

When TikZ encounters a macro it expands and uses its current value as a key. In your case it is fill=yellow.

So it looks up a key named fill=yellow and of course cannot find but it doesn't check the contents for an equal sign based on the suspicion of maybe this was meant as a key=value pair. Hence you need an extra expansion. You can do it via various methods but none of them are as practical as the keys themselves.

Instead use your own styles

\tikzset{mystyle/.style={ultra thick, fill=yellow}}

This will always work inside other styles. Note that the keys are invented specifically for this reason so that you don't set new macros for everything.

percusse
  • 157,807