Graphics, Figures & Tablesmultirow/multicolumn Cells in tikZ Matrices

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

multirow/multicolumn Cells in tikZ Matrices

Post by usr345 »

The question was asked on {TeX} SE.

Stefan said: "You could use the anchors of the matrix to place a multicolumn node over the matrix."

I don't understand, how to do it. Stefan, can you give and example and explain better?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

multirow/multicolumn Cells in tikZ Matrices

Post by localghost »

If you don't mind I will give the same answer as I recently did there (brought to my attention by this question here).

A basic structure can be built with the »fit« library to create nodes that surround empty cells in the matrix.

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit,matrix}

\begin{document}
  \begin{tikzpicture}
    \matrix (table) [%
      matrix of nodes,
      nodes in empty cells
    ] {%
      A & B & C & D \\
      E &   &   & G \\
      H & I & J &   \\
      K & L & M &   \\
   };
    \node[fit=(table-2-2)(table-2-3)]{F};
    \node[fit=(table-3-4)(table-4-4)]{N};
  \end{tikzpicture}
\end{document}
Further adjustments are necessary if you try to fill or frame these cells.


Thorsten
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

multirow/multicolumn Cells in tikZ Matrices

Post by usr345 »

Thanks. In fact I wanted the header, containing text: "Moose Attributes" to be located above the 2'nd column of the matrix 1cm wider, then the column width. But now the header has stretched along all the matrix.

Or it is impossible to do with matrix?

Code: Select all

\documentclass{article}
\usepackage[a3paper]{geometry}
\usepackage[cm]{fullpage}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{verbatim}
\usepackage{color}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,backgrounds,calc, positioning, fit, matrix}
\begin{document}
\definecolor{delicate-color}{HTML}{5CB3FF}
\pagecolor{delicate-color}

\begin{tikzpicture}
%{{{ tikz styles
  \tikzset{
    header/.style={ellipse, draw=black, top color=white, bottom color=yellow!50,thick, inner sep=3mm, minimum width=10cm, minimum height=2cm, font=\Large\bfseries, text centered, double, anchor=west},
    pointer/.style = {signal, signal to=east, anchor=west, text width = 40mm, fill=white, line width=0.4mm},
    mnemonic-block/.style={rectangle, anchor=west, inner sep=2mm, text width = 9.6cm, fill=white, line width=0.4mm},
    line/.style = {draw, line width=0.3mm},
    comment/.style = {rectangle, rounded corners, draw, inner sep=3mm, anchor=west, text width = 8cm, node distance=1.5cm, fill=white, line width=0.4mm},
  }
%}}}
\matrix (table) [
    matrix of nodes,
    nodes in empty cells,
	nodes={draw}, row  sep=5mm,
    column sep = 15mm,minimum width=.875cm, minimum height=1cm,
    ]
{
& &\\
\node [pointer,yshift=-3cm] (3_1) {read-write / read only}; & 
	\node [mnemonic-block,yshift=-3cm] (3_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {is => 'rw'}
has 'attr' => {is => 'ro'}
\end{lstlisting}};\\\node [pointer] (4_1) {Accessor methods}; & 
	\node [mnemonic-block] (4_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {reader => 'get_attr',}
has 'attr' => {writer => 'set_attr',}
\end{lstlisting}};\\\node [pointer] (5_1) {Predicate method}; & 
	\node [mnemonic-block] (5_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {predicate => 'has_attr'}
\end{lstlisting}
		}; & 
	\node [comment] (5_3) {Tells you whether or not a given attribute is currently set};\\\node [pointer] (6_1) {Cleaner method}; & 
	\node [mnemonic-block] (6_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {cleaner => 'clean_attr'}
\end{lstlisting}}; & 
	\node [comment] (6_3) {Unsets the attribute};\\\node [pointer] (7_1) {Required}; & 
	\node [mnemonic-block] (7_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {required => 1}
\end{lstlisting}};\\\node [pointer] (8_1) {\pgftext[x=1cm] {Lazy}}; & 
	\node [mnemonic-block] (8_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {lazy => 1}
\end{lstlisting}
		}; & 
	\node [comment] (8_3) {When lazy is true, the default is not generated until the reader method is called};\\\node [pointer] (9_1) {Constructor parameters}; & 
	\node [mnemonic-block] (9_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {init_arg => 'new_name'}
\end{lstlisting}
	}; & 
	\node [comment] (9_3) {
		Use a different name for the constructor parameter for this attribute.
	};\\\node [pointer] (10_1) {Weak references}; & 
	\node [mnemonic-block] (10_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {weak_ref => 1}
\end{lstlisting}
	}; & 
	\node [comment] (10_3) {
		If you set the weak\_ref option to a true value, then it will call Scalar::Util::weaken whenever the attribute is set
	};\\\node [pointer] (11_1) {Trigger}; & 
	\node [mnemonic-block] (11_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {trigger => \&attr_set}
\end{lstlisting}
	}; & 
	\node [comment] (11_3) {subroutine that is called whenever the attribute is set};\\\node [pointer] (12_1) {Attribute type}; & 
	\node [mnemonic-block] (12_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {isa => 'Str',}
has 'attr' => {isa => 'MyApp::Weapon',}
\end{lstlisting}
	};\\\node [pointer] (13_1) {Documentation}; & 
	\node [mnemonic-block] (13_2) {\begin{lstlisting}[language=Perl]
has 'attr' => {documentation => q{Some text}}
\end{lstlisting}
		};\\
};
\node [header, align=center, fit=(table-1-1)(table-1-2)(table-1-3)] (1_1) {Moose Attributes};

\draw[line] (3_1.west) --++ (-5mm, 0) |- (4_1.west) --++ (-5mm, 0) |- (5_1.west) --++ (-5mm, 0) |- (6_1.west) --++ (-5mm, 0) |- (7_1.west) --++ (-5mm, 0) |- (8_1.west) --++ (-5mm, 0) |- (9_1.west) --++ (-5mm, 0) |- (10_1.west) --++ (-5mm, 0) |- (11_1.west) --++ (-5mm, 0) |- (12_1.west) --++ (-5mm, 0) |- (13_1.west);\draw[line] (3_1.east) -- (3_2.west);\draw[line] (4_1.east) -- (4_2.west);\draw[line] (5_1.east) -- (5_2.west);\draw[line] (5_2.east) -- (5_3.west);\draw[line] (6_1.east) -- (6_2.west);\draw[line] (6_2.east) -- (6_3.west);\draw[line] (7_1.east) -- (7_2.west);\draw[line] (8_1.east) -- (8_2.west);\draw[line] (8_2.east) -- (8_3.west);\draw[line] (9_1.east) -- (9_2.west);\draw[line] (9_2.east) -- (9_3.west);\draw[line] (10_1.east) -- (10_2.west);\draw[line] (10_2.east) -- (10_3.west);\draw[line] (11_1.east) -- (11_2.west);\draw[line] (11_2.east) -- (11_3.west);\draw[line] (12_1.east) -- (12_2.west);\draw[line] (13_1.east) -- (13_2.west);
\draw[line] (3_1.west) --++ (-5mm, 0) |- (1_1.west);
\end{tikzpicture}
\end{document}
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

multirow/multicolumn Cells in tikZ Matrices

Post by localghost »

This seems to be just another description for an older problem [1]. But the problem there is solved. So, please clarify where the difference is between the problem here and there.

[1] View topic: Centering a node in tikz
usr345
Posts: 37
Joined: Fri Apr 01, 2011 11:39 pm

multirow/multicolumn Cells in tikZ Matrices

Post by usr345 »

The difference is: there I moved a node into cell[1,2] of matrix. But now I want to make it wider to emphasize, that this is the header of the whole diagram. To get something like this:

Image

But then it will stretch the center column, that I don't want.

And when I tried your solution with fit lib, it gave this:

Image
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

multirow/multicolumn Cells in tikZ Matrices

Post by localghost »

You are already loading the »positioning« library but as far as I can see you don't use it. The pgf/tikZ user guide has some examples of how to position nodes relatively to others. I think this will remedy the issue. Probably needs to rewrite a bit of code.
Post Reply