1

Consider the following MWE

\documentclass{standalone}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{hhline}
\newcommand{\firstrow}[1]{%
  \multicolumn{2}{|c|}{\cellcolor{blue!25} #1}
}
\newcolumntype{C}{>{\cellcolor{blue!50} }c<{}}

\begin{document}
\renewcommand{\arraystretch}{2}
  \begin{tabular}{|C|C|c|C|C|}
    \hhline{--~--}
    \firstrow{R1C1--2}&&\firstrow{R1C4--5} \\ 
    \hhline{--~--}
    \hhline{==~~~}
    R2C1 & R2C2 & \multicolumn{3}{c}{\strut} \\
    \hhline{--~--}
    R3C1 & R3C2 && R3C4 &  R3C5  \\ 
    \hhline{--~--}
    R4C1 &  R4C2  &&  \multirow{2}{*}{R4--5C4} & \multirow{2}{*}{R4--5C5} \\ 
    \hhline{--~~~}
    R5C1 & R5C2 &&&  \\ 
    \hhline{--~--}
    R6C1 & R6C2 && R6C5 &  R6C5                                    
    \\ 
    \hhline{--~--}
  \end{tabular}
\end{document}

which results in

enter image description here

There are some problems:

  1. corners between columns 3 and 4 are not well drawn;
  2. background color of cell R5C4-5 overrides the content of cells R4C4-5;
  3. the horizontal line between R4C4-5 and R5C4-5 should not be there;
  4. the cell borders don't look uniform around all the cells but I can't understand if it is a graphic problem of the figure viewed on the monitor or a real issue;
  5. minor issue: column 3 is meant to separate columns 1-2 from columns 4-5; it would be nice to have control on its width (I would prefer to have it narrower than it is).

Problem 3 is the same as in this question but I couldn't figure out the solution there because the code there is too messy.

2
  • For the problem 4: The construction of tabulars with colors as done by array and colortbl (loaded by the key table of xcolor) writes in the PDF an alternance of instructions for the colored panels and the rules. Most PDF viewers have difficulties with this kind of description because they give priority, during the rasterization, to the elements drawn after. With the nicematrix, you won't have those problems because all the rules are drawn after the colored panels by using Tikz. Commented Aug 11, 2022 at 19:22
  • @F.Pantigny : yes, I am aware of this. It results that in this case it was an actual issue, due to my bad use of the hhline package, see my own answer which I edited. In any case, thank you for your attention.
    – brad
    Commented Aug 12, 2022 at 8:58

2 Answers 2

1

Here is a way to construct that table with {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{cc>{\hspace*{-2mm}}ccc}[cell-space-limits=2mm]
\Block[draw,fill=blue!50]{1-2}{R1C1-2} & & & \Block[draw,fill=blue!50]{1-2}{R1C4-5} \\
\noalign{\vspace{-2mm}}
\\
\Block[hvlines,fill=blue!25]{5-2}{}
R2C1 & R2C2 \\
R3C1 & R3C2 & & \Block[hvlines,fill=blue!25]{4-2}{} 
                R3C4                & R3C5 \\
R4C1 & R4C2 & & \Block{2-1}{R4-5C4} & \Block{2-1}{R4-5C5} \\
R5C1 & R5C2 \\
R6C1 & R6C2 & & R6C4 & R6C5 
\end{NiceTabular}

\end{document}

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

Output of the above code

1
  • That's another solution which can come handy in other situations. Thanks.
    – brad
    Commented Aug 11, 2022 at 21:02
1

Working on it, I solved my problem.

I post the solution in case someone else need it.

\documentclass{standalone}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{hhline}
\newcommand{\firstrow}[2]{%
  \multicolumn{2}{#1}{\cellcolor{blue!25} #2}
}
\newcolumntype{C}{>{\cellcolor{blue!50} }c<{}}
\setlength{\arrayrulewidth}{2pt}
\begin{document}
\renewcommand{\arraystretch}{2}
  \begin{tabular}{|C|C|>{\hspace*{-3mm}}c|C|C|}
    \hhline{--~|--}
    \firstrow{|c|}{R1C1--2}&&\firstrow{c|}{R1C4--5} \\ 
    \hhline{--~|--}
    \multicolumn{5}{c}{\strut\vspace{-20pt}} \\
    \hhline{--~~~}
    R2C1 & R2C2 & \multicolumn{3}{c}{\strut} \\
    \hhline{--~|--}
    R3C1 & R3C2 && R3C4 &  R3C5  \\ 
    \hhline{--~|--}
    R4C1 &  R4C2  &&&\\ 
    \hhline{--~|>{\arrayrulecolor{blue!50}}->{\arrayrulecolor{black}}|>{\arrayrulecolor{blue!50}}->{\arrayrulecolor{black}}|}
    R5C1 & R5C2 && \multirow{-2}{*}{R4--5C4} & \multirow{-2}{*}{R4--5C5}  \\ 
    \hhline{--~|--}
    R6C1 & R6C2 && R6C5 &  R6C5                                    
    \\ 
    \hhline{--~|--}
  \end{tabular}
\end{document}

which results in

enter image description here

Solution to every point is:

  1. add a | in every relevant position of \hhline, which cempletes the vertical border.
  2. Move the multirow text in the lower row, so that it is this one which overrides the preceding row;
  3. Take care of the color of every piece of border (a bit annoying...).
  4. It is actually an issue: it is not evident from the image I posted, but looking at the pdf after compilation you can realize, zooming in, that some border have double thickness. It is more evident if you add, like I did in the solution, the \setlength{\arrayrulewidth}{2pt} command. The problem arises because some borders are drawed twice. In the solution this is fixed. It was a bad use from me of the hhline package (it was first time with this particular package).
  5. As far as I could understand, there is no actual way to control actual space between two given rows or columns in the plain tabular environment. If I am not mistaken, this is possible using the tabularray package. Without reverting to it, I came out with inserting a row containing a negative space. It works even between columns. A bit of a workaround, more than a solution, but more than enough for my purpose.

All in all, I have now the table I desired. :)

2
  • For the problem 3: use the preamble {|C|C|>{\hspace*{-2.5mm}}c|C|C|} Commented Aug 11, 2022 at 19:18
  • @F.Pantigny : probably you mean problem 5, for which your suggestion works. :) Thanks
    – brad
    Commented Aug 11, 2022 at 21:00

You must log in to answer this question.

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