4

Is it possible to have an option to an \usetikzlibrary command? I would like to define different styles when an option is activate. For example:

\usetikzlibrary[blue]{something}
\usetikzlibrary[green]{something}
\usetikzlibrary[red]{something}

would define different colors for a same

\tikzset{C/.style={thick, circle, fill=red}};

Thanks.

wwjoze
  • 367
  • 1
  • 7
  • Use that option value as a global switch for your custom styles and don't initialize the library until one is set. That's the basic idea of using the key=values. This way you won't have any hacks on the original macros. – percusse Dec 21 '14 at 19:39
  • Or you could make it a package. But what if you want to change the colour for different pictures in the same document? If you set a default colour, you could create a command which would let you override the default. Then you could switch colours within the same document which seems more flexible to me. – cfr Dec 21 '14 at 20:48

2 Answers2

5

As the comment of percusse and cfr says, you can't use options for tikz library. And you can read in the manual that you can replace the curly braces with square brackets (ConTeXt specific).

Here is an example how you can set the option by using .is choice handler.

\documentclass[border=1cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
% ----------- code to put in your  tikzlibrarysomething.code.tex
\tikzset{
  C/.style={ultra thick, circle},
  theme/.is choice,
  theme/ocean/.style={C/.append style={draw=none,fill=blue!50}},
  theme/fire/.style={C/.append style={draw=orange,fill=red}},
  theme/nature/.style={C/.append style={draw=brown,fill=green!70}},
  theme = fire % <- set the default theme
}
%------------- then to use
% \usetikzlibrary{something}
\tikzset{theme=ocean}

%------------- and here we are \begin{document} \tikz \node[C] {Test}; \end{document}

enter image description here

EDIT: After the comments of cfr I have changed the code to make it more "self explanatory".

Kpym
  • 23,002
  • (+1) for using .choice. I like this but why not use one line and let the user pick any colour? – cfr Dec 21 '14 at 23:01
  • @cfr because this is a theme selection, not a color selection. In this minimal example this is not visible. You can have other styles set inside a choice. For the name of the blue theme we can pu ocean ;) – Kpym Dec 21 '14 at 23:39
  • Oh. Well, yes. If it is the name of a theme, that's different. So the key name is evidently just misleading. (theme color does not suggest to me the selection of an entire theme!) – cfr Dec 21 '14 at 23:43
  • @cfr Yes. My choices for the names are not good. I have to replace theme color with theme, change the names of all themes and add one more style inside each choice ... may be the next time ;) – Kpym Dec 21 '14 at 23:47
  • @cfr It's done. I've changed the code. Thanks for your coments. – Kpym Dec 22 '14 at 07:53
  • Thanks for your comments, this is exactly what I was searching for! – wwjoze Dec 22 '14 at 11:50
  • @wwjoze you are welcom. – Kpym Dec 22 '14 at 12:09
3

This is an adaption of Kpym's answer. While Kpym's solution is closer to your stated desiderata, mine is shorter and more flexible in that it allows you to select any colour.

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}

\tikzset{
  C/.style={circle},
  theme color/.style={C/.append style={fill=#1!50}},
}

\tikzset{theme color=magenta}

\begin{document}
  \tikz \node[C] {Test};
\end{document}

However, you might like to include the option of adjusting the opacity, and it is probably best to ensure that defaults are set in case the user (possibly you) does not remember to set the colour or whatever.

The following is based on Claudio Fiandrino's answer and sets up defaults for the colour and opacity, with the option of overriding them. If used in your preamble, you need to enclose in \makeatletter... \makeatother due to the @. If used in your library code, this won't be necessary.

\tikzset{
  C/.style={circle, fill=\my@theme@color!\my@theme@color@opacity},
  theme color/.store in=\my@theme@color,
  theme color opacity/.store in=\my@theme@color@opacity,
  theme settings/.code={%
    \tikzset{#1}},
  theme color=blue,% set a default
  theme color opacity=50,% set a default
}

If you do nothing, you'll get a blue circle, 50% opacity. But you can change those defaults by overriding them in the preamble:

\tikzset{theme settings={theme color=magenta, theme color opacity=75}}

which will give you a magenta circle, filled at 75% opacity. Or you can override them for a particular node or for a particular scope within a picture.

Preamble, scope and node settings override defaults

\documentclass[border=5pt,tikz]{standalone}
\usetikzlibrary{calc}

\makeatletter
\tikzset{% https://tex.stackexchange.com/a/159856/ - Claudio Fiandrino
  C/.style={circle, align=center, fill=\my@theme@color!\my@theme@color@opacity},
  theme color/.store in=\my@theme@color,
  theme color opacity/.store in=\my@theme@color@opacity,
  theme settings/.code={%
    \tikzset{#1}},
  theme color=blue,% set a default
  theme color opacity=50,% set a default
}
\makeatother
\tikzset{theme settings={theme color=magenta, theme color opacity=75}}

\begin{document}
  \begin{tikzpicture}
    \node[C] {Preamble\\Config};
    \begin{scope}[theme settings={theme color=yellow, theme color opacity=100}]
      \node [C, xshift=15mm] {Scope\\Config};
    \end{scope}
    \node [C, theme color=green, theme color opacity=15, xshift=29mm] {Node\\Config};
  \end{tikzpicture}
\end{document}
cfr
  • 198,882