3

I'm interested in making a table with overlaping colors. What I mean is that I would like to add an additional color to the first column of my table that would have opacity set to 20% and if it were possible I'd like it to blend with the row colors. I had a real hard time to find an answer to this. I would really appreciate some help :)

What I have so far is:

\documentclass[12pt, a4paper, oneside, onecolumn]{memoir}

\usepackage[table]{xcolor}
\definecolor{ltable}{HTML}{EDE7FF}
\definecolor{dtable}{HTML}{D9CEFF}

\begin{document}

\rowcolors{3}{ltable}{dtable}
\begin{tabular}{ |c|c| }
    \hline
    \rowcolor{dtable} \multicolumn{2}{|l|}{header} \\ \hline
    \rowcolor{dtable} \multicolumn{2}{|c|}{some text} \\ \hline
    1 & text \\ \hline
    2 & text \\ \hline
    3 & text \\ \hline
    4 & text \\ \hline
    5 & text \\ \hline
\end{tabular}

\end{document}

The overlaping color would start on 1 and end on 5. For excercise purposes it could just be black with opacity set on 20%.

2 Answers 2

1

You can use tikz to do it. I made a table using matrix and then applied the colors on a background layer. The result that I obtained is given below. I am think you can improve and get the desired result.

enter image description here

MWE

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,backgrounds,calc}
\definecolor{oddrow}{HTML}{00FF00}
\definecolor{evenrow}{HTML}{0000FF}
\definecolor{column1}{HTML}{FF0000}

\begin{document}
\begin{tikzpicture}
\matrix(tab)[matrix of nodes,nodes={inner sep=5pt,minimum width=1cm},column sep=2mm]
{%table without header
1 & text \\
2 & text \\
3 & text \\
4 & text \\
5 & text \\
};

\begin{scope}[on background layer]
\fill[column1,opacity=0.5] (tab-1-1.north west) rectangle (tab-5-1.south east); column1 color
\foreach \rw in {1,3,...,5} %odd rows
\draw[draw=black,fill=oddrow,opacity=0.5] (tab-\rw-1.north west) rectangle (tab-\rw-2.south east);
\foreach \rw in {2,4,...,4} %even rows
\draw[draw=black,fill=evenrow,opacity=0.5] (tab-\rw-1.north west) rectangle (tab-\rw-2.south east);
%headers
\draw (tab-1-1.north west) rectangle ($(tab-1-2.north east)+(0,0.5)$)node[midway]{Some text};
\draw ($(tab-1-1.north west)+(0,0.5)$) rectangle ($(tab-1-2.north east)+(0,1)$)node[midway]{Header};
\end{scope}
\end{tikzpicture}
\end{document}
1
  • Honestly I was afraid Tikz was the fastest solution I find it confusing. Now, I understand the first part: making the two columns of squares. Thanks to another question on this platform I was able to do this: \begin{tikzpicture} \matrix(tab)[matrix of nodes, every node/.style={text depth=.5ex,text height=2ex, minimum width=10mm, draw}, draw=red] {%table without header 1 & text \\ 2 & text \\ 3 & text \\ 4 & text \\ 5 & text \\ }; \end{tikzpicture} But could you point me to a guide so I can figure out how to use/modify the rest of your code?
    – shantais
    Oct 29, 2018 at 21:47
1

The package nicematrix has tools to address that kind of problems.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{cc}[hvlines]
\CodeBefore
  \rowlistcolors{3}{green!50,blue!50}
  \rectanglecolor[opacity=0.2]{black}{3-1}{9-1}
\Body
  \Block{1-2}{Header} \\
  \Block{1-2}{Some text} \\
  1 & text \\
  2 & text \\
  3 & text \\
  4 & text \\
  5 & text \\
  6 & text \\
  7 & text
\end{NiceTabular}

\end{document}

You need several compilations.

Output of the above code

You must log in to answer this question.

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