5

I almost figured out how to draw what I want by piecing together two answers here (@Ignasi for TiKZ matrix of objects and @alexraasch for L shaped boxes in tikz matrix).

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{matrix, backgrounds}
\begin{document}

\begin{tikzpicture}[
    mycell/.style={draw, minimum size=1cm},
    dot1/.style={mycell,
        append after command={\pgfextra \fill (\tikzlastnode) 
        circle[radius=10pt]; \endpgfextra}},
    dot2/.style={mycell,
        append after command={\pgfextra \fill[white] (\tikzlastnode)  
        circle[radius=10pt]; \endpgfextra}},
    dot3/.style={mycell,
        append after command={\pgfextra \draw[thick] (\tikzlastnode) 
        circle[radius=10pt]; \endpgfextra}},  ]

\matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
    nodes={mycell}, nodes in empty cells]
{
|[dot2]|&&&|[dot1]|&&\\
&&&&&\\
&&&&|[dot1]|&\\
|[dot1]|&&&&&\\
&&&&&\\
&|[dot3]|&&&&\\
};

\begin{scope}[on background layer]
\path [fill=black!10]
      (m-1-1.north west) -- (m-2-1.south west)
   -- (m-2-2.south east) -- (m-3-2.south east)
   -- (m-3-5.south east) -- (m-2-5.north east)
   -- (m-2-3.north east) -- (m-1-3.north east)
   -- cycle;

\draw (m-1-1.north west) -- (m-2-1.south west)
   -- (m-2-2.south east) -- (m-3-2.south east)
   -- (m-3-5.south east) -- (m-2-5.north east)
   -- (m-2-3.north east) -- (m-1-3.north east)
   -- cycle;
\end{scope}

\end{tikzpicture}

\end{document}

creates

enter image description here

I want the white disc in the m-1-1 position (a dot2) to have a black boundary as in m-6-2 (a dot3). My naive efforts of combining \draw and \fill didn't work. I'm open to entirely different solutions if what I want is tricky with the code I spliced together.

Sebastiano
  • 54,118
Brian Hopkins
  • 549
  • 2
  • 11

1 Answers1

9

To the upvoters: please don't upvote this very trivial answer!

Use \draw[fill=white, thick] instead of the simple \fill[white].

Moreover, you don't need to write the path of your gray area twice, one to fill and one to draw, you can do it with a unique command (I added very thick only to highlight it).

As percusse pointed out in his comment, you can use perpendicular paths (|-) to shorten your code, and, as you realized by yourself, dot3 is useless since you can use dot2 for both cases of white dots.

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{matrix, backgrounds}
\begin{document}

    \begin{tikzpicture}[
        mycell/.style={draw, minimum size=1cm},
        dot1/.style={mycell,
            append after command={\pgfextra \fill (\tikzlastnode) 
                circle[radius=10pt]; \endpgfextra}},
        dot2/.style={mycell,
            append after command={\pgfextra \draw[fill=white, thick] (\tikzlastnode)  
                circle[radius=10pt]; \endpgfextra}}, 
        ]

    \matrix (m) [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
    nodes={mycell}, nodes in empty cells]
    {
        |[dot2]|&&&|[dot1]|&&\\
        &&&&&\\
        &&&&|[dot1]|&\\
        |[dot1]|&&&&&\\
        &&&&&\\
        &|[dot2]|&&&&\\
    };

    \begin{scope}[on background layer]
    \draw[fill=black!10, very thick] (m-1-1.north west) |- (m-2-2.south east) |- (m-3-5.south east) |- (m-2-3.north east) |-cycle;
    \end{scope}

    \end{tikzpicture}

\end{document}

enter image description here

CarLaTeX
  • 62,716
  • 3
    With perpendicular paths you can save some typping: \draw[fill=black!10, very thick] (m-1-1.north west) |- (m-2-2.south east) |- (m-3-5.south east) |- (m-2-3.north east) |-cycle; – Ignasi May 04 '18 at 06:58
  • @CarLaTeX (and @Ignasi) Thanks so much. With the improved dot2 I don't think dot3 is necessary. – Brian Hopkins May 04 '18 at 14:31
  • @BrianHopkins Yes, it is not necessary :) – CarLaTeX May 04 '18 at 14:40