为表格的每一行添加不同颜色的线

为表格的每一行添加不同颜色的线

\hline使用下面提供的示例可以获取彩色的s。但是,此实现存在几个问题:

  1. 每种颜色都需要一个\newcommand,然后必须用它来替换\\通常位于每个表格行末尾的。
  2. \arrayrulecolor需要将全局设置的颜色\newcommand复制到每个颜色中以重置颜色。

还有哪些其他选项可用于本地设置或\hline的颜色?longtabletabular

\documentclass[a4paper]{book}
\usepackage{xcolor}             %need xcolor because otherwise color definitions won't work
\usepackage{colortbl}
\usepackage{longtable}
\usepackage{rotating}

%tables: change color of table lines
\arrayrulecolor{gray!50}
%update the newline command to have a line at each row
\newcommand\tgln{\tabularnewline\hline}
%update the newline command to have a blue line at each row
\newcommand\tbln{\tabularnewline\arrayrulecolor{blue!30}\hline\arrayrulecolor{gray!50}}

\newcommand{\bs}{\begin{sideways}}
\newcommand{\es}{\end{sideways}}

\begin{document}
\scriptsize
\begin{longtable}{|p{2.2cm}cp{2cm}c|p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}|}
    \caption{Overview of a table with a lot of numbers.}\label{tbl:numbers} \\
    \hline
    Name &  &Location   &Number 1   & \multicolumn{7}{c|}{Items} \\
                        &   &                   &               &\bs Item 1 \es &\bs Item 2 \es &\bs Item 3 \es &\bs Item 4 \es &\bs Item 5 \es &\bs Item 6 \es &\bs Item 7 \es \\
    \hline \endhead
    \hline \endfoot
    \hline \endlastfoot
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tgln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tgln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tgln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tgln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tgln
\end{longtable}

\begin{longtable}{|p{2.2cm}cp{2cm}c|p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}p{0.15cm}|}
    \caption{Overview of a table with a lot of numbers.}\label{tbl:numbers} \\
    \hline
    Name &  &Location   &Number 1   & \multicolumn{7}{c|}{Items} \\
                        &   &                   &               &\bs Item 1 \es &\bs Item 2 \es &\bs Item 3 \es &\bs Item 4 \es &\bs Item 5 \es &\bs Item 6 \es &\bs Item 7 \es \\
    \hline \endhead
    \hline \endfoot
    \hline \endlastfoot
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tbln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tbln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tbln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tbln
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tbln
\end{longtable}

\end{document}

答案1

带有颜色参数的命令怎么样?

\newcommand\tln[1]{\tabularnewline\arrayrulecolor{#1}\hline}

然后在你的桌子上

Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tln{green}
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tln{red}
Some name    &Sometext   &Sometext  &100    &   &x  &   &   &   &   &\tln{blue}

相关内容