Search code examples
colorsbackgroundlatex

How to highlight merged cells in LaTeX without obscuring the content?


I am trying to highlight merged cells in a LaTeX table. Specifically, I want to:

  1. Fill the merged cell "A" with a red background.
  2. Fill the merged cell "B" with a blue background.

I attempted to use the \cellcolor command from the xcolor package, but it ends up obscuring the original content of the cell.

LaTeX Table

Here is my minimal working example (MWE):

\documentclass[12pt]{article}

\usepackage[table]{xcolor}
\usepackage{makecell}
\usepackage{multirow}

\begin{document}
\begin{table}[ht]
    \centering
    \begin{tabular}{c|c|c}
        \Xhline{1pt}
        \multirow{3}{*}{A}  & B                     & C  \\ \cline{2-3}
                            & \multirow{2}{*}{D}    & E  \\ \cline{3-3}
                            &                       & F  \\
        \Xhline{1pt}
    \end{tabular}
\end{table}
\end{document}

My goal is to make "A" have a red background and "B" have a blue background while keeping the text visible. How can I achieve this?

Any help or suggestions would be greatly appreciated. Thank you!


Solution

  • You could use the tabularray package. It makes it easy to merge cells and colour them:

    \documentclass[12pt]{article}
    
    \usepackage{xcolor}
    \usepackage{tabularray}
    
    \begin{document}
    \begin{table}[ht]
        \centering
        \begin{tblr}{
          colspec={ccc},
          vlines,hlines,
          vline{1,Z}={0pt},
          hline{1,Z}={1pt}
        }
          \SetCell[r=3]{bg=red} A & B & C  \\ 
          & \SetCell[r=2]{bg=blue} D & E \\ 
          & & F \\
        \end{tblr}
    \end{table}
    \end{document}
    

    enter image description here