# Combining diagonal table cell lines with minimum row clearance

So, yeah, it just never ends I guess. I'm fully aware that the following table is not exactly going to win an award for its stunning beauty no matter what. But the most important problem is that in Column 2, the two rows are looking like one long paragraph instead of two separate table cells.

There's an automatic fix for cases like this which is the \minrowclearance length from the colortbl package:

Much better … except now the diagonal line is broken because the cell height was changed by some amount. I was using the Best Code Currently Out There™. Also, the column headers now seem off-center to me but maybe that's just my eyes and in any case I guess one can't have everything, especially with LaTeX tables. But the diagonal line thing is really ugly so if anybody knows how to fix this without having to manually space the table cells, that'd be incredibly great.

\documentclass[a5paper]{article}
\usepackage{tikz,calc,array,pbox,ragged2e,xcolor,colortbl}
\renewcommand{\familydefault}{\sfdefault}
\newcommand\diag[6]{%
\multicolumn{1}{p{#3}|}{\hskip-\tabcolsep
$\vcenter{\begin{tikzpicture}[baseline=0,anchor=south west,inner sep=#2,color=#1] \path[use as bounding box] (0,0) rectangle (#3+2\tabcolsep,#4); \node[minimum width={#3+2\tabcolsep-\pgflinewidth}, minimum height=#4+\extrarowheight-\pgflinewidth] (box) {}; \draw[line cap=round] (box.north west) -- (box.south east); \node[anchor=south west] at (box.south west) {#5}; \node[anchor=north east] at (box.north east) {#6}; \end{tikzpicture}}$\hskip-\tabcolsep}}
\setlength{\minrowclearance}{.25\baselineskip}
\begin{document}
\begin{table}
\arrayrulecolor{white}
\newlength{\firstcolumnwidth}\settowidth\firstcolumnwidth{Diagonal \hspace{1em} Upper}%
\newlength{\othercolumnwidth}\setlength{\othercolumnwidth}{ ( \tabcolsep * \real{-6.0} + \textwidth - \firstcolumnwidth ) / \real{2.0}}%
\begin{tabular}{>{\color{white}\columncolor{blue}\centering\arraybackslash}m{\firstcolumnwidth}|>{\RaggedRight\arraybackslash}m{\othercolumnwidth}|>{\RaggedRight\arraybackslash}m{\othercolumnwidth}}\hline
\rowcolor{blue}\diag{white}{.1em}{\firstcolumnwidth}{3\baselineskip}{\pbox{\firstcolumnwidth}{Left of\\Diagonal}}{\pbox{\firstcolumnwidth}{Upper\\Half}}&\color{white}Column 1&\color{white}Column 2\\\hline
Row 1&There is lots of text in these table cells&So much in fact, that it's hard to see where one cell ends and the next one begins
\\\hline
Row 2&This can be kinda fixed&But that breaks the diagonal line thing which in turn breaks my sanity\\\hline
\end{tabular}
\caption{Broken Diagonal Table Cell}
\end{table}
\end{document}


\minrowclearance is added above the cell. So simply add it to your box height.

\documentclass[a5paper]{article}
\usepackage{tikz,calc,array,pbox,ragged2e,xcolor,colortbl}
\renewcommand{\familydefault}{\sfdefault}

\newcommand\diag[6]{%
\multicolumn{1}{p{#3}|}{\hskip-\tabcolsep
$\vcenter{\begin{tikzpicture}[baseline=0,anchor=south west,inner sep=#2,color=#1] \path[use as bounding box] (0,0) rectangle (#3+2\tabcolsep,#4); \node[minimum width={#3+2\tabcolsep-\pgflinewidth}, minimum height=#4+\minrowclearance+\extrarowheight-\pgflinewidth] (box) {}; %%%%%%%%%%%%% \draw[line cap=round] (box.north west) -- (box.south east); \node[anchor=south west] at (box.south west) {#5}; \node[anchor=north east] at (box.north east) {#6}; \end{tikzpicture}}$\hskip-\tabcolsep}}
\setlength{\minrowclearance}{0.25\baselineskip}
\begin{document}
\begin{table}
\arrayrulecolor{white}
\newlength{\firstcolumnwidth}\settowidth\firstcolumnwidth{Diagonal \hspace{1em} Upper}%
\newlength{\othercolumnwidth}\setlength{\othercolumnwidth}{ ( \tabcolsep * \real{-6.0} + \textwidth - \firstcolumnwidth ) / \real{2.0}}%
\begin{tabular}{>{\color{white}\columncolor{blue}\centering\arraybackslash}m{\firstcolumnwidth}|>{\RaggedRight\arraybackslash}m{\othercolumnwidth}|>{\RaggedRight\arraybackslash}m{\othercolumnwidth}}\hline
\rowcolor{blue}\diag{white}{.1em}{\firstcolumnwidth}{3\baselineskip}{\pbox{\firstcolumnwidth}{Left of\\Diagonal}}{\pbox{\firstcolumnwidth}{Upper\\Half}}&\color{white}Column 1&\color{white}Column 2\\\hline
Row 1&There is lots of text in these table cells&So much in fact, that it's hard to see where one cell ends and the next one begins
\\\hline
Row 2&This can be kinda fixed&But that breaks the diagonal line thing which in turn breaks my sanity\\\hline
\end{tabular}
\caption{Broken Diagonal Table Cell}
\end{table}
\end{document}

• Isn't it just added sometimes, depending on the cell content? Apr 23, 2013 at 19:42
• I'm not quite sure about it, because I don't use colortbl. It was just a test and it worked. BTW: All cell entries are off center, because the center is not calculated with \minrowclearance in mind. I guess this is by design, because it shall create space between cells and not inside the cells. Nonetheless, it's behaviour should be different for m columns. Apr 23, 2013 at 19:52
• I guess always adding \minrowclearance won't do any harm as the diagonal cell will probably always trigger the whole row clearance to be added. Apr 24, 2013 at 12:42

Here is what you can do with {NiceTabular} of nicematrix. That environment (similar to the classical environments {tabular} and {tabularx}) has a built-in command \diagbox and a key cell-space-limits.

\documentclass[a5paper]{article}
\renewcommand{\familydefault}{\sfdefault}

\usepackage{nicematrix}

\begin{document}

\begin{table}

\begin{NiceTabular}
[
rules/color=white,
hvlines-except-borders,
cell-space-limits=3pt
]
{>{\color{white}}w{c}{3cm}X[l,m]X[l,m]}
\CodeBefore
\rowcolor{blue}{1}
\columncolor{blue}{1}
\Body
\rule[-6mm]{0pt}{16mm}
\RowStyle[color=white]{}
\diagbox
{\color{white}Left of\\Diagonal}
{\color{white}Upper\\Half}
& Column 1& Column 2 \\
Row 1 & There is lots of text in these table cells
& So much in fact, that it's hard to see where one cell ends and the next one begins \\
Row 2 & This can be kinda fixed & But that breaks the diagonal line thing which in turn breaks my sanity \\
\end{NiceTabular}
\caption{Broken Diagonal Table Cell}
\end{table}

\end{document}


You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).