1

I adapted a solution from post Multi-colored cell background using Overleaf.

The code I use is the following:

%%% This code comes from https://tex.stackexchange.com/questions/348933/multi-colored-cell-background with minor adjustments regarding the format of the table %%%
\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{nicematrix}
\usepackage{siunitx}
\usepackage{booktabs}

\begin{document} \makeatletter \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} } } \makeatother

\tikzset{% diagonal fill/.style 2 args={% double color fill={#1}{#2}, shading angle=45, opacity=0.8}, other filling/.style={% shade, shading=myshade, % myshade is defined below shading angle=0, opacity=0} }

\noindent\begin{NiceTabular}{| p{0.25\textwidth}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |}
            \hline 
            \textbf{Lorem}  
            & \multicolumn{4}{c|}{dolor}
            & \multicolumn{4}{c|}{sit} 
            & \multicolumn{4}{c|}{amet} 
            & \multicolumn{4}{c|}{beneficae} \\ \cline{2-17}
        \hline
        \hline
        \textbf{ipsum} &  \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} \\
        \hline
        Lorem ipsum dolor sit amet & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & &  \\
        \hline
\end{NiceTabular}

\end{document}

producing this result: enter image description here

In contrast to the original post, the two colors are not strictly separated, but they have a gradient, even though I am very sure I am using the same code. I'd like to get rid of this gradient, can anyone help me with that ?

Thanks a lot!

user258410
  • 13
  • 4
  • Welcome to TeX.SE! – Mensch Dec 10 '21 at 15:22
  • 2
    I can confirm that this is the result on overleaf but not on an updated MikTeX system. Overleaf doesn't use the most updated system and sometimes you get unexpected results. I cannot say what's the outdated package that provoques these result. – Ignasi Dec 10 '21 at 16:22
  • 1
    In Overleaf, you have a setting called "PDF Viewer" (in the "Menu") : you can choice between "Overleaf" and "Browser". With "Overleaf", you have a good synchronisation between the LaTeX source and the view of the PDF. However, you may have some problems as yours. You can try to switch to the choice "Browser". – F. Pantigny Dec 10 '21 at 17:49
  • Thanks for the suggestions, but switching to "Browser" sadly didn't do anything for me ... – user258410 Dec 13 '21 at 15:27

1 Answers1

1

You could maybe also try this approach (taken from here) which does not use the shadings library and hence should be displayed correctly on Overleaf:

\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\usepackage{nicematrix}
\usepackage{siunitx}
\usepackage{booktabs}

\begin{document} \tikzset{% diagonal fill/.style 2 args={% fill=#2, path picture={ \fill[#1, sharp corners] (path picture bounding box.south west) -| (path picture bounding box.north east) -- cycle; } }, }

\noindent\begin{NiceTabular}{| p{0.25\textwidth}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |p{0.02\textwidth}*{3}{|p{0.02\textwidth}}
        |}
            \hline 
            \textbf{Lorem}  
            & \multicolumn{4}{c|}{dolor}
            & \multicolumn{4}{c|}{sit} 
            & \multicolumn{4}{c|}{amet} 
            & \multicolumn{4}{c|}{beneficae} \\ \cline{2-17}
        \hline
        \hline
        \textbf{ipsum} &  \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} & \multicolumn{4}{c|}{} \\
        \hline
        Lorem ipsum dolor sit amet & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & \Block[tikz={diagonal fill={yellow}{green}}]{}{} & &  \\
        \hline
\end{NiceTabular}

\end{document}

enter image description here


Addendum

In case you need to adjust the angle, you can use an enhanced version of the above approach that takes an angle as third argument (I used a simpler example to show the application, but you can as well apply this to the above code):

\documentclass[border=1mm, tikz]{standalone}
\usetikzlibrary{calc}

\tikzset{% diagonal fill/.style n args={3}{% fill=#2, path picture={ \fill[#1, rotate=#3] let \p1 = ($(path picture bounding box.south west)-(path picture bounding box.north east)$) in (path picture bounding box.center) -- ++(0,{veclen(\x1,\y1)}) arc (90:-90:{veclen(\x1,\y1)}) -- cycle; } }, }

\begin{document}

\begin{tikzpicture}

\fill[diagonal fill={red}{blue}{30}] (0,0) rectangle ++(5,5);

\fill[diagonal fill={gray}{cyan}{-10}] (7.5,2.5) ellipse (2 and 3);

\fill[diagonal fill={yellow}{green}{135}] (10,1) rectangle ++(6,3);

\end{tikzpicture}

\end{document}

enter image description here