3

In LaTeX, is it possible to set table alternative rule color i.e., first column background color GRAY then hline rule color WHITE(hline), if second column background color WHITE then hline rule color(gray)?enter image description here

MWE:

\documentclass[12pt,a4paper]{book} 
\usepackage{xcolor} 
\usepackage[table]{colortbl} 

\begin{document} 
\begin{table} 
\arrayrulecolor{red} 
\begin{tabular}{|l!{\color{blue}\vline}>{\columncolor[gray]{.8}}l!{\color{blue}\vline}l|} 
\hline 
NOUN &VERB &HOW YOU USE/APPLY EACH DOMAIN\\ 
\hline 
Knowledge &Remembering&\\ 
\hline 
Comprehension &Understanding&\\ 
\hline 
Application &Applying&\\ 
\hline 
Analysis &Analyzing&\\ 
\hline 
Synthesis &Creating and Evaluating&\\ 
\hline 
\end{tabular} 
\end{table} 
\end{document}
Mensch
  • 65,388
Rajesh N
  • 829

3 Answers3

4

You could quickly get the table and this kind of customizations using the following editor: https://www.latex-tables.com; on the right of the page you can set the border colour.

Or try setting the color for each box, adapting a script like this:

\documentclass[12pt,a4paper]{book}
\usepackage{colortbl}
\usepackage{hhline}
\begin{document}

\begin{table}
\centering
\refstepcounter{table}
\label{t:test}
\arrayrulecolor[rgb]{0.753,0.753,0.753}
\begin{tabular}{llll}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{->{\arrayrulecolor{white}}->{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}->{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}->{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}->{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}->{\arrayrulecolor[rgb]{0.753,0.753,0.753}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t 
\end{tabular}
\arrayrulecolor{black}
\end{table}

% or black-white hhline:
\begin{table}
\centering
\refstepcounter{table}
\label{}
\arrayrulecolor{black}
\begin{tabular}{llll}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{->{\arrayrulecolor{white}}->{\arrayrulecolor{black}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{white}}->{\arrayrulecolor{black}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{white}}->{\arrayrulecolor{black}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{white}}->{\arrayrulecolor{black}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t  \\ 
\hhline{>{\arrayrulecolor{black}}->{\arrayrulecolor{white}}->{\arrayrulecolor{black}}->{\arrayrulecolor{white}}-}
t & {\cellcolor[rgb]{0.753,0.753,0.753}}e & s & {\cellcolor[rgb]{0.753,0.753,0.753}}t 
\end{tabular}
\arrayrulecolor{black}
\end{table}
\end{document}
Mi Ka El
  • 131
3

I tried to faithfully reproduce all the characteristics of the table shown as an example in the question (possibly a fraction of a larger table), not just the alternating colors of the rows and the horizontal lines.

While it may not be in the accepted ascetic style of technical publications, it will fit very well in a financial or corporate report. Not only alternate color columns, but number grouping and underlining are widely used in those environments as well. Just for fun I added bold.

The S column allows grouping, but then it is necessary to robustify the bold and underlined commands. The underline length has to be extended to account for the extra space introduced by the number grouping.

Even if there is currently no package that takes care of hline colors matching alternate columns, the code can be used as a template and it is easily modified.

\documentclass[12pt,a4paper]{book}

\usepackage[table]{xcolor} \usepackage{latexcolors} \usepackage{colortbl}

%%color definitions for alternate colors \colorlet{rbacki}{white} %background column type 1 white original style \colorlet{rbackii}{gainsboro} %background column type 2 gray original style

%\colorlet{rbacki}{whitesmoke} %background column type 1 whitesmoke new style %\colorlet{rbackii}{palecornflowerblue} %background column type 2 palecornflowerblue new style

%%%%%%%% only first and second row \colorlet{rbackiii}{copperrose} %background second row \colorlet{rbackiv}{mediumturquoise} %background first row

\colorlet{vrulei}{rbacki} %vertical rule = background column type 1

\usepackage{siunitx} % grouping, align, etc \usepackage{hhline} % \hhline \usepackage{array}

%https://alexwlchan.net/2017/10/latex-underlines/ \usepackage[normalem]{ulem} \renewcommand{\ULdepth}{2.7pt}

%%https://tex.stackexchange.com/questions/469559/bold-entries-in-table-with-s-column-type \usepackage{etoolbox} % provides robustify \robustify\bfseries % make it robust

%https://tex.stackexchange.com/questions/161032/problem-with-uline-and-table-format-in-siunitx-table \robustify\uline % make it robust \def\Uline#1{#1\llap{\uline{\phantom{#1i}}}} % +i for added space of grouping

\robustify\uuline% make it robust \def\UUline#1{#1\llap{\uuline{\phantom{#1i}}}} % +i for added space of grouping

\renewcommand{\familydefault}{\sfdefault} \begin{document}

\setlength\arrayrulewidth{1pt} % horizontal and also vertical lines

% to simplify the table code \newcommand{\rci}{{\cellcolor{rbacki}}} \newcommand{\rcii}{{\cellcolor{rbackii}}} \newcommand{\rciii}{{\cellcolor{rbackiii}}} \newcommand{\rciv}{{\cellcolor{rbackiv}}} \newcommand{\tci}{\textcolor{rbacki}} \newcommand{\vrulei}{\color{vrulei}\vrule width \arrayrulewidth}

\newcommand{\althhline}{% \hhline{% >{\arrayrulecolor{rbackii}}- >{\arrayrulecolor{rbacki}}- >{\arrayrulecolor{rbackii}}- >{\arrayrulecolor{rbacki}}- >{\arrayrulecolor{rbackii}}- } }

\newcolumntype{?}{!{\vrulei}} \renewcommand{\arraystretch}{1.3} \setlength\belowcaptionskip{8pt}

\begin{table}[htbp] \sisetup{% group-minimum-digits = 4, table-format=6.0, detect-weight=true
}
\centering \caption{With features matching the desired result.}

\begin{tabular}{%
        >{\rci}p{170pt}?
        >{\rcii}c
        >{\rci}S
        >{\rcii}S
        >{\rci}S
    }
    \rciv       &\multicolumn{1}{r}{\tci{NOTES}\rciv}   & \multicolumn{2}{?c?}{\tci{20X4}\rciv}                                             & \multicolumn{1}{r}{\tci{20X3}\rciv} \\
    \rciii      & \rciii            & \multicolumn{1}{?r?}{\tci{\$000}\rciii} & \multicolumn{1}{r?}{\tci{\$000}\rciii}  & \multicolumn{1}{r}{\tci{\$000}\rciii}  \\
    \textit{Non-current liabilities}    &   &               &               &  \\
    \althhline
    \textbf{Total liabilities}          &   &               & 8200          & 5500 \\
    \althhline
    Net assets                          &   &               & \UUline{96300}& \UUline{\bfseries 74500}  \\
    \althhline
    \textbf{Capital and reserves}       &   &               &               &  \\
    \althhline
    Share capital                       & 8 & \bfseries 70000 &             & 64000 \\
    \althhline      
    Retained profits                    & 9 & 5300          &               & 4500 \\
    \althhline      
    Reserves                            & 10&\Uline{21000}  &               & \Uline{6000} \\
    \althhline
    \textbf{Total capital and reserves} &   &               & \UUline{96300}& \UUline{\bfseries 74500} \\

\end{tabular}%
\label{tab:addlabel}%

\end{table}%

\end{document}

Close to the example

Simon Dispa
  • 39,141
1

The environment {NiceTabular} of nicematrix relies upon {tabular} (of array) but creates PGF/Tikz nodes under the cells, row and columns.

It's possible to program instructions to color alternately the columns and draw the rules as required. The following code will work wathever the number of rows and columns.

\documentclass[12pt,a4paper]{book}
\usepackage{nicematrix}
\usepackage{siunitx}
\usepackage{tikz}

\begin{document}

\ExplSyntaxOn \NewDocumentCommand \MyStyle { } { % First, the color of the columns \int_step_inline:nnn 1 { \value{jCol} } { \int_if_even:nT { ##1 } { \tikz \fill [ gray ] (1-|##1) rectangle (\int_eval:n{\value{iRow}+1}-|\int_eval:n{##1+1}) ; } } % % Now, the horizontal rules \begin { tikzpicture } \int_step_inline:nnn 2 { \value{iRow} } { \int_step_inline:nnn 1 { \value{jCol} } { \begin { pgfscope } \int_if_odd:nTF { ####1 } { \pgfsetcolor { gray } } { \pgfsetcolor { white } } \draw ([xshift=0.5\arrayrulewidth]##1-|####1) -- ([xshift=0.5\arrayrulewidth]##1-|\int_eval:n { ####1 + 1 }) ; \end { pgfscope } } } \end { tikzpicture } } \ExplSyntaxOff

\begin{NiceTabular}{cccc}[ code-before = \MyStyle ] 12 & 124 & 61 & 1 \ 465 & 1 & 23 & 2 \ 97 & 2 & 2 & 71 \ 436 & 3 & 45 & 65 \end{NiceTabular}

\bigskip \begin{NiceTabular}{*{5}{S[table-format=3.2]}}[ code-before = \MyStyle ] 12.1 & 124.24 & 61 & 1.25 & 2.234 \ 465 & 1 & 23.5 & 2 & 122.1 \ 97.23 & 2.5 & 2 & 71 & 14 \ 436 & 3 & 145.23 & 65 & 7.5 \end{NiceTabular}

\end{document}

Ouput of the above code

F. Pantigny
  • 40,250