3

I'm creating a table containing HTML colour and merged cells. However, some \cline/\hline have not the default \arrayrulewidth. I tried to use \setlength{\arrayrulewidth}{1pt} before \cline/\hline but failed.

\documentclass{article}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{graphicx}
\begin{document}
\begin{table}
\centering
\resizebox{\columnwidth}{!}{
\begin{tabular}{|c|c|c|c|l|}
\hline
                            & 1                                              & 2           & 3       & 4                                     \\\hline  
\cellcolor[HTML]{FFE2CC} A  & B                                              & C           & D       & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\\hline  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}F} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\cline{2-4}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}G} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\cline{2-4}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}H} & \cellcolor[HTML]{DAE8FC}   \\\cline{2-4}  
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}I                      & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}J &   \cellcolor[HTML]{DAE8FC}    \\\cline{2-2} \cline{4-4} 
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}                       & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}K &   \cellcolor[HTML]{DAE8FC}     \\\cline{4-4}  
\multirow{-6}{*}{\cellcolor[HTML]{CDDAED}J} & \multirow{-2}{*}{\cellcolor[HTML]{DAE8FC}}    & \multirow{-3}{*}{\cellcolor[HTML]{DAE8FC}}    &\cellcolor[HTML]{DAE8FC}L        &   \cellcolor[HTML]{DAE8FC}          \\\cline{1-4}  
\cellcolor[HTML]{D4B1E5}                               & \cellcolor[HTML]{E1D5E7}N                                    & \cellcolor[HTML]{E1D5E7}O                        &\cellcolor[HTML]{E1D5E7}N         &  \cellcolor[HTML]{E1D5E7}  \\\cline{2-4}  
\multirow{-2}{*}{\cellcolor[HTML]{D4B1E5}E}    & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} &\cellcolor[HTML]{E1D5E7}Z       &  \cellcolor[HTML]{E1D5E7}     \\\cline{1-5}
\end{tabular}}
\caption{With color}
\end{table}
\end{document}

Table with color

I followed Werner's answer which use \hhline, but the white rule is more emphasized in coloured cells.

\begin{table}
\centering
\resizebox{\columnwidth}{!}{
\begin{tabular}{|c|c|c|c|l|}
\hline
                            & 1                                              & 2           & 3       & 4                                     \\\hline  
\cellcolor[HTML]{FFE2CC} A  & B                                              & C           & D       & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\\hline  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}F} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\hhline{|~|-|-|-|~|}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}G} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}    \\\hhline{|~|-|-|-|~|}  
\cellcolor[HTML]{CDDAED}    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}H} & \cellcolor[HTML]{DAE8FC}   \\\hhline{|~|-|-|-|~|}  
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}I                      & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}J &   \cellcolor[HTML]{DAE8FC}    \\\hhline{|~|-|~|-|~|}
\cellcolor[HTML]{CDDAED}    & \cellcolor[HTML]{DAE8FC}                       & \cellcolor[HTML]{DAE8FC}  &\cellcolor[HTML]{DAE8FC}K &   \cellcolor[HTML]{DAE8FC}     \\\hhline{|~|~|~|-|~|}
\multirow{-6}{*}{\cellcolor[HTML]{CDDAED}J} & \multirow{-2}{*}{\cellcolor[HTML]{DAE8FC}}    & \multirow{-3}{*}{\cellcolor[HTML]{DAE8FC}}    &\cellcolor[HTML]{DAE8FC}L        &   \cellcolor[HTML]{DAE8FC}          \\\hhline{|-|-|-|-|~|}
\cellcolor[HTML]{D4B1E5}                               & \cellcolor[HTML]{E1D5E7}N                                    & \cellcolor[HTML]{E1D5E7}O                        &\cellcolor[HTML]{E1D5E7}N         &  \cellcolor[HTML]{E1D5E7}  \\\hhline{|~|-|-|-|~|}  
\multirow{-2}{*}{\cellcolor[HTML]{D4B1E5}E}    & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} & \multicolumn{1}{l|}{\cellcolor[HTML]{E1D5E7}} &\cellcolor[HTML]{E1D5E7}Z       &  \cellcolor[HTML]{E1D5E7}     \\\hline
\end{tabular}}
\caption{Using \texttt{hhline}}
\end{table}

Using \hhline

I then found Zarko's answer which consist of colour \hhline

\definecolor{M1}{HTML}{CDDAED}
\definecolor{M2}{HTML}{DAE8FC}

\begin{table}
\centering
\resizebox{\columnwidth}{!}{
\begin{tabular}{!{\color{black}\vrule}c!{\color{black}\vrule}c!{\color{black}\vrule}c!{\color{black}\vrule}c!{\color{black}\vrule}l!{\color{black}\vrule}}
\hline
                                            & 1                                              & 2                                                    & 3                              & 4                                     \\\hline  
\cellcolor[HTML]{FFE2CC} A                  & B                                              & C                                                    & D                              & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\\hline  
\cellcolor[HTML]{CDDAED}                    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}F} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}                                                                   \\\hhline{|>{\arrayrulecolor{M1}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{black}}} 
\cellcolor[HTML]{CDDAED}                    & \multicolumn{3}{c|}{\cellcolor[HTML]{DAE8FC}G} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}}                                                                   \\\hhline{|>{\arrayrulecolor{M1}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{M2}}-|>{\arrayrulecolor{black}}}
\end{tabular}}
\end{table}

The code above gives me this :

hhline coloured

but it's quite complicated and the intersections of \hhline and \vline are not in black. This issue can be solved if we could draw vline after \hhline. Is there a more elegant way to deal with such problem? Changing rule's colour is annoying.

  • Welcome to TexSE!! Great 1st question. – A Feldman Apr 27 '18 at 13:44
  • In my experience the only way to get good lines in colored tables is to redraw them at the end of the table with e.g. tikz: https://tex.stackexchange.com/questions/409650/line-problem-when-use-color-table/409657#409657 – Ulrike Fischer Apr 27 '18 at 13:45

4 Answers4

5

Here is how to do it with hhline:

\documentclass[a4paper, twocolumn]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{adjustbox}
\usepackage[table]{xcolor}
\usepackage{array, multirow, hhline, booktabs}

\definecolor{blue1}{HTML}{CDDAED}
\definecolor{blue2}{HTML}{DAE8FC}
\definecolor{mauve}{HTML}{E1D5E7}
\definecolor{violet}{HTML}{D4B1E5}

\newcommand{\hhlinea}{\hhline{|>{\arrayrulecolor{blue1}}->{\arrayrulecolor{black}}|-|-|-|>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}}
\newcommand{\hhlineb}{\hhline{|>{\arrayrulecolor{blue1}}->{\arrayrulecolor{black}}|-|>{\arrayrulecolor{blue2}}-%
>{\arrayrulecolor{black}}|-|>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}}
\newcommand{\hhlinec}{\hhline{|>{\arrayrulecolor{blue1}}->{\arrayrulecolor{black}}|%
*{2}{>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}-|>{\arrayrulecolor{blue2}}->{\arrayrulecolor{black}}|}}

\begin{document}

\begin{table}
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{|c|*{3}{c|}>{\columncolor{blue2}}l|}
\hline
\rowcolor{white} & 1 & 2 & 3 & 4 \\
\hline
\rowcolor{white} \cellcolor[HTML]{FFE2CC} A & B & C & D & \multicolumn{1}{c|}{\cellcolor[HTML]{FFF2CC}E} \\
\hline
\rowcolor{blue2}\cellcolor{blue1} & \multicolumn{3}{c|}{F} & \\
\hhlinea
\rowcolor{blue2}\cellcolor{blue1} & \multicolumn{3}{c|}{G} & \\
\hhlinea
\rowcolor{blue2}\cellcolor{blue1} & \multicolumn{3}{c|}{H} & \\
\hhlinea
\rowcolor{blue2}\cellcolor{blue1} & I & & J & \\
\hhlineb
\rowcolor{blue2}\cellcolor{blue1} & & & K & \\
\hhlinec
\rowcolor{blue2}\multirow{-6}{*}{\cellcolor[HTML]{CDDAED}J} & & & L & \\
\hhline{|-|-|-|-|>{\arrayrulecolor{mauve}}->{\arrayrulecolor{black}}|}
\rowcolor{mauve}\cellcolor{violet} & N & O & N & \\
\hhline{|>{\arrayrulecolor{violet}}->{\arrayrulecolor{black}}|-|-|-|>{\arrayrulecolor{mauve}}->{\arrayrulecolor{black}}|}
\rowcolor{mauve}\multirow{-2}{*}{\cellcolor{violet}E} & & & Z & \\
\hline
\end{tabular}}
\caption{Using \texttt{hhline}}
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350
5

The package nicematrix provides tools designed to solve that kind of problems.

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

\definecolor{color1}{HTML}{FFE2CC} \definecolor{color2}{HTML}{FFF2CC} \definecolor{color3}{HTML}{CDDAED} \definecolor{color4}{HTML}{DAE8FC} \definecolor{color5}{HTML}{E1D5E7} \definecolor{color6}{HTML}{D4B1E5}

\begin{document} \begin{table} \centering \begin{NiceTabular}{ccccl}[hvlines,colortbl-like] & 1 & 2 & 3 & 4 \ \cellcolor{color1}A & B & C & D & \cellcolor{color2}E \ \Block[fill=color3]{6-1}{J} & \Block[fill=color4]{1-3}{F} & & & \Block[fill=color4]{6-1}{} \ & \Block[fill=color4]{1-3}{G} \ & \Block[fill=color4]{1-3}{H} \ & \cellcolor{color4}I & \Block[fill=color4]{3-1}{} & \cellcolor{color4}J \ & \Block[fill=color4]{2-1}{} & & \cellcolor{color4}K \ & & & \cellcolor{color4}L \ \Block[fill=color6]{2-1}{E} & \cellcolor{color5}N & \cellcolor{color5}O &\cellcolor{color5}N & \Block[fill=color5]{2-1}{} \ & \cellcolor{color5} & \cellcolor{color5} &\cellcolor{color5}Z & \ \end{NiceTabular} \caption{With color} \end{table} \end{document}

  • In {NiceTabular} (which is similar to {tabular} of array), you have a command \Block to merge cells horizontally and vertically. You can specify the color of the block with the key color.

  • With the key hvlines, all the rules are drawn, excepted in the blocks (created by \Block).

  • You have a good result in all the PDF viewers (you don't have rules which seem to vanish at some levels of zoom; you don't have the thin white lines you see for instance in the viewers using MuPDF such as SumatraPDF when the tabular is constructed with colortbl).

  • However, you need several compilations.

Output of the above code

It's also possible to specify the colors of the cells, rows and columns before the body of the tabular with the keyword \CodeBefore (and Body).

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

\begin{document} \begin{table} \centering \begin{NiceTabular}{ccccl}[hvlines] \CodeBefore \cellcolor[HTML]{FFE2CC}{2-1} \cellcolor[HTML]{FFF2CC}{2-5} \rectanglecolor[HTML]{CDDAED}{3-1}{8-1} \rectanglecolor[HTML]{DAE8FC}{3-2}{8-5} \rectanglecolor[HTML]{E1D5E7}{9-2}{10-5} \rectanglecolor[HTML]{D4B1E5}{9-1}{10-1} \Body & 1 & 2 & 3 & 4 \ A & B & C & D & E \ \Block{6-1}{J} & \Block{1-3}{F} & & & \Block{6-1}{} \ & \Block{1-3}{G} \ & \Block{1-3}{H} \ & I & \Block{3-1}{} & J \ & \Block{2-1}{} & & K \ & & & L \ \Block{2-1}{E} & N & O &N & \Block{2-1}{} \ & & &Z & \ \end{NiceTabular} \caption{With color} \end{table} \end{document}

The output is the same.

F. Pantigny
  • 40,250
4

I am a fan of packages that include others with improvements, in this case through the tabu package, you can vary the thickness and style of the horizontal lines with a single command \tabulinestyle{2pt black} but it will occur that spaces will appear, for that reason you must manually subtract the thickness at the end of new row command & foo \\[-barpt], where bar is the thickness of the line and do not forget to declare \setlength{\arrayrulewidth}{2pt} for the \cline command also with the same desired thicknness, for example in the code if I change the \tabulinestyle{3pt black} and I do not change the value in & foo \\[-2pt] I get this error:

enter image description here

In the MWE, is defined on 2pt \tabulinestyle{2pt black} and all the new row commands are substracted using & foo \\[-2pt]and I get this: enter image description here

The code is based on easy to read structure:

% arara: pdflatex: {synctex: yes, action: nonstopmode}
\documentclass{article}
\usepackage{tabu,booktabs}
\usepackage{tabularx}
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{graphicx}
% to not write all of these many times...
\definecolor{C1}{HTML}{FFE2CC}
\definecolor{C2}{HTML}{FFF2CC}
\definecolor{C3}{HTML}{DAE8FC}
\definecolor{C4}{HTML}{D4B1E5}
\definecolor{C5}{HTML}{E1D5E7}
\setlength{\arrayrulewidth}{2pt} % To declare the \cline width
\arrayrulecolor{blue} % If you need to change the color of \cline
\begin{document}
    \begin{table}
        \centering
        \tabulinesep = 2mm
        \tabulinestyle{2pt blue}
        \begin{tabu}to 6cm {|X[2c]|X[c]|X[c]|X[c]|X[2c]|}
            \tabucline -
            %Row 1
            &1
            &2
            &3
            &4\\ \tabucline -  
            %Row 2
            \cellcolor{C1}A
            &B
            &C
            &D
            &\cellcolor{C2}E \\ \tabucline - 
            %Row 3
            \cellcolor{black!20!C3}    
            & \multicolumn{3}{c|}{\cellcolor{C3}F} 
            & \multicolumn{1}{c|}{\cellcolor{C3}}    \\[-2pt] \tabucline{2-4} 
            %Row 4
            \cellcolor{black!20!C3}
            &\multicolumn{3}{c|}{\cellcolor{C3}G}
            & \multicolumn{1}{c|}{\cellcolor{C3} } \\[-2pt] \tabucline{2-4}  
            %Row 5
            \cellcolor{black!20!C3}
            & \multicolumn{3}{c|}{\cellcolor{C3}H}
            & \cellcolor{C3}\\[-2pt] \tabucline{2-4}  
            %Row 6
            \cellcolor{black!20!C3}
            &\cellcolor{C3}I
            &\cellcolor{C3}
            &\cellcolor{C3}J
            &\cellcolor{C3}\\[-2pt] \cline{2-2}\tabucline{4-4} %<-that is something unespected good result!
            %Row 7
            \cellcolor{black!20!C3}
            &\cellcolor{C3}
            &\cellcolor{C3}
            &\cellcolor{C3}K
            &\cellcolor{C3} \\[-2pt] \tabucline{4-4}  
            %Row 8
            \multirow{-6}{*}{\cellcolor{black!20!C3}J}
            &\multirow{-2}{*}{\cellcolor{C3}}
            &\multirow{-3}{*}{\cellcolor{C3}}
            &\cellcolor{C3}L
            &\cellcolor{C3}\\[-2pt] \tabucline{1-4}
            %Row 9
            \cellcolor{C4}
            &\cellcolor{C5}N
            &\cellcolor{C5}O
            &\cellcolor{C5}N
            &\cellcolor{C3}\\[-2pt] \tabucline{2-4}  
            %Row 10
            \multirow{-2}{*}{\cellcolor{C4}E}
            &\multicolumn{1}{l|}{\cellcolor{C5}}
            &\multicolumn{1}{l|}{\cellcolor{C5}}
            &\cellcolor{C5}Z
            &\cellcolor{C3}\\ \tabucline -
        \end{tabu}
        \caption{Table for testing colors and rules in tabu}
    \end{table}
\end{document}
J Leon V.
  • 11,533
  • 16
  • 47
4

FWIW, I find that ConTeXt's table mechanisms are MUCH cleaner for creating such tables. For example, here is how to create this table using the extreme tables mechanism:

\definecolor[C1][h=ffe2cc]
\definecolor[C2][h=fff2cc]
\definecolor[C3][h=cddaed]
\definecolor[C4][h=dae8fc]
\definecolor[C5][h=d4b1e5]
\definecolor[C6][h=e1d5e7]

\setupxtable[mytable]
            [
              option=stretch,
              background=color,
              align={middle,lohi},
              rulethickness=2pt,
              textwidth=6cm,
            ]

\starttext
\startxtable[mytable]
  \startxrow  
    \startxcell   \stopxcell
    \startxcell 1 \stopxcell
    \startxcell 2 \stopxcell
    \startxcell 3 \stopxcell
    \startxcell 4 \stopxcell
  \stopxrow
  \startxrow  
    \startxcell[backgroundcolor=C1] A \stopxcell
    \startxcell B \stopxcell
    \startxcell C \stopxcell
    \startxcell D \stopxcell
    \startxcell[backgroundcolor=C2] E \stopxcell
  \stopxrow
  \startxrow  
    \startxcell[backgroundcolor=C3,ny=6] J \stopxcell
    \startxcell[backgroundcolor=C4,nx=3] F \stopxcell
    \startxcell[backgroundcolor=C4,ny=8]   \stopxcell
  \stopxrow
  \startxrowgroup[backgroundcolor=C4]
    \startxrow \startxcell[nx=3] G \stopxcell \stopxrow
    \startxrow \startxcell[nx=3] H \stopxcell \stopxrow
    \startxrow 
      \startxcell I \stopxcell
      \startxcell[ny=3] \stopxcell
      \startxcell J \stopxcell
    \stopxrow
    \startxrow
      \startxcell[ny=2] \stopxcell
      \startxcell K \stopxcell
    \stopxrow
    \startxrow \startxcell L \stopxcell \stopxrow
  \stopxrowgroup
  \startxrowgroup[backgroundcolor=C6]
  \startxrow
    \startxcell[backgroundcolor=C5, ny=2] E \stopxcell
    \startxcell N \stopxcell
    \startxcell O \stopxcell
    \startxcell N \stopxcell
  \stopxrow
  \startxrow
    \startxcell   \stopxcell
    \startxcell   \stopxcell
    \startxcell Z \stopxcell
  \stopxrow
  \stopxrowgroup
\stopxtable
\stoptext

which gives

enter image description here

Aditya
  • 62,301