[Tex/LaTex] Alignment problem of empty cells when doing multirow/multicolumn cells in TikZ matrices

tikz-matrixtikz-pgfvertical alignment

This is a question which has been discussed here and here. One of the solution discussed is to use fit to cover empty cells. However, while trying it, it displayed an alignment problem between the empty cells and the others. Here is the MWE from the discussion thread, and where alignment problem is:

\documentclass[xcolor=dvipsnames, 14pt]{beamer}
\usepackage{lmodern}
\usepackage{tikz} 
\usetikzlibrary{matrix, fit}

\begin{document}
\begin{frame}
  \begin{tikzpicture} [
      block/.style ={rectangle
                      %,text width=6em
                      , draw
                      , minimum height=4em
                      , minimum width=4em
                      , outer sep=0pt}
    ]
    \matrix (table) [%
      matrix of nodes
      , nodes in empty cells
      , ampersand replacement=\&
      , nodes=block
    ] {%
      A \& B \& C \& D \\
      E \&   \&   \& G \\
      H \& I \& J \&   \\
      K \& L \& M \&   \\
   };
    %\node[fit=(table-2-2)(table-2-3), inner sep=0pt]{F};
    %\node[fit=(table-3-4)(table-4-4), inner sep=0pt]{N};
  \end{tikzpicture}
\end{frame}
\end{document}

enter image description here

Why do I need multicolumn cells? I actually want to plot the following stacked
enter image description here

I managed to do it in tikz, but the code is very inelegant (xshift, yshfit kind of tweaking). I was hoping Matrix could serve as a better solution, but met the aforementioned alignment issue. The following are the MWE of my tweaking solution:

\documentclass[xcolor=dvipsnames, 14pt]{beamer}
\usepackage{lmodern}
\usepackage{tikz} 
\usetikzlibrary{matrix, fit}

\begin{document}
\begin{frame}

  \centering
  \footnotesize
  \begin{tikzpicture}[
      , every node/.style={
                  , rectangle, rounded corners=1mm  % the shape 
                  , very thick , draw % the border 
                  , fill=orange!50
       }]
    \node [minimum width=.6\textwidth] (a) {AAAA};
    \node [minimum width=.3\textwidth] (b) at (a.north) [xshift=-16mm, yshift=4mm] {B};
    \node [minimum width=.07\textwidth] (c) at (b.east) [xshift=5mm] {CCC};
    \node [minimum width=.20\textwidth] (d) at (c.east) [xshift=12mm] {DDDDD};
  \end{tikzpicture}
\end{frame}

\end{document}

Best Answer

For the alignment issue, you can use the keys text height, text depth, row sep and column sep:

\documentclass[xcolor=dvipsnames, 14pt]{beamer}
\usepackage{lmodern}
\usepackage{tikz} 
\usetikzlibrary{matrix, fit}

\begin{document}
\begin{frame}
  \begin{tikzpicture} [
      block/.style ={rectangle
                      , draw
                      , text height=2em
                      , minimum width=4em,
                      , text depth=1em
                      , outer sep=0pt}
    ]
    \matrix (table) [%
      matrix of nodes
      , nodes in empty cells
      , ampersand replacement=\&
      , nodes=block
      , row sep=-\pgflinewidth
      , column sep=-\pgflinewidth
    ] {%
      A \& B \& C \& D \\
      E \&   \&   \& G \\
      H \& I \& J \&   \\
      K \& L \& M \&   \\
   };
  \end{tikzpicture}
\end{frame}
\end{document}

enter image description here

For the other layout, borrowing some of Jake's code from his answer to Creating a node fitting the horizontal width of two other nodes) to automate the calculations of wider nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,fit}

\definecolor{myorange}{RGB}{250,192,116}

\pgfkeys{
    /tikz/node distance/.append code={
        \pgfkeyssetvalue{/tikz/node distance value}{#1}
    }
}

\newcommand\widernode[5][myorange]{
\node[
        #1,
        inner sep=0pt,
        shift=($(#2.south)-(#2.north)$),
        yshift=-\pgfkeysvalueof{/tikz/node distance value},
        fit={(#2) (#3)},
        line width=1pt,
        label=center:{\sffamily\bfseries#4}] (#5) {};
}

\begin{document}

\begin{tikzpicture}[node distance=5pt,outer sep=0pt,
block/.style={
  line width=1pt,
  draw=black,
  fill=myorange,
  rounded corners,
  text width=#1,
  font={\sffamily\bfseries},
  align=center,
  text height=12pt,
  text depth=9pt
},
block/.default=1.5cm
]
\node[block=3cm] (a) {AAA};
\node[block=4.5cm,right=of a] (b) {BBB};
\node[block,right=of b] (c) {CCC};
\widernode[block]{a}{c}{DDD}{d}
\end{tikzpicture}

\end{document}

enter image description here