當內容包含 \sbox{}\usebox{} 時,如何將顏色選項套用至 TikZ 節點的內容?

當內容包含 \sbox{}\usebox{} 時,如何將顏色選項套用至 TikZ 節點的內容?

感謝 Sašo Živanović 幫助識別由於我嘗試將諸如blue或 之類的選項text=blue應用於其中的節點而出現的問題森林樹木,其中包括十字轉門十字轉門包裹。

通常,如果我將顏色選項應用於blueTikZ 節點,則節點的內容將被著色,無論是文字還是數學。

但是,如果節點包含由十字轉門包裝,則著色僅影響旋轉門之前和之後的內容。旋轉柵門本身不受影響。

如果我使用text=blue,則只有十字轉門之前的內容是彩色的。旋轉柵門及其之後的任何東西都不會受到影響。

然而,在tikzpicture環境之外,十字轉門及其周圍的文字或數學是正常著色的。

例如,

{\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$};  }

產生

藍色和黑色

Sašo Živanović 已確定,此處的問題並非特定於旋轉門,而是適用於\sbox{}以類似方式使用的任何內容。例如:

\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.};}

產生

保存藍色和黑色的盒子

這個問題似乎與此相關,我也感謝 Sašo Živanović 的指導。

我從那裡的討論中猜測,\sbox{}在選項之前執行bluetext=blue應用於節點內容,因此保存框時的活動顏色對應用於節點的選項不敏感。

它是否正確?這種影響是可以避免的嗎?為什麼要做text=blue一些不同的事情?難道不應該將其效果\sbox{}限制在其群組內,以便即使其內容保持黑色,後續內容也應適當著色嗎?

完整的 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}

答案1

這似乎是 pgf 顏色支援的失敗,它沒有設定乳膠所理解的當前顏色。

\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}

產生一個日誌

> \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...

? 

表明\color{blue}使用時,\current@color(pdf)是藍色的,但是在這種node[blue]情況下,儘管文字是藍色的,\current@color(pdf)是黑色的。

這很重要,因為它\sbox不「知道」由 pdf 後端或 dvi 驅動程式維護的顏色堆疊的狀態,它只是將保存的文字的顏色設為\current@color

\resetcurrentcolor命令實際上並未設定顏色(它發出 no\special\pdfliteral),它只是重新定義\current@color以使用 PGF 最後設定的描邊顏色。

在此輸入影像描述

相關內容