2

I am using tikz matrix to generate shapes of shaded cells. I have the following code working to generates the shape I am looking for in this example. It is just a matrix with some defined fills and patternfills.

\documentclass[11pt]{article}

\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{matrix,patterns,shadings,backgrounds}
\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}

\tikzset{myfillcolor/.style = {draw,fill=#1}}%

\NewDocumentCommand{\highlight}{O{black!20} m m}{%
\draw[myfillcolor=#1] (#2.north west)rectangle (#3.south east);
}

\NewDocumentCommand{\fillpatternh}{O{horizontal lines} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}

\NewDocumentCommand{\fillpatternch}{O{crosshatch} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}


\begin{document}

\begin{figure}

\begin{tikzpicture}
\matrix (m)[matrix of nodes, style={nodes={minimum width=4em}}, minimum height=2em, ampersand replacement =\&]% row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
{
{} \& {}  \& {}  \\
{} \& {}  \& {} \\
};

\begin{pgfonlayer}{background}

\fillpatternh{m-1-1}{m-1-1}%
\highlight{m-2-1}{m-2-1}%
\fillpatternch{m-1-2}{m-2-3}
\highlight{m-1-2}{m-1-2}
\end{pgfonlayer}
\end{tikzpicture}

\end{figure}

\end{document}

In addition to the above, I wish to also generate a shape as per the following: paradigmshape

However, I cannot generate these 'L' shaped shapes that have an outline. All my examples require overlapping or multiple shapes which have rectangular outlines. Could anybody suggest a method to generate the type of shape in the picture?

Thanks

2 Answers 2

2

Based on your code, declare a larger matrix and fill the corresponding area.

\draw[pattern=north west lines, pattern color=black!20] (m-4-2.north west) -| 
       (m-5-3.north west) -| (m-6-3.south east)-| (m-5-2.south east) -| cycle;

About your code: if you add nodes in empty nodes, there is no need for {} inside every matrix element.

\documentclass[11pt]{article}

\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{matrix,patterns,shadings,backgrounds}
\pgfdeclarelayer{myback}
\pgfsetlayers{myback,background,main}

\tikzset{myfillcolor/.style = {draw,fill=#1}}%

\NewDocumentCommand{\highlight}{O{black!20} m m}{%
\draw[myfillcolor=#1] (#2.north west)rectangle (#3.south east);
}

\NewDocumentCommand{\fillpatternh}{O{horizontal lines} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}

\NewDocumentCommand{\fillpatternch}{O{crosshatch} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west) rectangle (#4.south east);
}

\NewDocumentCommand{\fillpatternw}{O{north west lines} O{black!20} m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west)rectangle (#4.south east);
}

\begin{document}

\begin{figure}

\begin{tikzpicture}
\matrix (m)[matrix of nodes, style={nodes={minimum width=4em}}, minimum height=2em, ampersand replacement =\&]% row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
{
{} \& {}  \& {}  \\
{} \& {}  \& {} \\
};

\begin{pgfonlayer}{background}
\fillpatternh{m-1-1}{m-1-1}%
\highlight{m-2-1}{m-2-1}%
\fillpatternch{m-1-2}{m-2-3}
\highlight{m-1-2}{m-1-2}
\end{pgfonlayer}
\end{tikzpicture}

\begin{tikzpicture}
\matrix (m)[matrix of nodes, nodes in empty cells, style={nodes={minimum width=4em}}, minimum height=2em, ampersand replacement =\&]% row sep=-\pgflinewidth, column sep=-\pgflinewidth, 
{
 \&  \& \\
 \&  \& \\
 \&  \& \\
 \&  \& \\
 \&  \& \\
 \&  \& \\
 \&  \& \\
 \&  \& \\
};

\begin{pgfonlayer}{background}
\highlight{m-6-2}{m-6-2}%
\highlight{m-7-3}{m-7-3}
\fillpatternh{m-1-1}{m-3-2}%
\fillpatternh{m-5-1}{m-7-1}%
\fillpatternw{m-7-2}{m-8-2}%
\draw[pattern=north west lines, pattern color=black!20] (m-4-2.north west) -| (m-5-3.north west) -| (m-6-3.south east)-| (m-5-2.south east) -| cycle;
\end{pgfonlayer}
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

Update:

If all this strange figure are always similar, you can declare a command with four parameters, top left node, middle top, right bottom, middle lower.

\NewDocumentCommand{\fillfourpatternw}{O{north west lines} O{black!20} m m m m}{%
\draw[pattern=#1, pattern color=#2] (#3.north west) -| (#4.north west) -| (#5.south east) -| (#6.south east) -| cycle;
}

which in this particular case would be:

\fillfourpatternw{m-4-2}{m-5-3}{m-6-3}{m-5-2}
1
  • Excellent, this is just what I was looking for. Thank you. Aug 10, 2016 at 12:55
1

There are probably smarter ways to do this, but here's my shot:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{backgrounds,matrix}

\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of nodes, style={nodes={minimum width=4em}}, minimum height=2em] {
1-1 & 1-2 & 1-3 & 1-4 & 1-5 \\
2-1 & 2-2 & 2-3 & 2-4 & 2-5 \\
3-1 & 3-2 & 3-3 & 3-4 & 3-5 \\
};

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

enter image description here

1
  • Thanks for this. It looks like it should do what I need. Thank you. Aug 10, 2016 at 12:05

You must log in to answer this question.

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