In a TikZ matrix, how can I underline multicolumns automatically as follows (esp. when height of one header cell is larger than others):

MWE from this question:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\def\totcvtext{$\begin{array}{c}{{\rm tot}\ {\rm comm}}\\[-.8ex]{\rm vol (words)}\end{array}$}
\makeatletter
\newdimen\multi@col@width
\newdimen\multi@col@margin
\newcount\multi@col@count
\multi@col@width=0pt
\tikzset{
multicol/.code={%
\global\multi@col@count=#1\relax
\global\let\orig@pgfmatrixendcode=\pgfmatrixendcode
\global\let\orig@pgfmatrixemptycode=\pgfmatrixemptycode
\def\pgfmatrixendcode##1{\orig@pgfmatrixendcode%
##1%
\pgfutil@tempdima=\pgf@picmaxx
\global\multi@col@margin=\pgf@picminx
\advance\pgfutil@tempdima by -\pgf@picminx
\divide\pgfutil@tempdima by #1\relax
\global\multi@col@width=\pgfutil@tempdima
\pgf@picmaxx=.5\multi@col@width
\pgf@picminx=-.5\multi@col@width
\global\pgf@picmaxx=\pgf@picmaxx
\global\pgf@picminx=\pgf@picminx
\gdef\multi@adjust@position{%
\setbox\pgf@matrix@cell=\hbox\bgroup
\hfil\hskip-1.5\multi@col@margin
\hfil\hskip-.5\multi@col@width
% \gdef\multi@adjust@position{%
% \setbox\pgf@matrix@cell=\hbox\bgroup
% \hfil\hskip-\multi@col@margin
% \hfil\hskip-.5\multi@col@width
\box\pgf@matrix@cell
\egroup
}%
\gdef\multi@temp{\aftergroup\multi@adjust@position}%
\aftergroup\multi@temp
}
\gdef\pgfmatrixemptycode{%
\orig@pgfmatrixemptycode
\global\advance\multi@col@count by -1\relax
\global\pgf@picmaxx=.5\multi@col@width
\global\pgf@picminx=-.5\multi@col@width
\ifnum\multi@col@count=1\relax
\global\let\pgfmatrixemptycode=\orig@pgfmatrixemptycode
\fi
}
}
}
\makeatother
\begin{document}
\begin{tikzpicture}
\matrix (mat) [matrix of nodes] {
L2A &|[multicol=2]| L2B && |[multicol=2]| L2C & \\
L3A & L3B & L3C & \totcvtext & L3E \\
10 & 20 & 30 & 40 & 50 \\
};
\draw[thick,black,shorten >=0.5mm,shorten <=0.5mm] (mat-2-2.north west) -- (mat-2-3.north east);
\draw[thick,black,shorten >=0.5mm,shorten <=0.5mm] (mat-2-4.north west) -- (mat-2-5.north east);
\end{tikzpicture}
\end{document}
Note that height of other cells containing numbers should not be changed.



|[multicol=2]|? You don't want to have to type\draw[thick,black] ([shift={(1.1,1.1)}]mat.south west) -- ([shift={(2.8,1.1)}]mat.south west);each time, right? – John Wickerson May 08 '13 at 12:11\draw[thick,black] (mat-2-2.north west) -- (mat-2-4.north west);then you avoid your magic numbers. – John Wickerson May 08 '13 at 12:28(mat-2-2.north west) -- (mat-2-4.north west);is good. However, there will be problem when there are two multicolumns side by side, the two lines should not be combined, there must be a little space. So again I have to use magic numbers to shift lines. If it is possible to underline multicolumn via using only|[multicol=2]|or additionally with a letter for indicating underline operation, it will be great!!! – Kadir May 08 '13 at 15:57\draw[thick,black,shorten >=...,shorten <=...] (mat-2-2.north west)--(mat-2-4.north west);where...is some value (3pt, 1mm, ...). Your line will start this distance aftermat-2-2.north westand finish this distance earlier thanmat-2-2.north west. – Ignasi May 08 '13 at 16:41