2

This table is buggy somehow, there is this white cell and I want the gradient background to fill the whole 1st line of each main column; any one has an idea to make it cleaner ?

\documentclass{article}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{tikz}

\definecolor{customcolor}{RGB}{77, 50, 52}
\definecolor{lightcolor}{RGB}{110, 70, 72}
\definecolor{lightgrey}{RGB}{220, 220, 220}
\definecolor{lightpink}{RGB}{255, 230, 235}
\definecolor{myorange}{RGB}{255, 165, 0}
\definecolor{mydarkorange}{RGB}{255, 140, 0}
\definecolor{darkprune}{RGB}{102, 34, 102}
\definecolor{darkviolet}{RGB}{115, 0, 230}

\setlength{\arrayrulewidth}{2pt}
\arrayrulecolor{white}

\newcommand\mycell[2]{%
    \begin{tikzpicture}
        \node[inner sep=0pt, minimum width=\linewidth, minimum height=3em] (a) {\phantom{#2}};
        \fill[left color=customcolor, right color=lightcolor] (a.south west) rectangle (a.north east);
        \node at (a) {#2};
    \end{tikzpicture}%
}

\begin{document}
    
        \begin{tabular}{|>{\columncolor{white}}c|>{\columncolor{white}}c|>{\columncolor{white}}c|>{\columncolor{white}}c|}
            \hline
            \multicolumn{2}{c}{\mycell{customcolor}{First cell content}} & \multicolumn{2}{c}{\mycell{customcolor}{Second cell content}} \\
            \hline
            \multirow{2}{*}{\cellcolor{lightgrey} Content} & \cellcolor{lightpink}\textcolor{darkprune}{Content} & \cellcolor{lightgrey} Content & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
            \cline{2-4}
            & \cellcolor{lightpink}\textcolor{darkprune}{Content} & \cellcolor{lightgrey} Content & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
            \hline
            \cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} & \cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} \\
            \hline
            \cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} & \cellcolor{mydarkorange} Content & \cellcolor{myorange}\textcolor{darkviolet}{Content} \\
            \hline
        \end{tabular}
    
\end{document}

Table I'm trying to reproduce: enter image description here

2 Answers 2

4
  • command for colored multirow cell should be at the last of the spanned row with negative number of spanned rows:
...
            \hline
\cellcolor{lightgrey}
    & \cellcolor{lightpink}\textcolor{darkprune}{Content} 
        & \cellcolor{lightgrey} Content 
            & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
            \cline{2-4}
\multirow{-2}{*}{\cellcolor{lightgrey} Content}
    & \cellcolor{lightpink}\textcolor{darkprune}{Content} 
        & \cellcolor{lightgrey} Content 
            & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
            \hline
...

enter image description here

  • However, your table has issues with its width, probably caused with definition of \mycell. You may consider to coloring first row directly.

Addendum:
Unfortunately is unclear (to me) what you like to obtain with your colorful table. So I just guess, that table can be far simpler to write by use tblr of tabularray package and defining new command for cell coloring:

\documentclass{article}
\usepackage{xcolor}
    \definecolor{customcolor}{RGB}{77, 50, 52}
    \definecolor{lightcolor}{RGB}{110, 70, 72}
    \definecolor{lightgrey}{RGB}{220, 220, 220}
    \definecolor{lightpink}{RGB}{255, 230, 235}
    \definecolor{myorange}{RGB}{255, 165, 0}
    \definecolor{mydarkorange}{RGB}{255, 140, 0}
    \definecolor{darkprune}{RGB}{102, 34, 102}
    \definecolor{darkviolet}{RGB}{115, 0, 230}
\usepackage{tabularray}
\NewTableCommand\SCC[1]{\SetCell{bg=#1}}

\begin{document}

\begin{tblr}{hlines={2pt,fg=white}, vlines={2pt,fg=white},
             colspec = {*{2}{Q[l, fg=darkprune] X[c, fg=darkviolet]}},
             row{1}  = {ht=3em},
             hspan = minimum}
\SetCell[c=2]{c, bg=customcolor,fg=white}    First cell content
    &   &   \SetCell[c=2]{c, bg=customcolor,fg=white}    Second cell content
            &                                                   \\
\SetCell[r=2]{bg=lightgrey}   Content 
    &   \SCC{lightpink} Content 
        &   \SCC{lightgrey}  Content
            &   \SCC{lightpink} Content                         \\
    &   \SCC{lightpink} Content
        &   \SCC{lightgrey}  Content
            &   \SCC{lightpink} Content                         \\
\SCC{mydarkorange} Content 
    & \SCC{myorange}    Content  
        &   \SCC{mydarkorange}  Content 
            & \SCC{myorange}    Content                         \\
\SCC{mydarkorange} Content
    & \SCC{myorange}\textcolor{darkviolet}{Content}
        &   \SCC{mydarkorange}  Content
            &    \SCC{myorange} Content     \\  %Removed extra brace
\end{tblr}

\end{document}

enter image description here

4

There's no need for extra nodes. A single one accepts all the properties you need to set: text and fill custom colours, including shading. You can adjust dimensions as well as you would do for three separate overlapping nodes.

I slightly streamlined your code. For instance, \coumncolor{white}... seems redundant if you don't use white backgrounds. It'd be better to set it to most common colours, e.g. mydarkorange and myorange and avoid repeating local colour specification in every cell, apart from those which required separate settings. I removed optional [table] since you load colortbl to avoid potential package clash. However, if you want to add other useful options (svgnames for more colour definitions), place xcolor before any other package that also loads xcolor.

showframe is a convenient package which draws a reference frame (blue) so you can see how the table fits within a page.

Here's the table:

enter image description here

and the code:

\documentclass{article}
\usepackage{array}
\usepackage[svgnames]{xcolor}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{tikz}

\usepackage{showframe}   % Draws reference frame around a page
\renewcommand*\ShowFrameLinethickness{0.2pt}
\renewcommand*\ShowFrameColor{\color{blue}}

\definecolor{customcolor}{RGB}{77, 50, 52}
\definecolor{lightcolor}{RGB}{110, 70, 72}
\definecolor{lightgrey}{RGB}{220, 220, 220}
\definecolor{lightpink}{RGB}{255, 230, 235}
\definecolor{myorange}{RGB}{255, 165, 0}
\definecolor{mydarkorange}{RGB}{255, 140, 0}
\definecolor{darkprune}{RGB}{102, 34, 102}
\definecolor{darkviolet}{RGB}{115, 0, 230}

\newcommand\mycell[2]{%
  \tikz[baseline]
    \node[
      color=#1,
      left color=customcolor,
      right color=lightcolor,
      minimum width=\linewidth,
      minimum height=3em,
      inner sep=0pt,
      outer sep=0pt,
    ] {#2};}


\begin{document}
\noindent
\begingroup
\renewcommand{\arraystretch}{1.25}%
\setlength{\arrayrulewidth}{2pt}%
\arrayrulecolor{white}%
\centering
\begin{tabular}{|
  >{\columncolor{mydarkorange}\color{Blue}}c|
  >{\columncolor{myorange}\color{darkviolet}}c|
  >{\columncolor{mydarkorange}\color{Blue}}c|
  >{\columncolor{myorange}\color{darkviolet}}c |}
  %%% Header
  \hline
  \multicolumn{2}{|@{}p{\dimexpr0.5\linewidth-1.5\arrayrulewidth}@{}|}{\mycell{white}{First cell content}}
  & \multicolumn{2}{@{}p{\dimexpr0.5\linewidth-1.5\arrayrulewidth}@{}|}{\mycell{white}{Second cell content}} \\
  \hline
  %%% Subheader
  \cellcolor{lightgrey}
  & \cellcolor{lightpink}\textcolor{darkprune}{Content}
  & \cellcolor{lightgrey}\textcolor{black}{Content}
  & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
  \multirow{-2}*{\cellcolor{lightgrey}\textcolor{black}{Content}}
  & \cellcolor{lightpink}\textcolor{darkprune}{Content}
  & \cellcolor{lightgrey}\textcolor{black}{Content}
  & \cellcolor{lightpink}\textcolor{darkprune}{Content} \\
  %%% Main content
  \hline
  Content & Content & Content & Content \\
  \hline
  Content & Content & Content & Content \\
  \hline
  Content & Content & Content & Content \\
  \hline
\end{tabular}
\endgroup
\end{document}
3
  • Looks pretty, I have added in the OP the table I'm trying to reproduce. Apr 2 at 11:33
  • Well ..., this is quite different. You should definitely keep the old screenshot because this is what the code in your question generates and what your question is about. With complexity of the other table, you might actually follow Zarko's approach. While it's doable using standard tabular, colortbl and some additional packages s.a. \makecell, tblr of tabularray can simplify lots of code with minimum effort to produce complex colourful tables.
    – Celdor
    Apr 2 at 11:48
  • Yes I generally use tabularray, I find it very convenient. I was suggested the other approach with colortbl but I am not very comfortable with it. If you say that it would be easier with tabularray, it's all the best for me. But I might keep your solution sicne the table looks nice. I don't know how to get the same beautiful gradient though. Apr 2 at 12:04

You must log in to answer this question.

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