10

I am trying to insert a multirow in a tikz matrix with a grid, so:

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

\begin{document} 
\begin{tikzpicture}
\matrix (table) [%
  matrix of nodes,
  nodes in empty cells,
  draw,
  inner sep=0mm,
  minimum size=7mm
  ] {%
  \node[draw] {A}; & \node[draw] {B}; & \node[draw] {C}; & \node[draw] {D}; \\
  \node[draw] {E}; &   &   & \node[draw] {G}; \\   };
\node[draw,fit=(table-2-2)(table-2-3)]{F};
\end{tikzpicture}
\end{document}

With ugly results. Is there a way to fix this? A better way to implement it? Thanks.

David Carlisle
  • 757,742
Noam
  • 101

1 Answers1

9

You have better control over the borders of your cells if you explicitly set the text height and depth. Furthermore, I would use styles to keep the code more maintainable. Here's an example of how your table could be implemented, based on the code used in " coloring every other row of a table, with vertical lines ".

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

\tikzset{
    table nodes/.style={
        rectangle,
        draw=black,
        align=center,
        minimum height=7mm,
        text depth=0.5ex,
        text height=2ex,
        inner xsep=0pt,
        outer sep=0pt
    },      
    table/.style={
        matrix of nodes,
        row sep=-\pgflinewidth,
        column sep=-\pgflinewidth,
        nodes={
            table nodes
        },
        execute at empty cell={\node[draw=none]{};}
    }
}
\begin{tikzpicture}

\matrix (first) [table,text width=7mm,name=table]
{
A   & B & C & D\\
E   &   &   & F\\
};
\node[draw,fit=(table-2-2)(table-2-3),table nodes]{F};


\end{tikzpicture}
\end{document}

table with tikz

David Carlisle
  • 757,742
Jake
  • 232,450
  • 1
    Can TikZ tables work like longtable which can span across pages? – Display Name Jul 10 '11 at 05:04
  • 3
    @xport: I don't think tikzpicture environments can span across pages, so TikZ matrices can't either. They're really not meant as a replacement for real LaTeX tables anyway, so you shouldn't expect too much from this approach. It can be useful if you're trying to do something special, most likely if you also need to place other TikZ elements. – Jake Jul 10 '11 at 05:43
  • 1
    Is there an update needed here, it no longer works as shown. – Sal Jr Feb 15 '19 at 18:08
  • 4
    @Jake, This doesn't seem to work anymore. I get: Package pgf Error: No shape named 'table-2-2' is known. Do you know how to fix this? – evolving Jan 28 '20 at 11:48
  • @evolving and @Jake, the issue seems to be with the execute at empty cell={\node[draw=none]{};} which seems to deactivate the auto-naming of nodes within the matrix. Using nodes in empty cells instead seems to make it work again. – Hans Jan 17 '22 at 14:25
  • Ooops, this is only half the story, using nodes in empty cells works but introduces nodes with draw. A possible fix for this is given in https://tex.stackexchange.com/a/386805/21773. – Hans Jan 17 '22 at 14:38
  • if anyone has a solution to the current problems of this answer, that would be amazing! – Marine Galantin Jan 25 '22 at 18:22