0

I would like to draw a 1x3 grid in the top tree node, then fill the two leftmost cells of the grid.

This is my code:

\node {\tikz\draw[step=.2cm] (0, 0) grid (.6, .2); \tikz\fill (0,0) rectangle (.4,.2)}
    child {node {\tikz\draw[step=.2cm] (0, 0) grid (.6, .2);}
    child {node {\tikz\draw[step=.2cm] (0, 0) grid (.6, .2);};

It outputs this:

enter image description here

How can I get the rectangle to fill inside the grid?

1
  • 1
    This nests tikzpictures, which is not supported.
    – user194703
    May 4, 2020 at 18:05

1 Answer 1

1

Your approach nests tikzpictures, which is not supported. Rather, you an use the path picture of nodes to draw the grid. Also, I recommend forest for drawing trees (yet the grid node style defined below works in plain TikZ as well, of course).

\documentclass{article}
\usepackage{forest}
\tikzset{grid/.style args={#1/#2}{draw,minimum height=1ex,minimum width=#1*1ex,
path picture={\foreach \XX in {1,...,\the\numexpr#1-1}
{\draw ([xshift=\XX*1ex]path picture bounding box.south west) --
([xshift=\XX*1ex]path picture bounding box.north west);
 }
\ifnum#2>0 
\foreach \XX in {1,...,#2}
{\fill ([xshift=1ex-\XX*1ex]path picture bounding box.south east) rectangle
([xshift=-\XX*1ex]path picture bounding box.north east);
 }
\fi  
}}}
\begin{document}
\begin{forest}
[,grid=5/2,
 [,grid=3/0]
 [,grid=3/0]
]
\end{forest}
\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 .