5

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.

  • 2
    Could you please add a minimal example of what you tried so far? – CarLaTeX Feb 4 at 19:35
  • not only fragment of code .... please extend it to complete small document beginning with \documentclass{...} and ending with \end{document} – Zarko Feb 4 at 21:21
  • TikZ's matrix library may be useful for this? – Black Mild Feb 5 at 3:13
3

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}
\newcommand\ccg[1]{\cellcolor{gray!30!red!30}{#1}} % for cells in second column 
                                                   % and gray colored rows
\newcommand\ccw[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

0

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}
    \def\graybg{black!20!white}%
    \def\currowcolor{\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}%
                        {\global\def\currowcolor{white}}%
                        {\global\def\currowcolor{\graybg}}%
                }%
                {%
                    \ifdefstring%
                        {\oldcol}{\graybg}%
                        {\global\def\currowcolor{white}}%
                        {\global\def\currowcolor{\graybg}}%
                    \global\def\oldcol{NONE}%
                }%
            }%

    % Overwrite the row color (while preserving shading and column colors)
    \newcommand{\owc}[1]%
        {%
            \global\let\oldcol\currowcolor%
            \global\def\currowcolor{\oldcol!50!#1}%
        }%

    \newenvironment{mytable}[1]
        {%
        \global\def\oldcol{NONE}%
        \def\currowcolor{\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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.