6

This question is very similar to this question. I additionally need to be able to colour the second half of the node in any other color, preferrably via another pgf-Argument. I tried different approaches, all modifying the code below, but every single one failed in different ways. Could somebody enlighten me?

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

Herbrert
  • 103
  • 3

2 Answers2

5

You could just do the same \fill path again (one with delta angle=-180, though) and use keys that get assigned a value:

/semifill/upper/.initial=none,
/semifill/lower/.initial=none,

where your fillings use fill=\pgfkeysvalueof{/semifill/upper} and you could just do

\node[semifill={lower=gray,upper=red,ang=60}] {$y$};

but you'd need two times the calculations. I'll put in on one path.

Note the \pgfqkeys{/semifill}{#1} which evaluates your options (\tikzset is \pgfqkeys{/tikz}{#1}).

The usage of the to path needs a target node (even though we don't even use it), hence the empty coordinate ().

Notice that – as in the linked answer – the semicircle is even under the border of the node which sometimes is noticable depending on the rendering.
We might substract the \pgflinewidth from \n1 but we need additional calculations then.

enter image description here

Code

\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}
Black Mild
  • 17,569
Qrrbrbirlbel
  • 119,821
  • There's also a very similar solution with path picture which avoids the problem of drawing right onto the outside of the circle. – Qrrbrbirlbel Jul 18 '22 at 18:07
3

This is just a practice of path picture, see the PGF manual, Section 15.6; and also see the above link by @muzimuzhi. The default of the style is gray!50.

enter image description here

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

Black Mild
  • 17,569