这个问题与这问题。我还需要能够将节点的后半部分涂成任何其他颜色,最好是通过另一个 pgf 参数。我尝试了不同的方法,都修改了下面的代码,但每一种方法都以不同的方式失败了。有人能告诉我吗?
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc}
\makeatletter
\tikzset{
prefix after node/.style={
prefix after command={\pgfextra{#1}}
},
/semifill/ang/.store in=\semi@ang,
/semifill/ang=0,
semifill/.style={
circle, draw,
prefix after node={
\typeout{aaa \semi@ang}
\let\nodename\tikz@last@fig@name
\fill[/semifill/.cd, /semifill/.search also={/tikz}, #1]
let \p1 = (\nodename.north), \p2 = (\nodename.center) in
let \n1 = {\y1 - \y2} in
(\nodename.\semi@ang) arc [radius=\n1, start angle=\semi@ang, delta angle=180];
},
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node[semifill={gray,ang=60}] {$y$};
\end{tikzpicture}
\end{document}
答案1
您可以\fill
再次执行相同的路径(delta angle=-180
尽管使用 )并使用分配了值的键:
/semifill/upper/.initial=none,
/semifill/lower/.initial=none,
你的填充物在哪里使用fill=\pgfkeysvalueof{/semifill/upper}
,你可以这样做
\node[semifill={lower=gray,upper=red,ang=60}] {$y$};
但你需要两倍的计算。我会把它放在一条路径上。
请注意,\pgfqkeys{/semifill}{#1}
它会评估您的选择(\tikzset
是\pgfqkeys{/tikz}{#1}
)。
使用to path
需要一个目标节点(即使我们根本不使用它),因此坐标为空()
。
请注意,正如链接的答案中所述,半圆甚至位于节点的边界下方,这有时取决于渲染效果。
我们可能会减去它\pgflinewidth
,\n1
但我们需要进行额外的计算。
代码
\documentclass[tikz, border=.1cm]{standalone}
\usetikzlibrary{calc}
\tikzset{
prefix after node/.style={prefix after command=\pgfextra{#1}},
/semifill/ang/.initial=45,
/semifill/upper/.initial=none,
/semifill/lower/.initial=none,
semifill/.style={
circle, draw,
prefix after node={
\pgfqkeys{/semifill}{#1}
\path let \p1 = (\tikzlastnode.north), \p2 = (\tikzlastnode.center),
\n1 = {\y1-\y2} in [radius=\n1]
(\tikzlastnode.\pgfkeysvalueof{/semifill/ang})
edge[
draw=none,
fill=\pgfkeysvalueof{/semifill/upper},
to path={
arc[start angle=\pgfkeysvalueof{/semifill/ang}, delta angle=180]
-- cycle}] ()
(\tikzlastnode.\pgfkeysvalueof{/semifill/ang})
edge[
draw=none,
fill=\pgfkeysvalueof{/semifill/lower},
to path={
arc[start angle=\pgfkeysvalueof{/semifill/ang}, delta angle=-180]
-- cycle}] ();}}}
\begin{document}
\begin{tikzpicture}
\node[semifill={lower=gray, }] {45};
\node[semifill={upper=gray, ang=-45}] at (1,0) {10};
\node[semifill={upper=yellow, lower=red, ang=145}] at (0,1) {145};
\node[semifill={upper=yellow, lower=red, ang=235}] at (1,1) {235};
\end{tikzpicture}
\end{document}
答案2
这只是一种做法path picture
,请参阅 PGF 手册第 15.6 节;另请参阅上面的链接来自@muzimuzhi。该样式的默认设置是gray!50
。
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{shapes.geometric} % for [ellipse], [diamond], etc
\begin{document}
% see [path picture] in the PGF manual, Section 15.6
% also see https://tex.stackexchange.com/a/462473/140722
\begin{tikzpicture}[fill fraction/.style={path picture={
\fill[#1]
(path picture bounding box.south) rectangle
(path picture bounding box.north west);
}},
fill fraction/.default=gray!50
]
\path[nodes={draw}]
(210:1.5) node[fill fraction=yellow!50] (A) {AaA}
(-30:1.5) node[fill fraction=green!30,circle] (B) {BbB}
(90:1.5) node[fill fraction,circle] (C) {CcC}
(145:2) node[fill fraction=violet!50,ellipse] (D) {DdD}
(35:2) node[fill fraction=orange!50,diamond] (E) {EeE}
(-90:2) node[fill fraction=orange!50,star] (F) {FfF};
\draw[->] (A)--(B);
\draw[->] (B)--(C);
\draw[->] (C)--(A);
\end{tikzpicture}
\end{document}