[Tex/LaTex] Should \tikzset or \tikzstyle be used to define TikZ styles

best practicespgfkeystikz-pgftikz-styles

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.

Best Answer

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.