# How do I make a TikZ matrix match height?

I'm trying to make a table using the matrix TikZlibrary. The problem is that the height does not match.

\documentclass[tikz,border=2pt]{standalone}
\usetikzlibrary{matrix}
\usepackage[utf8]{inputenc}

\begin{document}
\begin{tikzpicture}

\matrix (M) [
matrix of nodes,
column sep=0mm,
row sep=0cm,
nodes={
draw,
line width=1pt,
anchor=south,
minimum height=8mm
},
column 1/.style={
nodes={
text width=3cm,
minimum width=3cm,
fill=yellow
}
},
column 2/.style={
nodes={
text width=4cm,
minimum width=4cm,
}
}
]{
Short text & Longer text. Spanning over several lines \\
Short text & Another long text. Spanning over even more lines...\\
};

\end{tikzpicture}
\end{document}

-

You can set the minimum height of the nodes to a value that is (at least) as large as the highest node. 16mm is sufficient for this example.

If you want different heights for different rows, you can use the row n styles, similar to the column n styles you've used in your example. For example

    row 1/.style={
nodes={
minimum height=10mm
}
},


Note that you should add these before the column n styles.

Another thing to note is that the lines around each cell doesn't overlap, giving lines of double width between cells. Set the row sep and col sep to -1pt to fix this.

You can of course use a tabular instead. That the lines in the table sometimes disappear is usually just a viewer issue -- the lines are there, but the coloured fields are snapped to pixel boundaries, which can cover the lines (see e.g. Missing lines when shading cells). If you zoom in, you'll probably see the lines, which should also appear in print.

In the below code I increased the width of the table rules a bit. (cf. Nice-looking tables with thick rules)

\documentclass{article}
\usepackage[table]{xcolor}
\setlength{\arrayrulewidth}{1pt}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tabular}{|>{\cellcolor{yellow}}m{3cm} | >{\raggedright\arraybackslash} m{4cm} |}
\hline
Short text & Longer text. Spanning over several lines \\ \hline
Short text & Another long text. Spanning over even more lines... \\ \hline
\end{tabular}

\begin{tikzpicture}

\matrix (M) [
matrix of nodes,
column sep=0mm,
row sep=0cm,
nodes={
draw,
line width=1pt,
anchor=south,
},
row 1/.style={
nodes={
minimum height=10mm
}
},
row 2/.style={
nodes={
minimum height=20mm
}
},
column 1/.style={
nodes={
text width=3cm,
minimum width=3cm,
fill=yellow
}
},
column 2/.style={
nodes={
text width=4cm,
minimum width=4cm,
}
}
]{
Short text & Longer text. Spanning over several lines \\
Short text & Another long text. Spanning over even more lines...\\
};

\end{tikzpicture}
\end{document}

-
Thanks, this will work, but say I wanted to have a more arbitrary height. Is that possible? –  David Kristiansen Apr 20 at 17:30
@davidAK What do you mean by 'arbitrary'? –  Torbjørn T. Apr 20 at 18:12
What I mean is that the right side column has a variable height, and I think it will look nicer when the left column matches that. Now, I could have done this with the tabular environment, but the cellcolor would overwrite the black lines. So I thought I that TikZ would be a better option. –  David Kristiansen Apr 20 at 18:31
@davidAK See my updated answer. Usually a cellcolor does not overwrite the lines, it is just a viewer issue. –  Torbjørn T. Apr 20 at 19:01
Thank you for taking your time. This is exactly what I'm looking for. –  David Kristiansen Apr 20 at 19:25