[Tex/LaTex] How to apply colour options to the contents of TikZ nodes when the content includes \sbox{}\usebox{}

colorforestlogictikz-pgfturnstile

Thanks to Sašo Živanović for helping to identify the problem which arises as a result of my trying to apply options such as blue or text=blue to nodes within Forest trees which include turnstiles from the turnstile package.

Normally, if I apply a colour option such as blue to a TikZ node, the content of the node is coloured, whether it be text or maths.

However, if the node includes a turnstile provided by the turnstile package, then the colouration only affects content before and after the turnstile. The turnstile itself is not affected.

If I use text=blue, only the content prior to the turnstile is coloured. Neither the turnstile nor anything after it is affected.

Outside the tikzpicture environment, however, turnstiles are coloured normally, along with their surrounding text or maths.

For example,

{\color{blue}
\[
  p \leftrightarrow q \sststile{}{} p \rightarrow q
\]
}
\centering

TikZ/blue:

\tikz{\node [blue] {$p \leftrightarrow q \sststile{}{} p \rightarrow q$};  }

TikZ/blue text:

\tikz{\node [text=blue] {$p \leftrightarrow q \sststile{}{} p \rightarrow q$};  }

produces

blue and black

Sašo Živanović has established that the issue here is not specific to turnstiles but applies to any content which uses \sbox{} in a similar way. For example:

\newsavebox\mybox
{\color{blue} This is in blue. \sbox{\mybox}{This is in a box.}\usebox{\mybox} This is in blue.}

\tikz{\node[blue]{This is in blue. \sbox{\mybox}{This is in a box.}\usebox{\mybox} This is in blue.};}

produces

save boxes in blue and black

This question, to which I am also indebted to Sašo Živanović for directing me, seems related.

I am guessing from the discussion there that the \sbox{} is executed before the option blue or text=blue is applied to the node content, so that the active colour at the point when the box is saved is not sensitive to the options applied to the node.

Is this correct? Is the effect avoidable? And why does text=blue do something different? Shouldn't the effect of the \sbox{} be limited to its group, so that even if its contents remains black, following content should be appropriately coloured?

Complete MWE:

\documentclass{article}
\usepackage{turnstile,tikz}
\begin{document}
{\color{blue}
\[
  p \leftrightarrow q \sststile{}{} p \rightarrow q
\]
}
\centering

TikZ/blue:

\tikz{\node [blue] {$p \leftrightarrow q \sststile{}{} p \rightarrow q$};  }

TikZ/blue text:

\tikz{\node [text=blue] {$p \leftrightarrow q \sststile{}{} p \rightarrow q$};  }

\newsavebox\mybox
{\color{blue} This is in blue. \sbox{\mybox}{This is in a box.}\usebox{\mybox} This is in blue.}

\tikz{\node[blue]{This is in blue. \sbox{\mybox}{This is in a box.}\usebox{\mybox} This is in blue.};}

\end{document}

Best Answer

This seems to be a failing in the pgf colour support which isn't setting the current colour as understood by latex.

\documentclass{article}
\usepackage{turnstile,tikz}
\makeatletter
\begin{document}
{\color{blue}
\[
  p \leftrightarrow q \sststile{}{} p \rightarrow q
\]
}
\centering

TikZ/blue:

\tikz{\node [blue] {$p \leftrightarrow q \sststile{}{} p \rightarrow q$};  }

TikZ/blue text:

\tikz{\node [text=blue] {$p \leftrightarrow q \sststile{}{} p \rightarrow q$};  }

\newsavebox\mybox
{\color{blue} \show\current@color This is in blue. \sbox{\mybox}{This is in a box.}\usebox{\mybox} This is in blue.}


\tikz{\node[blue]{\show\current@color\show\pgf@strokecolor@global This is in blue. \sbox{\mybox}{This is in a box.}\usebox{\mybox} This is in blue.};}

\edef\foo#1#2#3#4#5{#3}
\def\resetcurrentcolor{\edef\current@color{\expandafter\foo\pgf@strokecolor@global}}
\tikz{\node[blue]{\resetcurrentcolor This is in blue. \sbox{\mybox}{This is in a box.}\usebox{\mybox} This is in blue.};}

\end{document}

produces a log of

> \current@color=macro:
->0 0 1 rg 0 0 1 RG.
l.21 {\color{blue} \show\current@color
                                       This is in blue. \sbox{\mybox}{This i...

? 
> \current@color=macro:
->0 g 0 G.
l.24 \tikz{\node[blue]{\show\current@color
                                          \show\pgf@strokecolor@global This ...

? 
> \pgf@strokecolor@global=macro:
->\xcolor@ {}{0 0 1 rg 0 0 1 RG}{rgb}{0,0,1}.
l.24 ...\current@color\show\pgf@strokecolor@global
                                                   This is in blue. \sbox{\m...

? 

showing that when \color{blue} was used, \current@color is (the pdf for) blue however in the node[blue] case, although the text is blue, \current@color is (the pdf for) black.

This is important as \sbox does not "know" the state of the color stack maintained by the pdf backend or dvi driver it just sets the colour of the saved text to \current@color

The \resetcurrentcolor command doesn't actually set the colour (it issues no \special or \pdfliteral) it just redefines \current@color to use the stroke colour as last set by PGF.

enter image description here