2

I need to embed a (complex) TikZ picture in a command in order to reuse it in several scales and colors. No problems when scaling, but I cannot change the color of the image (see image below for MWE output).

MWE:

\documentclass{article}
\usepackage{tikz}

\tikzset{mystyle/.style={fill=#1}, mystyle/.default=black}

\newcommand{\aaa}{
  \begin{scope}
    \path[mystyle] (0,0) -- (1,0) -- (0,1) -- cycle;
  \end{scope}
}
\newcommand{\bbb}{
  \begin{scope}[mystyle=blue]
    \path[mystyle] (0,0) -- (1,0) -- (0,1) -- cycle;
  \end{scope}
}

\begin{document}
  \begin{tabular}{rl}
    Black triangle:&
    \begin{tikzpicture}
      \aaa
    \end{tikzpicture}
    \\
    Red triangle:&
    \begin{tikzpicture}[mystyle=red]
      \aaa
    \end{tikzpicture}
    \\
    Red triangle:&
    \begin{tikzpicture}
      \begin{scope}[mystyle=red]
        \aaa
      \end{scope}
    \end{tikzpicture}
    \\
    Blue triangle:&
    \begin{tikzpicture}
      \bbb
    \end{tikzpicture}
    \\
    Red triangle:&
    \begin{tikzpicture}[mystyle=red]
      \bbb
    \end{tikzpicture}
    \\
    Red triangle:&
    \begin{tikzpicture}
      \begin{scope}[mystyle=red]
        \bbb
      \end{scope}
    \end{tikzpicture}
  \end{tabular}
\end{document}

MWE rendering

Astrinus
  • 1,809
  • 10
  • 28
  • 1
    I don't really understand why you expect a different outcode. Your path is doing [mystyle] which is equivalent to [mystyle=black]. – Ulrike Fischer Oct 19 '15 at 16:02
  • 2
    Try with \newcommand{aaa}[1][black]{\begin{scope}\path[mystyle=#1](0,0)...\end{scope}}. Then \aaa or \aaa[red], ... – Ignasi Oct 19 '15 at 16:53
  • mystyle/.default=black says 'whenever mystyle is called with no argument, use black'. So you set mystyle=blue for the scope, but then you override that value with mystyle=mystyle=black for the \path. – cfr Oct 19 '15 at 22:21
  • In addition to the other comments and answers, you might want to create a pic ? – Philipp Imhof Oct 20 '15 at 08:33

1 Answers1

1

As cfr and Ulrike explained, \path[mystyle] is equivalent to the default style: \path[mystyle=black]. Then, although you use \begin{scope}[mystyle=red] it has no effect because \path[mystyle] overwrites it.

In case you want all path in each scope use the same style you can use:

\begin{tikzpicture}
\begin{scope}[every path/.style={mystyle=red}]
\path (0,0) -- (1,0) -- (0,1) -- cycle;
\end{scope}
\end{tikzpicture}

Another solution is what I've proposed in my comment:

\documentclass{article}
\usepackage{tikz}

\tikzset{mystyle/.style={fill=#1}, mystyle/.default=black}

\newcommand{\aaa}[1][black]{
  \begin{scope}
    \path[mystyle=#1] (0,0) -- (1,0) -- (0,1) -- cycle;
  \end{scope}
}

\begin{document}
  \begin{tabular}{rl}
    Black triangle:& \tikz{\aaa}\\
    Red triangle:& \tikz{\aaa[red]}\\
    Blue triangle:& \tikz{\aaa[blue]}
  \end{tabular}
\end{document}

The result is:

enter image description here

Note: Although I don't recommend and as a collateral effect of this declaration you can even say \aaa[blue, scale=-1, draw=red, ultra thick] to obtain

enter image description here

Ignasi
  • 136,588
  • Thank you, but the problem is that in the scope there is more than one path, so I cannot say every path, neither I can define commands for all the parts. I'll try a workaround with a \def or something like that. – Astrinus Oct 20 '15 at 08:20
  • @Astrinus I understand you cannot use every path, that's why I suggested \aaa[...]. But if you give us more details about your problem it will be easier for us to help you. Complex figures can be defined inside a tikzpicture but TiKZ also provides command pic for reusing parts of a tikzpicture: http://tex.stackexchange.com/a/151772/1952 – Ignasi Oct 20 '15 at 09:44
  • I have a logo, with three colors and several filled area (about 10). This logo is currently embedded in a scope in order to embed it in different ways with a single command. Now a sheet with many logos, each with slighty different colors from its neighbour, has to be generated and I thought that \foreach would help me a lot. The colors are already styled in the same way as above. – Astrinus Oct 20 '15 at 10:20
  • @Astrinus Although this description is a little bit better I still don't understand the problem. Could you show the code? Or at least something more similar to your real problem than your original question? – Ignasi Oct 20 '15 at 10:35
  • Something similar would involve two paths and two styles, or ten paths and three styles, but the same principle of \aaa holds: I have a command that holds the content of a scope and the path are styled in that way – Astrinus Oct 20 '15 at 11:40
  • I have resolved using \colorlet and overwriting colors. Sledgehammer way, but it works ;-) – Astrinus Oct 20 '15 at 15:29