4

could not figure out an answer to this one.

I have a tikz diagram using tikz/matrix, and I want to have a legend next to it, also using tikz/matrix. But I do not know how to refer to one particular matrix in the style definitons.

Let me visually show the problem:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{
    positioning,
    shapes.geometric,
    matrix}

\begin{document}

\begin{tikzpicture}
\tikzset{
    status/.style={rectangle, draw=black, text centered, anchor=north, text=black, minimum width=2em, minimum height=2em},
    mstyle/.style={column sep=10em, row sep=3em,nodes={status},font=\bfseries},
    line/.style={draw,thick,-latex},
    row 1 column 1/.style={nodes={fill=green}},
    row 1 column 2/.style={nodes={fill=red}},
    row 1 column 3/.style={nodes={fill=yellow}},
    row 2 column 3/.style={nodes={fill=gray}},
    row 2 column 2/.style={nodes={diamond}},
}
\matrix(m)[matrix of nodes,ampersand replacement=\&,mstyle]{
    H \& A1 \& A2 \\
      \& {} \& D \\
};
\tikzset{
    status/.style={rectangle, draw=black, text centered, anchor=north, text=black, minimum width=2em, minimum height=2em},
    m2style/.style={column sep=1em, row sep=1em,nodes={status},font=\bfseries},
}
\matrix(m2)[right=of m, draw=black, matrix of nodes,ampersand replacement=\&,m2style]{
    H  \& Healthy \\
    A1 \& Infected  \\
    A2 \& Infected  \\
    D \& Death  \\
};
\draw[line] (m-1-1) -- (m-1-2) node[pos=0.5,above,align=left] {$\#\mathrm{A1} \geq 1$ or\\$\#\mathrm{A2} \geq R$};
\draw[line] (m-1-2) -- (m-1-3) node[pos=0.5,above] {wait $\tau$ ticks};
\draw[line] (m-1-3) -- (m-2-3) node[pos=0.5,right] {wait 1 tick};
\draw[line] (m-2-3) -- (m-2-2) node[pos=0.5,above] {$p_\mathrm{replenished}$};
\draw[line] (m-2-2) -| (m-1-1) node[pos=0.2,above] {$1-p_\mathrm{infected}$};
\draw[line] (m-2-2) -- (m-1-2) node[pos=0.5,left] {$p_\mathrm{infected}$};
\end{tikzpicture}

\end{document}

I want tikzset to be limited to the scope of the first matrix m ....

screenshot

Ricardo Cruz
  • 1,770
  • 1
  • 22
  • 34

1 Answers1

4

I am not sure if I understood the question correctly. You can give your options to a particular matrix in several ways: using a scope environment:

\begin{scope}[<options>]
<code for the matrix>
\end{scope}

or passing the options to the optional argument for the particular matrix:

\matrix[<options>]
<code for the matrix>

The general options can be passed to the tikzpicture environment or using a \tikzset command.

Here's a modified version of your code that I think produces the result you are trying to achieve:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
    positioning,
    calc,
    shapes.geometric,
    matrix}

\begin{document}

\begin{tikzpicture}[ line/.style={ draw, thick, -latex }, status/.style={ rectangle, draw=black, text centered, anchor=north, text=black, minimum width=2em, minimum height=2em }, m2style/.style={ column sep=1em, row sep=1em, nodes={status}, font=\bfseries }, mstyle/.style={ column sep=10em, row sep=3em, nodes={status}, font=\bfseries } ]

\matrix (m) [ matrix of nodes, ampersand replacement=&amp;, mstyle, row 1 column 1/.style={nodes={fill=green}}, row 1 column 2/.style={nodes={fill=red}}, row 1 column 3/.style={nodes={fill=yellow}}, row 2 column 3/.style={nodes={fill=gray}}, row 2 column 2/.style={nodes={diamond}} ]{ H &amp; A1 &amp; A2 \ &amp; {} &amp; D \ };

\matrix (m2) [ right=1.8cm of m, draw=black, matrix of nodes, ampersand replacement=&amp;, m2style, column 2/.append style={nodes={text width=1.5cm}}, ]{ |[fill=green]|H &amp; Healthy \ |[fill=red]|A1 &amp; Infected \ |[fill=yellow]|A2 &amp; Infected \ |[fill=gray]|D &amp; Death \ };

\draw[line] (m-1-1) -- (m-1-2) node[pos=0.5,above,align=left] {$#\mathrm{A1} \geq 1$ or\$#\mathrm{A2} \geq R$}; \draw[line] (m-1-2) -- (m-1-3) node[pos=0.5,above] {wait $\tau$ ticks}; \draw[line] (m-1-3) -- (m-2-3) node[pos=0.5,right] {wait 1 tick}; \draw[line] (m-2-3) -- (m-2-2) node[pos=0.5,above] {$p_\mathrm{replenished}$}; \draw[line] (m-2-2) -| (m-1-1) node[pos=0.2,above] {$1-p_\mathrm{infected}$}; \draw[line] (m-2-2) -- (m-1-2) node[pos=0.5,left] {$p_\mathrm{infected}$};

\path let \p1=(m2.east), \p2=(m2.west) in node[align=center,text width=\x1-\x2,anchor=south,inner sep=0pt] at ([yshift=5pt]m2.north) {The title for the legend box}; \end{tikzpicture}

\end{document}

enter image description here

Remark

After a comment, a title was added to the legend box. The title was set in a node whose width is automatically calculated to be equal to the width of the matrix for the legend (this requires the calc library).

In case the title should be inside the legend box, I'd suggest another approach not using a matrix of nodes for the legends, but simple nodes and the fit library:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{
    positioning,
    calc,
    fit,
    shapes.geometric,
    matrix}

\begin{document}

\begin{tikzpicture}[ line/.style={ draw, thick, -latex }, status/.style={ rectangle, draw=black, text centered, anchor=north, text=black, minimum width=2em, minimum height=2em }, m2style/.style={ status, font=\bfseries }, mstyle/.style={ column sep=10em, row sep=3em, nodes={status}, font=\bfseries } ]

\matrix (m) [ matrix of nodes, ampersand replacement=&amp;, mstyle, row 1 column 1/.style={nodes={fill=green}}, row 1 column 2/.style={nodes={fill=red}}, row 1 column 3/.style={nodes={fill=yellow}}, row 2 column 3/.style={nodes={fill=gray}}, row 2 column 2/.style={nodes={diamond}} ]{ H &amp; A1 &amp; A2 \ &amp; {} &amp; D \ };

\begin{scope}[ node distance=1em and 1em, mytext/.style={m2style,text width=1.6cm} ] \node[m2style,fill=green,right=2cm of m.north east] (H) {H}; \node[m2style,fill=red,below=of H] (A1) {A1}; \node[m2style,fill=yellow,below=of A1] (A2) {A2}; \node[m2style,fill=gray,below=of A2] (D) {D}; \node[mytext,right=of H] (he) {Healthy}; \node[mytext,right=of A1] (in) {Infected}; \node[mytext,right=of A2] (inf) {Infected}; \node[mytext,right=of D] (de) {Death}; \path let \p2=(H.west), \p1=(he.east) in node[align=center,text width=\x1-\x2,anchor=south west,inner sep=0pt] (title) at ([yshift=5pt]H.north west) {The title for the legend box}; \end{scope}

\node[draw,fit={(title) (D) (de)}] {};

\draw[line] (m-1-1) -- (m-1-2) node[pos=0.5,above,align=left] {$#\mathrm{A1} \geq 1$ or\$#\mathrm{A2} \geq R$}; \draw[line] (m-1-2) -- (m-1-3) node[pos=0.5,above] {wait $\tau$ ticks}; \draw[line] (m-1-3) -- (m-2-3) node[pos=0.5,right] {wait 1 tick}; \draw[line] (m-2-3) -- (m-2-2) node[pos=0.5,above] {$p_\mathrm{replenished}$}; \draw[line] (m-2-2) -| (m-1-1) node[pos=0.2,above] {$1-p_\mathrm{infected}$}; \draw[line] (m-2-2) -- (m-1-2) node[pos=0.5,left] {$p_\mathrm{infected}$};

\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • Interesting. How would you suggest I add title within the legend box? – Ricardo Cruz Jun 23 '15 at 14:09
  • @RicardoCruz Please see my updated answer. If you want the title inside the box for the legend, let me know so I can suggest another approach. – Gonzalo Medina Jun 23 '15 at 14:57
  • @RicardoCruz In the meantime, I added to my answer a new sapproach; this time the title is inside the legend box. – Gonzalo Medina Jun 23 '15 at 15:12
  • Yes, the legend at the right of the diagram with the title inside it (like in a graph) is exactly what I was looking for. Thanks. Will change my question title to make it easier to find for others ...! – Ricardo Cruz Jun 24 '15 at 14:30