表格背景规则颜色

表格背景规则颜色

在 LaTeX 中,是否可以设置表格替代规则颜色,即第一列背景颜色为灰色,然后 hline 规则颜色为白色(hline),如果第二列背景颜色为白色,则 hline 规则颜色为灰色?在此处输入图片描述

梅威瑟:

\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}

答案1

您可以使用以下编辑器快速获取表格和此类定制:https://www.latex-tables.com;您可以在页面右侧设置边框颜色。

或者尝试设置每个框的颜色,并采用如下脚本:

\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}

答案2

我试图忠实地再现问题中作为示例显示的表格的所有特征(可能是更大表格的一小部分),而不仅仅是行和水平线的交替颜色。

虽然它可能不符合技术出版物所接受的严谨风格,但它非常适合财务或公司报告。在这些环境中,不仅交替颜色列,而且数字分组和下划线也广泛使用。只是为了好玩,我添加了粗体。

S 列允许分组,但随后必须使用robustify粗体和下划线命令。下划线的长度必须延长,以适应数字分组引入的额外空间。

即使目前没有包可以处理线颜色匹配交替列,该代码也可以用作模板并且易于修改。

\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}

接近示例

答案3

{NiceTabular}的环境nicematrix依赖于{tabular}(的array),但在单元格、行和列下创建 PGF/Tikz 节点。

可以编写指令来交替为列着色并根据需要绘制规则。无论行数和列数是多少,以下代码都可以工作。

\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}

上述代码的输出

相关内容