Still trying to figure out the TikZ pipeline (i.e. the sequence of low-level actions that a TikZ path is converted to as it is being "executed") by analysing the source code, specifically <tex installation directory>/tex/generic/pgf/frontendlayer/tikz/tikz.code.tex...
The \tikz@handle macro is dubbed (in the comment preceding this macro) the "Central dispatcher for commands". It is the engine driving the "execution" of a TikZ picture. It works by identifying the smallest semantically meaningful chunk at the beginning of the input stream, and then calling the appropriate handler. When the handlers finish processing, they call \tikz@scan@next@command to move the input stream "cursor" past the just-processed chunk, and then call \tikz@handle recursively.
It is important to realize that the input stream is scanned linearly from start to end, always moving the "cursor" forward, and that the next action to take place is the one determined by the chunk at the current "cursor".
\tikz@handle is implemented as a long, but simple switch statement:
\def\tikz@handle{%
\let\@next=\tikz@expand%
\ifx\pgf@let@token(%)
\let\@next=\tikz@movetoabs%
\else%
\ifx\pgf@let@token+%
\let\@next=\tikz@movetorel%
\else%
\ifx\pgf@let@token-%
\let\@next=\tikz@lineto%
\else%
\ifx\pgf@let@token.%
\let\@next=\tikz@dot%
\else%
\ifx\pgf@let@token r%
\let\@next=\tikz@rect%
\else%
\ifx\pgf@let@token n%
\let\@next=\tikz@fig%
\else%
\ifx\pgf@let@token[%]
\let\@next=\tikz@parse@options%
\else%
...
\fi%
\fi%
\fi%
\fi%
\fi%
\fi%
\@next%
}
As can be seen in the last ifx clause above, when options, identified by an opening square bracket, are encountered, the \tikz@parse@options handler is invoked. This simple macro is defined as follows:
\def\tikz@parse@options#1]{%
\tikzset{#1}%
\tikz@scan@next@command%
}
It does nothing but pass the options to \tikzset, which simply processes the options using the \pgfkeys command, documented in detail in Section 82 of the TikZ & PGF manual for version 3.0.1a, with the default path set to /tikz (see p. 128).
All this boils down to the following conclusion. The "execution" of a path of the form
\path[<options>] ...;
starts with the <options>. The <options> are executed before any other element on the path.
Equipped with this understanding, let's consider the following LaTeX manuscript featuring a simple TikZ picture. The picture consists of a path that is comprised of a single, empty node at the origin with a drawn border. The path has a single option: the draw color is set to red.
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\tikz \path[draw=red] (0,0) node[draw] {};
\end{document}
Based on the conclusion highlighted above, we expect the draw=red option to be executed before the node is created, and therefore the node border's color should be red. Our prediction is spot on:
Let's now add the option behind path to the node:
\tikz \path[draw=red] (0,0) node[draw,behind path] {};
This makes no difference to our analysis, and we therefore expect the same picture as the one generated before. Alas, our expectations are crushed: the border is rendered black!
Why, oh why? The added option signifies (p. 215 of the manual) that the node should be drawn behind the path. In the present case this is moot, since there is no path (or, more precisely, the path is degenerate, consisting of a single point at the origin), but even if there were a path, it would not matter: the node's border would still come out black, despite having established that the path options (draw=red in this case) are executed first.
Addendum
After some digging around the source code and experimenting I've come to realize that the story is stranger still than what I presented above, and that the real question I should ask is not: 'Why does the node's border in the 2nd example black?', but: 'Why does the node's border in the 1st example red?'
For, you see, it turns out that in the beginning stages of processing a node - every node - the options are cleared, including the stroke-color option, if set, and they remain unset all while the node is being rendered and eventually locked in a TeX box. This is true whether or not the behind path option has been specified.
This means that even in the first example, the one seen above to have a red border, when you view the node after it has just finished rendering and has been enclosed in a TeX box, it looks exactly like the 2nd picture above, the one with the black border.
So how does a black-bordered node that has been locked in a TeX box (which is itself nested in another TeX box), transform later into a red-bordered node? Can a Cushite change his skin, or a leopard its spots?
To flesh out the last four paragraphs' claims with references to the source-code:
The point in the beginning stages of a node's processing where the options are cleared, is in the macro
\tikz@normal@fig:\let\tikz@options=\pgfutil@emptyYou can verify that, just before this line,
\tikz@optionscontains the instruction\pgfsetstrokecolor{red}, by prepending\wlog{options: \meaning\tikz@options}%The rendering and locking-in-a-TeX-box of a node take place inside the
\tikz@fig@continuemacro:\setbox\tikz@whichbox=\hbox{% \unhbox\tikz@whichbox% \hbox{{% \pgfinterruptpath% \pgfscope% \tikz@options% \setbox\tikz@figbox=\box\pgfutil@voidb@x% \setbox\tikz@figbox@bg=\box\pgfutil@voidb@x% \pgfmultipartnode{\tikz@shape}{\tikz@anchor}{\tikz@fig@name}{% \pgfutil@tempdima=\pgflinewidth% {\begingroup\tikz@finish}% \global\pgflinewidth=\pgfutil@tempdima% }% \endpgfscope \endpgfinterruptpath% }}% }%To view the node's appearance just after it has been rendered and locked in a TeX box, add the following line immediately after the above code:
\shipout\expandafter\copy\tikz@whichbox%An extra page will be prepended to the final pdf file, showing this box's contents.
By the way, you should verify that the
\tikz@optionsabove are empty, even when thebehind pathoptions has not been given!The code that renders the node, shown in the previous bullet-point, delegates to the
\tikz@finishmacro. This macro is called twice: firstly to render the node, and later to render the overall path. If you\shipoutthe node's box (\tikz@whichbox) just before it is rendered, which, in the 2nd example, happens in the following line:% Step 13: Add labels and nodes % \box\tikz@figbox%(
\tikz@whichboxexpands to\tikz@figboxwhen thebehind pathoption is not specified), you will notice that the node's border is black! (Note that you will get an extra empty page in the beginning of the pdf file, since when the node is being rendered this box is empty; only once the node has been rendered does this box come to be, enclosing the node.)You can verify that no options are applied when the node is being rendered, by appending
\wlog{here!}to the end of theifxbranch, and\wlog{there!}just after the\elsekeyword in the following:% % Step 3: Setup options % \ifx\tikz@options\pgfutil@empty% \else% \pgfsys@beginscope% \let\pgfscope@stroke@color=\pgf@strokecolor@global% \let\pgfscope@fill@color=\pgf@fillcolor@global% \begingroup% \tikz@options% \fi%The log will contain two entries: 'here!' and 'there!', resulting from the rendering of the node and path, respectively.


draw=redto the node options and get on with my life. – Mark Wibrow Aug 04 '17 at 09:58\tikz@finishagain. – cfr Aug 04 '17 at 16:36draw=redoption withfont=\Hugeortext=red, the options do take effect. I have a feeling (but I cannot pin it down to a place in the source code) that some of the color settings get reset just before the node is rendered, but evidently not all color settings, sincetext=redworks. – Evan Aad Aug 04 '17 at 17:28\tikz@fig@mainintikz.code.tex– Mark Wibrow Aug 04 '17 at 18:52\path[<options>] ...;is executed, the first thing that happens is that<options>get executed, and that the reason why, in my 2nd example, the node's draw color is not red is because at some point before the node is rendered the valueredgets reset toblack(or, at any rate, to the default color)? – Evan Aad Aug 04 '17 at 21:21\pgfmultipartnodecommand on line 3908 (there is only one\pgfmultipartnodecommand in the entire file). I placed a\typeout{\tikz@strokecolor}command just before this, and it output: 'red'. – Evan Aad Aug 05 '17 at 00:32\tikz@normal@fig, on line 3650. Interestingly, the reset occurs regardless of whether the optionbehind pathis present! So apparently whenbehind pathis not specified, the options are first reset, and later restored! I'll now try and find out when this restoration takes place. – Evan Aad Aug 05 '17 at 01:46\tikz@finishthat ultimately determines the color of the node's border. But this only makes the mystery greater, since\tikz@finishis executed twice: first to typeset the node and then to typeset the overall path, and during the first time the options are empty, while during the second time the node has already been typeset with respect to empty options. – Evan Aad Aug 05 '17 at 09:59\pgfmultipartnodecommand (there's only one instance of this command in the entire file), and this command is executed inside a TeX box. Just before it is executed, the\tikz@optionsmacro is empty, and remains so throughout the subsequent execution of the\tikz@finishmacro, which typesets the node. I checked this by printing the value of\tikz@optionsto the log at various points. – Evan Aad Aug 05 '17 at 09:59[draw=blue]towards the end of a path. – cfr Aug 05 '17 at 12:06