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.

3

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}
4

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

  • Thank you! I finally use this solution since I used already \hhline. I will vote up when I have privilege. Just note here that hhline is incompatible with beamer(I moved it later to beamer and it strangely doesn't work). – Firmin Martin May 10 '18 at 21:34
  • I don't use beamer very often, but I'm not surprised: I know it's incompatible with a number of packages, most notably enumitem for lists. – Bernard May 10 '18 at 21:41
2

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

  • It looks powerful, but you could exemplify it in a minimum working example (MWE)... – J Leon V. Apr 28 '18 at 4:03
  • This is a working example! But in ConTeXt rather than LaTeX. – Aditya Apr 28 '18 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.