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}

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,
\tikzset{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.
\tikzset
as it looks like the options that would be passed to a command