Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

The PGF/TikZ manual states in section 12.4 Using Graphic Options, page 120 (v2.10), that TikZ styles can be set using:

\tikzset{my style/.style={<tikz options>},...}

However, in several places in the same manual the otherwise undocumented macro \tikzstyle is used for settings styles:

\tikzstyle{my style}=[<tikz options>]

I also have seen this usage in several online code examples.

Why are there two ways to set TikZ styles and which one should be used for new code?

Note: I actually know the answer but think that such a question with good answers would be beneficial for other users.

share|improve this question
    
Your answer to Is there something like \providetikzstyle similar to \providecommand? and the comments that follow is great :) –  cmhughes Apr 18 '12 at 8:42
    
@cmhughes: Thanks. I know the answer(s) for this question are already out there as part of other specific questions and comments, at least part of them. Here I would like to have at least one complete and well written answer just for this point. –  Martin Scharrer Apr 18 '12 at 8:50

2 Answers 2

up vote 52 down vote accepted

Someone on pgf-users list writes to T Tantau :

I suggest keeping the old syntax, \tikzstyle{my style}=[some options,...] since it is much easier to read and understand. The new syntax would be for the more "advanced" users and library writers.

Answer of Till Tantau :

I'm a bit undecided on this.

First, for compatibility reasons \tikzstyle will be around indefinitely, so the real question is what will be better for new code.

The reason I'm trying to kick out the \tikzstyle is that I would like to encourage people to keep style settings as local as possible. So, normally, style definitions should be given at the beginning of a picture as in

\begin{tikzpicture}
[some options,
first style/.style={blah},
second style/.style={blub}]    ...
\end{tikzpicture}

Then, if the tikzpicture is moved/copied/whatever, the styles are moved around with it. Furthermore, styles given as options always apply to the whole scope and end with it automatically.

However, I'm also not quite sure whether this argument is all that convincing. Global styles still have to be set globally using

 \tikzset anyway...

I have started to change the whole documentation code to the new system and it does not seem to change the readability much.

Now I think if you find some \tikzstyle in several places in the manual It's because it's not very important in these cases and perhaps the author forgot to correct the very big document.

Remarks:

  1. The full answer can be found online at http://osdir.com/ml/tex.pgf.user/2007-08/msg00004.html

  2. The answer continued:

    Anyway, you can now say things like

    \tikzstyle{my style}=[draw=#1,fill=#1!20]
    

    and I have actually extended the syntax of \tikzstyle so that one can also provide a default argument as in

    \tikzstyle{my style}[red]=[draw=#1,fill=#1!20]
    

    I never try this because I don't use \tikzstyle.

  3. For pgfplots, see page 8 of the pgfplots manual [revision 1.8]:

    Starting with pgfplots 1.1, \tikzstyle should no longer be used to set pgfplots options.

    Although \tikzstyle is still supported for some older pgfplotsoptions, you should replace any occurance of \tikzstyle with \pgfplotsset{style name/.style={key-value-list}} or the associated /.append style variant.

    Then, on page 280:

    Please note that it is not possible to use the TikZ-command \tikzstyle{stylename}=[]. This was possible in a previous version and is still supported for backwards compatibility. But in some cases, it may not work as expected.

share|improve this answer
2  
This answer would benefit if you could add a short summary on the top. –  Martin Scharrer May 10 '12 at 14:50
    
Yes you are right, I will make it when I have a little time (actually I write some programming notes about TikZ and it will be useful to add some notes about styles) –  Alain Matthes May 10 '12 at 15:44
    
@AlainMatthes: Are your programming notes about TikZ publicly available? If so where? Thanks. –  Sergio Parreiras May 6 at 16:13

TikZ and its underlying engine PGF have a powerful built-in module pgfkeys. It can also be used as a standalone package (via \usepackage{pgfkeys}) to manipulate options for other purposes.

No matter which command is used, therefore, every defined option goes to some instruction in the form of

\pgfkeys{/key family/key/.subkey = {value list} }

Since TikZ is the frontend of PGF engine, analogous to LaTeX and TeX, there is a default key family, tikz. For example, every tikzpicture when started already selects the tikz key family and internally the options are processed accordingly if not stated otherwise.


As we are mostly using TikZ commands, it is tedious to select the tikz key family every time we define a node option etc. so a shortcut serves well here which is the topic of this question. \tikzset command is defined as

% |\tikzset| is a shortcut to set keys that belongs to the tikz
% family.
\def\tikzset{\pgfqkeys{/tikz}}

Hence, \tikzset is a wrapper for Whatever is given next, belongs to the TikZ family.


Up to here, the key family part is handled and we can focus on key/.subkey part. There is not much that can be done without refering to the manual on this issue and the pgfkeys section is quite comprehensive. I will just emphasize that the most common two examples are mykey/.code and mykey/.style.

The subkey /.style is a collection or a catalogue of options to be processed whenever called for, such as "define the shape as circle and draw it then run some code and also put a label and fill it with yellow etc.". A style can also receive some parameters for example, the last example can be achieved by putting

\begin{tikzpicture}
\tikzset{mystyle/.style={draw,circle,label={[fill=yellow]0:#1}}}
\node[mystyle=label text] (nodename) {node text};
\end{tikzpicture}

enter image description here

For more wizardry,say with more arguments etc. please refer to the manual. For the sake of convenience, here is another important subkey /.append style

\begin{tikzpicture}
\tikzset{mystyle/.style={draw,circle,label={[fill=yellow]0:#1}}}
\tikzset{mystyle/.append style={fill=red,rectangle}}
\node[mystyle=label text] (nodename) {node text};
\end{tikzpicture}

which simply adds the arguments to the key. Note that, rectangle comes after circle and dominates the previous option, hence, the node becomes a red rectangle. The order of the style specifications matter.


Now, back to the question!! We have two options to give style specifications. Namely,

\tikset{mystyle/.style= {draw,circle, etc.} }
\tikzset{mystyle/.append style= {rectangle,fill=red, etc.} }
% or ...
\tikzstyle{mystyle}=[draw,circle,label={[fill=yellow]0:#1}]
\tikzstyle{mystyle}+=[fill=red,rectangle]

Both would lead to the same output however as we see that the \tikzstyle form is shorter to type in. Other than that, as far as I know there is no particular difference. For example, appending is simply done by putting a + sign next to = and also as shown below defining a default value is also quite convenient. However tikzstyle is only limited to this particular case and it's not applicable to other subkeys e.g. if you want to put a piece of code for some computation you can directly use

\tikzset{mystyle/.code={execute whatever is given here}}

but there is no \tikzcode similar to \tikzstyle to achieve this.

Hence, as Altermundus quoted, it's more of a shortcut.

For the interested, below is the code where these commands are defined. The relevant code is given in tikz.code.tex file at line 1143.

% Styles
\tikzoption{set style}{\tikzstyle#1}

% Handled in a special way.
\def\tikzstyle{\pgfutil@ifnextchar\bgroup\tikz@style@parseA\tikz@style@parseB}
\def\tikz@style@parseB#1={\tikz@style@parseA{#1}=}
\def\tikz@style@parseA#1#2=#3[#4]{% check for an optional argument
  \pgfutil@in@[{#2}%]
  \ifpgfutil@in@%
    \tikz@style@parseC{#1}#2={#4}%
  \else%
    \tikz@style@parseD{#1}#2={#4}%
  \fi%
}%

\def\tikz@style@parseC#1[#2]#3=#4{%
  \pgfkeys{/tikz/#1/.default={#2}}%
  \pgfutil@in@+{#3}%
  \ifpgfutil@in@%
    \pgfkeys{/tikz/#1/.append style={#4}}%
  \else%
    \pgfkeys{/tikz/#1/.style={#4}}%
  \fi}
\def\tikz@style@parseD#1#2=#3{%
  \pgfutil@in@+{#2}%
  \ifpgfutil@in@%
    \pgfkeys{/tikz/#1/.append style={#3}}%
  \else%
    \pgfkeys{/tikz/#1/.style={#3}}%
  \fi}


At the beginning of the same file there is a note which tells us directly the story about \tikzoption:

% Note: |\tikzoption| is supported for compatibility only. |\tikzset|
% should be used instead.
share|improve this answer
    
Very nice. Maybe add that you can set several keys within the same \tikzset command (\tikzset{my style/.style={...}, my second style/.style={...}, ...}), which is shorter than using \tikzstyle commands. And an added bonus of using \tikzstyle is that you can just cut and paste the same list of styles to the options of a tikzpicture or a scope, which I guess is what Till Tantau means by wanting to "encourage people to keep style settings as local as possible". –  Jake Apr 18 '12 at 10:44
    
@Jake Thanks. I agree with you but Altermundus pretty much covered these by quoting the man himself :) Feel free to edit anyway. Maybe we should make this CW. –  percusse Apr 18 '12 at 13:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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