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.

I am not too sure just by reading the manual as to the subtle differences between the .initial and .default PGF key types. Can someone explain and demonstrate the differences with a minimal example?

share|improve this question

2 Answers 2

up vote 10 down vote accepted

They're for two different things: A key defined using /.initial=<value or string> is a value-storing key, with the initial value set to <value or string>.

The /.default keyword defines what value will be used as the argument for a key defined with /.code=<code> if no argument is provided.

The two things are similar, but not equivalent. A key can at the same time have a value, which can be queried with \pgfkeysvalueof{/key}, and a code, which will be run when you just call the key using \pgfkeys{/key}. If no code is defined, the two are the same, but if you have defined both a .initial


%% Case 1: We want a key to store a value

% Just passing a value to an undefined key fails:

% It first needs to be initialised

% Then we can get the value

% After the key has been initialised, we can change the stored value using simple assignments

%% Case 2: We want a key to execute code using the argument
\pgfkeys{secondkey/.code=Your argument: \textbf{#1}}
\pgfkeys{secondkey=Some words}

% If we don't use an argument, it's assumed to be empty

% We can provide a default value to be used if no argument is provided:

% Let's define a .code key that just returns the argument...
% ...and give it an initial value...
% ...and a default argument

% If we query the value, we get "green"
% If we run the code, we get "red"

share|improve this answer
Does tikz have a reason for this semantics, ie, for separating .initial from .default? They seem to serve the same purpose: if there is no user-supplied value, use initial or default value. –  Ahmed Musa Apr 5 '12 at 16:26
@AhmedMusa: The value stored in a key and the code it runs are two different things. I've edited the example. Also, they don't really serve the same purpose: .initial initialises a key with a value, once you've assigned a different value to the key, the initial value is lost. The .default value stays around, even if you call the key with a different argument. –  Jake Apr 5 '12 at 16:48
I think there is an existence issue as well. Non-existing keys originated by /.initial would be assumed as empty. However, if you want to use /.value required it seems that the /.default is the way to go. I am just guessing by the way I have to check once again. –  percusse Apr 5 '12 at 16:54
@Jake the /.default also applies for the /.style keys (you only mention /.code). I know that there is no implementation difference (from a user perspective), however for future readers might I suggest to add that as well to disclose any ambiguities. –  zeroth Apr 5 '12 at 17:22

Although this does not specifically add anything to the answer already provided, the following example helped me understand the difference more intuitively:





%% Create the keys, default values, etc.
  /mythingy/.is family, /mythingy,
  usecolor/.default = green,
  usecolor/.code = {You have chosen to use the color #1.},
  usecolor/.initial = purple,

%% Create a command that will use the keys
  \pgfkeys{/mythingy, #1}   %% The usecolor/.code is executed
  \pgfkeysvalueof{/mythingy/usecolor}   %% Retrieves only the VALUE of the usecolor key

%% Use the command along with the keys in various different manners

\textbf{Attempt 1}:\\
%% Here, the usecolor/.code is executed using "blue" as the argument


\textbf{Attempt 2}:\\
%% Here, the usecolor/.code is execute using the /.default argument value "green"


\textbf{Attempt 3}:\\
%% Here, the usecolor key is initialized to the /.initial value "purple"
%% However, the usecolor/.code is not executed !!!


which outputs to:

enter image description here

In understanding this behavior, it is very important to understand the conceptual difference between the VALUE of a key and an ARGUMENT for the code associated to that key.

I could imagine this distinction being used in the following manner (see code below), although this example is grotesque (in my opinion) in the sense that it should use a distinct second key radius to actually control the radius:





  /mypainter/.is family, /mypainter,
  drawcircle/.default = true,
  drawcircle/.code = {%
        \draw (0,0) circle[radius=\pgfkeysvalueof{/mypainter/drawcircle}];
  drawcircle/.initial = 1cm,

  Executing {\ttfamily drawcircle/.code}: \pgfkeys{/mypainter, #1}
  Value of key {\ttfamily drawcircle}: \pgfkeysvalueof{/mypainter/drawcircle}

\textbf{Attempt 1}:\\


\textbf{Attempt 2}:\\


\textbf{Attempt 3}:\\
%% The order of the "keys" is important here (try switching the two)
\mypainter[drawcircle/.initial=2cm, drawcircle]


\textbf{Attempt 4}:\\
%% Notice the drawcircle/.initial now remains set to 2cm !!!


\textbf{Attempt 5}:\\


The output to that last bit of code is:

Output for the drawcircle example

share|improve this answer

Your Answer


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.