2

I am using the TikZ force directed graph library, and would like to draw a filled-in triangle between three graph nodes. I would have thought this would do it, but it only draws the triangle boundary without any fill:

\usetikzlibrary{graphs,graphdrawing} \usegdlibrary{trees} 
\begin{tikzpicture}
  \graph [tree layout, nodes={draw}]
  { a -- b,
    b -- c,
    c -- a,
    c -- d};
  \path[red,draw,fill=green] (a) -- (b) -- (d) -- (a); 
\end{tikzpicture}

Does anyone know what I'm missing here? Thanks in advance for your help!

1

1 Answer 1

3

Using the node names without specifying a specific coordinate doesn't give you a closed path, so fill doesn't work. You can use

\path[red, draw, fill=green] (a.center) -- (b.center) -- (d.center) -- (a.center);

enter image description here

But that's probably not what you want since the filled triangle will overlap the nodes. You could place the triangle on background layer using the backgrounds library, which also requires that you fill the nodes with white (or whatever the page color is).

enter image description here

Note that the red edge from a to b is on the background layer so is overdrawn by the black edge.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, backgrounds} \usegdlibrary{trees} 

\begin{document}

\begin{tikzpicture}
  \graph [tree layout, nodes={draw, fill=white}]
  { a -- b,
    b -- c,
    c -- a,
    c -- d};
  \scoped[on background layer]\path[red, draw, fill=green] (a.center) -- (b.center) -- (d.center) -- (a.center);
\end{tikzpicture}

\end{document}

You must log in to answer this question.

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