Is it possible to make TikZ matrix cells span multiple rows/columns, like they can in LaTeX tables using \multirow or \multicolumn? Specifically, I'd like to typeset something like the following in a TikZ matrix:

header1  longcell1   longcell2
header2  cellA cellB cellC cellD
header3  X     Y     Z     W

4 Answers 4


A very basic approach could be to use the fit library to create nodes that surround empty matrix cells.


    \matrix (table) [%
      matrix of nodes,
      nodes in empty cells
    ] {%
      A & B & C & D \\
      E &   &   & G \\
      H & I & J &   \\
      K & L & M &   \\

But it first becomes a bit ugly when you e. g. try to fill those cells with a color. So you will need some more adjustments.

  • This worked great for me, thanks. I didn't find filling in with a color to be a problem. I used these in a Beamer frame. I made the text for A, B, C, D, etc. white so it didn't show up on slides before the box got revealed. Then, the box just covered that up. Without Beamer, it wouldn't have been necessary even to make the text white. Commented Jan 22, 2020 at 17:22

Here's a method of doing this that tries to take into account the length of the multicolumn entry. It's not all that sophisticated as all it does is assign a factor of the length of the desired entry to each of the columns (a better approach would be to ensure that the total length was at least the length of the multicolumn entry). It works by hooking in to the \pgfmatrixendcode and \pgfmatrixemptycode macros to adjust their widths accordingly. We have to be a bit sneaky as those macros are executed before the widths are actually known, so what they actually do is set stuff up so that when the widths are known then they are adjusted.

Here's the code:



      \advance\pgfutil@tempdima by -\pgf@picminx
      \divide\pgfutil@tempdima by #1\relax
      \global\advance\multi@col@count by -1\relax

\matrix[matrix of nodes,nodes={draw}] {
A &|[multicol=3]| A long entry spanning three columns &&&  A \\
A B &|[multicol=2]| A shorter entry &&  A & D E F \\
A B & A B & A B C D E F & A B\\

Here's the result:

multi row entries in TikZ matrix

An important thing to note in the code is that the multirow entry is followed by the requisite number of empty entries (which makes me think that this probably wouldn't interact well with a style that put something in every empty node, incidentally).

There are obviously many ways in which this could be improved! And it only works for columns.

Update 2013-04-12 I've been informed (by email) that the following fixes an issue with centring the content in cells. Change the start of the definition of \multi@adjust@position to:

  • Excellent, as usual! It also almost directly works with stuff like the table in the horizontal row separation line in tikz matrix. However, I need to manually specify the width of the combined cell: 0 &|[multicol=2,minimum width=6em]| 6 &\\ , otherwise it's too small. Easily fixed, or too special a case to consider?
    – Jake
    Commented Jun 19, 2011 at 21:37
  • WOW!! Can you tell me how can I learn all internal PGF command?
    – Azoun
    Commented Jun 19, 2011 at 21:40
  • @Jake: I just took a look at that question. I found an issue with your answer there ... Commented Jun 20, 2011 at 7:44
  • 2
    @Azoun: browse the code! And answer questions here. Commented Jun 20, 2011 at 7:45
  • Dear @AndrewStacey, how can I underline multicolumns automatically as I stated in tex.stackexchange.com/questions/113203/…
    – Kadir
    Commented May 8, 2013 at 12:06

You could use the anchors of the matrix to place a multicolumn node over the matrix.

An approach would be to nest matrices, but afaik TikZ doesn't support that yet.


One simple approach (which probably wouldn't work in every case) is to set negative inter-column spacing between the over-size element and the adjacent empty row. This isn't ideal since you have to hand-tune the distance.

\matrix[column sep=1mm,row sep=1mm]{
\node[draw] {header1};  &\node[right,draw] {longcell1};&[-5mm]&   \node[right,draw] {longcell2};&[-5mm]\\
\node[draw] {header2};  &\node[right,draw] {cellA}; &\node[right,draw] {cellB}; &\node[right,draw] {cellC}; &\node[right,draw] {cellD};\\
\node[draw] {header3};  &\node[right,draw] {X};     &\node[right,draw] {Y};     &\node[right,draw] {Z};     &\node[right,draw] {W};\\

enter image description here

You must log in to answer this question.

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