TransWikia.com

Color table rows and columns

TeX - LaTeX Asked on April 5, 2021

I have a table whose rows are set to alternate between white and gray using xcolor’s rowcolors{2}{white}{gray!10}. I also want to highlight a column of that table, sos that it looks something like this:

Alternating rows, highlighted column

I tried using begin{tabular}{c>{columncolor{_lblue!25}}cc} to do this, but that just overwrites the color, blending it with white. Is there a way to blend the row and column colors so that it looks like the image? Maybe a variable that holds the background color so I can do something like columncolor{_lblue!25!cellbgcolor}?

Edit: What I have right now is

rowcolors{2}{white}{gray!10}
begin{tabular}[c>{columncolor{blue}}cc]
    a & b & c 
    hline
    1 & 2 & 3 
    4 & 5 & 6 
    7 & 8 & 9
end{tabular}

I’ve tried to use the transparency package to see I might be able to make the column color transparent in case it was overlaid instead of overwriting, but that makes the whole cell including text transparent. I don’t know how I might go about alternating the color on each row while keeping track of the column color.

3 Answers

unfortunately coloring of rows and columns in table doesn't has option for transparency of row colors (as far as i know). so it should be imitated by cellcolor with appropriate color. for example like this:

documentclass{article}
usepackage[T1]{fontenc}
usepackage[table]{xcolor}
newcommandccg[1]{cellcolor{gray!30!red!30}{#1}} % for cells in second column 
                                                   % and gray colored rows
newcommandccw[1]{cellcolor{red!15}{#1}}         % for cells in  second column 
                                                   % white colored rows

begin{document}
rowcolors{1}{white}{gray!30}
    begin{tabular}{c c c}
        a   & ccw{b}   & c     
        hline
        1   & ccg{2}   & 3     
        4   & ccw{5}   & 6     
        7   & ccg{8}   & 9     
    end{tabular}
end{document}

enter image description here

Correct answer by Zarko on April 5, 2021

One way that I have since figured out is to ditch rowcolors completely. This lets you highlight rows and columns, shades alternating columns, and will blend colors for highlights. It might not be too pretty, but it works.

documentclass[a4paper]{article}
usepackage[T1]{fontenc}
usepackage[utf8]{inputenc}
usepackage[table]{xcolor}
usepackage{etoolbox}
usepackage{array}

begin{document}
    defgraybg{black!20!white}%
    defcurrowcolor{graybg}%
    newcolumntype{H}[1]{>{cellcolor{currowcolor!60!#1}}c} % Highlighted col
    newcolumntype{C}{H{white}} % Normal col

    newcommand{rowalt}
            {%
            ifdefstring{oldcol}{NONE}%
                {%
                    ifdefstring%
                        {currowcolor}{graybg}%
                        {globaldefcurrowcolor{white}}%
                        {globaldefcurrowcolor{graybg}}%
                }%
                {%
                    ifdefstring%
                        {oldcol}{graybg}%
                        {globaldefcurrowcolor{white}}%
                        {globaldefcurrowcolor{graybg}}%
                    globaldefoldcol{NONE}%
                }%
            }%

    % Overwrite the row color (while preserving shading and column colors)
    newcommand{owc}[1]%
        {%
            globalletoldcolcurrowcolor%
            globaldefcurrowcolor{oldcol!50!#1}%
        }%

    newenvironment{mytable}[1]
        {%
        globaldefoldcol{NONE}%
        defcurrowcolor{graybg}%
        begin{tabular}{!rowalt #1}%
        }%
        {%
        end{tabular}%
        }%

    begin{mytable}{CH{blue!50}CCC}
        a & b & c & d & e 
        hline
        1 & 2 & 3 & 2 & 1 
        4 & 5 & 6 & 5 & 4 
        owc{red}7 & 8 & 9 & 8 & 7 
        owc{red}4 & 5 & 6 & 5 & 4 
        1 & 2 & 3 & 2 & 1
    end{mytable}
end{document}

enter image description here

Answered by Robert on April 5, 2021

Here is a way to that with {NiceTabular} of nicematrix. The environment is similar to the classical {tabular} (of array) but adds PGF/Tikz nodes under the cells, rows and columns. It's possible to use those nodes to draw whatever you want before the construction of the main array (in the CodeBefore).

documentclass{article}
usepackage{nicematrix,tikz}

begin{document}
begin{NiceTabular}{ccc}
CodeBefore
  begin{tikzpicture}
  fill [gray!10] (2-|1) rectangle (3-|last) 
                  (4-|1) rectangle (5-|last) ;
  fill [blue,fill opacity = 0.2] (1-|2) rectangle (last-|3) ;
  end{tikzpicture}
Body
    a   & b   & c      hline
    1   & 2   & 3     
    4   & 5   & 6     
    7   & 8   & 9     
end{NiceTabular}
end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

Answered by F. Pantigny on April 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP