4

How do I make partially shaded nodes with tikz without hardcoding coordinates?

A plain MWE is as follows:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[mynode/.style={circle,draw=black,minimum size=10mm}]

    \filldraw[fill=gray!25] (-3.5mm,-3.5mm) arc [start angle=225, end angle=405, radius=5mm];
    \node at (0,0) [mynode] {\Large $x$};

\end{tikzpicture}
\end{document}
\end{tikzpicture}

This works, but I had to compute the coordinates of the shaded arc, and is very tedious when I have to work with a large number of nodes. Is there a way to do this automatically?

I have other code that uses tikz-bayesnet, so if the solution can build on top of that it would be even better.

3
  • You can: \usetikzlibrary{backgrounds} and then \begin{scope}[on background layer] \filldraw[fill=gray!25] (a.225) arc [start angle=225, end angle=405, radius=5mm]; \end{scope}. Will that do?
    – user11232
    May 1, 2015 at 5:43
  • Thanks! It almost does it... the background just lines up very slightly off... as you can see from here: i.imgur.com/TLvkrZh.png May 1, 2015 at 7:27
  • Just use \fill instead of \filldraw
    – user11232
    May 1, 2015 at 7:41

3 Answers 3

4

You can load backgrounds library and use the node anchors like a.225. The radius should be half of minimum size. Also, use \fill instead of \filldraw.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[mynode/.style={circle,draw=black,minimum size=10mm}]
    \node (a) at (0,0) [mynode] {\Large $x$};
    \begin{scope}[on background layer]
      \fill[fill=gray!25] (a.225) arc [start angle=225, end angle=405, radius=5mm];
    \end{scope}
\end{tikzpicture}
\end{document}

enter image description here

0
1

If all your node has the same fill, then one way to draw such nodes is use pics:

\documentclass[12pt,tikz,border=3mm]{standalone}
    \usetikzlibrary{backgrounds}

        \begin{document}
    \begin{tikzpicture}[
pics/mynode/.style = {code = 
        {\node[name=a,%node name
               shape=circle, draw, minimum size=10mm,
               inner sep=0mm, outer sep=0mm] at (0,0) {#1};
         \scoped[on background layer]
         \filldraw[gray!50] (a.south west) -- (a.north east) arc (45:-135:5mm);
                 }},
                        ]
\pic    {mynode=\Large$X$};
    \end{tikzpicture}
        \end{document}
0

A more conveniant way with /.style: you can use semifill to specify a style of semi-filled node, and use semifill={gray} to specify the fill color and semifill={ang=125} to specify the angle of dividing line.

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

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .