1

I know how to color a cell of a table in one color, as shown here: color only a cell of a table. However, what I want is to have multiple colors in one cell.

Here is a mockup of what I hope is possible (sorry it's so big, I don't know how to make it smaller): enter image description here

As a secondary question, it would also be lovely if I were able to programmatically condition the colors on the contents of the cell. So in my example above, all "a"s in the table would have a background of yellow, all "b"s brown, and so on. I have seen this: Tables: Cell Color based on content / conditional cell coloring but I think the implementation would be different with multiple colors in one cell.

abias
  • 13
  • 3

1 Answers1

0

Solution thanks to @GuilhermeZanotelli -- https://tex.stackexchange.com/a/349140/197451

enter image description here

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{tikzmark, calc}
% Makes a style for double color filling
\tikzset{
    double color fill/.code 2 args={
        \pgfdeclareverticalshading[%
            tikz@axis@top,tikz@axis@middle,tikz@axis@bottom%
        ]{diagonalfill}{100bp}{%
            color(0bp)=(tikz@axis@bottom);
            color(50bp)=(tikz@axis@bottom);
            color(50bp)=(tikz@axis@middle);
            color(50bp)=(tikz@axis@top);
            color(100bp)=(tikz@axis@top)
        }
        \tikzset{shade, left color=#1, right color=#2, shading=diagonalfill}
    }
}
% Code adapted from Gonzalo: https://tex.stackexchange.com/a/67096/81905
\newcommand\BGcell[4][0pt]{%
  \begin{tikzpicture}[overlay,remember picture]%
    \path[#4] ( $ (pic cs:#2) + (-.5\tabcolsep,1.9ex) $ ) rectangle ( $ (pic cs:#3) + (\tabcolsep,-#1*\baselineskip-.8ex) $ );
  \end{tikzpicture}%
}%
\newcounter{BGnum}
\setcounter{BGnum}{1}
\newcommand\cellBG[3]{
    \multicolumn{1}{
        !{\BGcell{startBG\arabic{BGnum}}{endBG\arabic{BGnum}}{%
                #1}
            \tikzmark{startBG\arabic{BGnum}}}
            #2
        !{\tikzmark{endBG\arabic{BGnum}}}}
        {#3} 
      \addtocounter{BGnum}{1}
}
% end of code from Gonzalo

\usepackage{dcolumn, array, booktabs}
\newcolumntype{.}{D{.}{.}{-1}}
\newcommand*{\tabhead}[1]{\multicolumn{1}{c}{#1}}

\begin{document}
  \tikzset{%
    diagonal fill/.style 2 args={%
        double color fill={#1}{#2},
        shading angle=45,
        opacity=0.8},
    other filling/.style={%
        shade,
        shading=myshade,
        shading angle=0,
        opacity=0.5}
   }
  % Other crazy filling options using shading
  \pgfdeclarehorizontalshading{myshade}{100bp}{%
      color(0bp)=(blue);
      color(25bp)=(blue);
      color(37.5bp)=(blue);
      color(37.5bp)=(brown);
      color(50bp)=(brown);
      color(50bp)=(green);
      color(62.5bp)=(green);
      color(62.5bp)=(purple);
      color(75bp)=(red);
      color(100bp)=(red)}

  \begin{tabular}{c.}
    \toprule
    \cellBG{double color fill={red}{blue}, shading angle=-45, opacity=0.5}{c}{Header 1} & \tabhead{Header 2}\\
    \midrule
    \cellBG{diagonal fill={yellow}{green}}{c}{Text} & \cellBG{other filling}{.}{1.2333}\\
    \cellBG{other filling}{c}{Text} & 154.622\\
    Text & 1.244\\
    Text & 11.3\\
    Text & 121.2\\
    \bottomrule
  \end{tabular}
\end{document}
js bibra
  • 21,280