8

I have a macro that will draw several things in a TikZ picture using some styles I defined, which include fills:

\documentclass{article}
\usepackage{tikz}
\tikzset{%
mystyle1/.style={fill=red,font={\bfseries}},
mystyle2/.style={fill=yellow,draw=blue}
}
\newcommand{\doit}{%
 \begin{tikzpicture}
  \node[mystyle1,circle]at(0,0){Text1};
  \node[mystyle2,rectangle] at (3,0){Text2};
 \end{tikzpicture}
}
\begin{document}
Foo

\doit

bar
\end{document}

Which looks like

have

How can I tell TikZ, before or after the \tikzset, to

don't do the fills but everything else

without redefining all the styles? (Here it's only two, but in my real applications it's many and they may change.)

So, the result should look like this:

want

I thought, that something like

\tikzset{every node/.append style={fill=none}}

should work, but it doesn't.

3 Answers 3

4

One solution could be to define a style myfill which can be switched on and off.

\documentclass{article}
\usepackage{tikz}
\tikzset{myfill/.style={fill=#1}}

\tikzset{%
mystyle1/.style={myfill=red,font={\bfseries}},
mystyle2/.style={myfill=yellow,draw=blue}
}
\newcommand{\doit}{%
 \begin{tikzpicture}
  \node[mystyle1,circle]at(0,0){Text1};
  \node[mystyle2,rectangle] at (3,0){Text2};
 \end{tikzpicture}
}
\begin{document}
Foo

\doit

bar

\tikzset{myfill/.style=}
\doit

off
\end{document}

enter image description here

6

I would recommend you allow for an optional Parameter to the \doit macro with which you can specify other options.. So \doit produces the first image and \doit[fill=none] produces the second:

enter image description here

If you want more flexibility you can have two optional parameters to control the options for the circle and rectangle separately. So the third option below was produced with:

\doit[fill=orange, draw=blue, ultra thick][fill=green!50, draw=magenta, ultra thick]

enter image description here

Notes:

  • I used the xparse package to provide the macro that allows for two optional parameters. If you do not want to include an additional package, there are probably other ways to accomplish this.

Code: One Optional Parameter

\documentclass{article}
\usepackage{tikz}
\tikzset{%
    mystyle1/.style={fill=red,font={\bfseries}},
    mystyle2/.style={fill=yellow,draw=blue}
}

\newcommand{\doit}[1][]{%
 \begin{tikzpicture}
  \node[mystyle1, circle, #1] at (0,0){Text1};
  \node[mystyle2, rectangle, #1] at (3,0){Text2};
 \end{tikzpicture}
}
\begin{document}
Foo

\doit

\doit[fill=none]


bar
\end{document}

Code: Two Optional Parameters

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}

\tikzset{%
    mystyle1/.style={fill=red,font={\bfseries}},
    mystyle2/.style={fill=yellow,draw=blue}
}

\NewDocumentCommand{\doit}{O{} O{}}{%
 \begin{tikzpicture}
  \node[mystyle1, circle, #1] at (0,0){Text1};
  \node[mystyle2, rectangle, #2] at (3,0){Text2};
 \end{tikzpicture}
}
\begin{document}
Foo

\doit

\doit[fill=none][fill=none]

\doit[fill=orange, draw=blue, ultra thick][fill=green!50, draw=magenta, ultra thick]

bar
\end{document}
4
  • Thanks. But I wanted a solution without redefining the styles.
    – Toscho
    Commented Jan 20, 2014 at 18:51
  • Perhaps I am not fully understanding your comment, but I am not redefining the styles, just adding to them as needed. Your complete style is applied then only what you specify in the optional parameter is overwritten. Commented Jan 20, 2014 at 18:53
  • 2
    I have made no changes to mystyle1 and mystyle2. Commented Jan 20, 2014 at 19:49
  • Ah, sorry, confused the lines.
    – Toscho
    Commented Jan 21, 2014 at 16:42
0

Thanks to inspiration from Thomas F. Sturm and Peter Grill I have found a nice solution for myself:

Just redefine the fill-style:

\tikzset{fill/.style={}}

Please comment, if this is an acceptable way or should be put into l2tabu.

You must log in to answer this question.

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