尝试解决pgf 基本层:再次与颜色斗争,我有一个解决方法(保存状态circuitikz
......)。
但尽管我知道如何在color=
键上加一个钩子,Ti钾Z 允许您仅使用颜色作为同义词,但在这种情况下我找不到如何使其发挥作用:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\tikzset{mykey/.initial=(none)}
\pgfkeys{/tikz/color/.add code={}{\tikzset{mykey={#1}}}}
\begin{document}
\begin{tikzpicture}[]
\draw[color=red] (0,1) node{I see \pgfkeysvalueof{/tikz/mykey}};
\draw[red] (0,0) node{I see \pgfkeysvalueof{/tikz/mykey}};
\end{tikzpicture}
\end{document}
是否可以将钩子“连接”到第二条线上?
顺便说一句:我尝试过这个:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{etoolbox}
\tikzset{mykey/.initial=(none)}
\pgfkeys{/tikz/color/.add code={}{\tikzset{mykey={#1}}}}
\makeatletter
\apptocmd{\tikz@compat@color@set}{\tikzset{mykey={#1}}}%
{\typeout{color patching ok}}{\typeout{color patching failed}}
\makeatletter
\begin{document}
\begin{tikzpicture}[]
\draw[color=red] (0,1) node{I see \pgfkeysvalueof{/tikz/mykey}};
\draw[blue] (0,0) node{I see \pgfkeysvalueof{/tikz/mykey}};
\end{tikzpicture}
\end{document}
虽然没有给出错误,但\tikzset
被调用时仍然失败(可能是范围问题)。
答案1
在 中\pgfkeys{/tikz/.unknown/.code=...}
,\tikz@compat@color@set
在 内部使用\tikz@addoption{...}
,它只是将其参数附加到\tikz@options
。因此,直到在当前路径的最末端使用 时,\tikz@compat@color@set
才会执行。\tikz@options
\tikz@finish
%% run latexdef -p tikz -s tikz@addoption
% tikz.code.tex, line 47:
\def\tikz@addoption#1{%
\expandafter\def\expandafter\tikz@options\expandafter{\tikz@options#1}}%
为了\tikzset{mykey={#1}}
立即执行,恐怕您必须修补密钥的代码/tikz/.unknown
:
\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{regexpatch} % for the starred-form of \xpatchcmd
\tikzset{
mykey/.initial=(none),
color/.append code={\tikzset{mykey={#1}}}
}
\makeatletter
% note `/tikz/.unknown/.@body` is not updated
\pgfkeysgetvalue{/tikz/.unknown/.@cmd}{\my@temp}
\xpatchcmd*\my@temp % use starred-form to replace all (two places actually)
{\expandafter\tikz@addoption\expandafter}
{\tikzset{mykey/.expand once=\tikz@key}%
\expandafter\tikz@addoption\expandafter}
{}{\PatchFailed}
\pgfkeyslet{/tikz/.unknown/.@cmd}{\my@temp}
\makeatletter
\begin{document}
\begin{tikzpicture}[]
\draw[color=red] (0,1) node {I see \pgfkeysvalueof{/tikz/mykey}};
\draw[blue] (0,0) node {I see \pgfkeysvalueof{/tikz/mykey}};
\end{tikzpicture}
\end{document}
答案2
不是答案,但评论有点长。我写了一个命令来输出 tikz 的各种颜色命令。请注意,我不会测试每个位置,例如,节点或范围内的颜色可能不同。但看起来你至少可以尝试使用两个全局命令\pgf@strokecolor@global
和\pgf@fillcolor@global
\documentclass{article}
\usepackage{tikz}
\begin{document}
\makeatletter
\newcommand\showcolors[1]{%
\typeout{^^J#1:^^J========}
\typeout{fill: \tikz@fillcolor}
\typeout{fill: \csname\string\color@pgffillcolor\endcsname}
\typeout{fill (global): \pgf@fillcolor@global}
\typeout{stroke:\tikz@strokecolor}
\typeout{stroke: \csname\string\color@pgfstrokecolor\endcsname}
\typeout{stroke (global): \pgf@strokecolor@global}
\typeout{text:\tikz@textcolor}
\typeout{text:\csname\string\color@tikzcolor\endcsname}
\typeout{current color: \current@color}
}
\tikzset{showcolors/.code={\showcolors{#1}}}
\color{blue} Text
\begin{tikzpicture}[]
\tikz\draw[showcolors=nocolors] (0,0);
\tikz[draw=red, fill=green] \draw[showcolors=draw/fill] (0,0);
\tikz[color=yellow] \draw[showcolors=color] (0,0);
\tikz[brown] \draw[showcolors=no key] (0,0);
\end{tikzpicture}
\end{document}
nocolors:
========
fill:
fill: \\color@pgffillcolor
fill (global): 0 0 1 rg 0 0 1 RG
stroke:
stroke: 0 0 1 rg 0 0 1 RG
stroke (global): 0 0 1 rg 0 0 1 RG
text:
text:\\color@tikzcolor
current color: 0 0 1 rg 0 0 1 RG
draw/fill:
========
fill: green
fill: 0 1 0 rg 0 1 0 RG
fill (global): 0 1 0 rg 0 1 0 RG
stroke:red
stroke: 1 0 0 rg 1 0 0 RG
stroke (global): 1 0 0 rg 1 0 0 RG
text:
text:\\color@tikzcolor
current color: 0 0 1 rg 0 0 1 RG
color:
========
fill:
fill: \\color@pgffillcolor
fill (global): 0 0 1 0 k 0 0 1 0 K
stroke:
stroke: 0 0 1 0 k 0 0 1 0 K
stroke (global): 0 0 1 0 k 0 0 1 0 K
text:yellow
text:\\color@tikzcolor
current color: 0 0 1 rg 0 0 1 RG
no key:
========
fill:
fill: 0.75 0.5 0.25 rg 0.75 0.5 0.25 RG
fill (global): 0.75 0.5 0.25 rg 0.75 0.5 0.25 RG
stroke:
stroke: 0 0 1 rg 0 0 1 RG
stroke (global): 0.75 0.5 0.25 rg 0.75 0.5 0.25 RG
text:brown
text:\\color@tikzcolor
current color: 0.75 0.5 0.25 rg 0.75 0.5 0.25 RG