这应该是一个简单的问题。我正在使用\rowcolors{1}{}{lightgray}
交替颜色来改变表格中的行。
有没有办法在某个点之后“重新启动”重新着色,以便我确保无论上面有多少行,我都可以保证交替行为从给定的预设行开始恢复?
梅威瑟:
\documentclass[11pt]{article}
\usepackage{threeparttable} % tables with footnotes, capions all the same width
\usepackage{dcolumn} % decimal-aligned tabular math columns
\usepackage{multirow} % Allow table cells to span multiple rows
\usepackage{booktabs} % Formatting options for publication-quality tables
\usepackage{ltxtable} % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable}
\end{document}
测试表.tex:
\begin{longtable}{p{3cm} X}
\toprule
Heading row & This should be lightgrey \\
\hline % I use \hline because \midrule counts as a row and screws with the colors
A data row & This row should be uncolored \\
A data row & This row should be lightgrey \\
A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
\hline
A new heading row & This should be lightgrey regardless of whether the previous row exists.
\end{longtable}
答案1
软件包xcolor
通过计数寄存器记住彩色行的状态\rownum
。可以通过以下方式将其重置为初始值:
\global\rownum=0\relax
更改当前行中的计数寄存器已经太迟了。因此需要在前一行的单元格中执行此操作。
以下示例添加了一个 LaTeX 接口,以\rownum
使宏\setcounter
、\addtocounter
和\therownum
工作。然后,计数器在标记为 的行之前的行中重置restart
:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{array}
% LaTeX counter interface for \rownum
% ---
\makeatletter
\@ifundefined{c@rownum}{%
\let\c@rownum\rownum
}{}
\@ifundefined{therownum}{%
\def\therownum{\@arabic\rownum}%
}{}
\makeatother
\begin{document}
\rowcolors{1}{yellow}{red}
\begin{tabular}{>{\the\rownum}ll}
\hline
&\\
&\\
&\\
&\setcounter{rownum}{0}\\
\hline
\hline
& restart\\
&\\
&\setcounter{rownum}{0}\\
\hline
\hline
& restart\\
&\setcounter{rownum}{0}\\
\hline
\hline
& restart\\
&\\
&\\
\hline
\end{tabular}
\end{document}
评论:
\rowcolors{1}{yellow}{red}
意思是,从第一行开始,改变行颜色。第一种颜色用于奇数行,第二种颜色用于偶数行。
包裹longtable
更“有趣”的是包longtable
。它需要LTchunksize
行来测量表格单元格的宽度。因此可以处理一行一些次。这会影响\rownum
。前面的示例使用\usepackage{longtable}
和longtable
代替tabular
:
或者使用\setcounter{LTchunksize}{1}
(和几次 LaTeX 运行):
已经longtable
给行编号带来了麻烦,因此使用 不会变得更好ltxtable
,因为它基于longtable
。在下面的示例中,第一行获得编号 2,因此 的第一个参数\rowcolors
将无法可靠地工作,除非将其设置为 1。
如果在前一行进行“重置”,则在某种程度上“重置”是有效的。但是,根据规定,第一行应该是白色而不是浅灰色,\rowcolors
并且行编号无法正常工作。
\RequirePackage{filecontents}
\begin{filecontents}{testtable1.tex}
\begin{longtable}{>{\the\rownum.}r p{3.5cm} X}
\toprule
& Heading row & This should be lightgrey \\
\hline
& A data row & This row should be uncolored \\
& A data row & This row should be lightgrey \\
& A data row & This row sometimes doesn't exist.
It would be uncolored if it did
\restartrowcolors\\
\midrule
& A new heading row & This should be lightgrey regardless
of whether the previous row exists.\\
& A data row & This should be uncolored \\
& A data row & This should be lightgrey
\end{longtable}
\end{filecontents}
\begin{filecontents}{testtable2.tex}
\begin{longtable}{>{\the\rownum.}r p{3.5cm} X >{\the\rownum}l}
\toprule
& Heading row & This should be lightgrey \\
\hline
& A data row & This row should be uncolored \\
& A data row & This row should be lightgrey
\restartrowcolors\\
\midrule
& A new heading row & This should be lightgrey regardless
of whether the previous row exists.\\
& A data row & This should be uncolored \\
& A data row & This should be lightgrey
\end{longtable}
\end{filecontents}
\documentclass[11pt]{article}
\usepackage{booktabs} % Formatting options for publication-quality tables
\usepackage{ltxtable} % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\newcommand*{\restartrowcolors}{%
\ifhmode\unskip\fi
\vadjust{%
\global\rownum=0 %
}%
}
\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable1}
\LTXtable{\textwidth}{testtable2}
\end{document}
答案2
无论之前的行颜色定义如何,你都可以简单地rowcolor{lightgray}
在想要着色的行之前说出来。
如果前一行不存在:
\documentclass[11pt]{article}
\usepackage{threeparttable} % tables with footnotes, capions all the same width
\usepackage{dcolumn} % decimal-aligned tabular math columns
\usepackage{multirow} % Allow table cells to span multiple rows
\usepackage{booktabs} % Formatting options for publication-quality tables
\usepackage{ltxtable} % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{filecontents}
\begin{filecontents*}{testtable.tex}
\rowcolors{1}{white}{lightgray}
\begin{longtable}{p{3cm} X}
\toprule
Heading row & This should be lightgrey \\
\hline % I use \hline because \midrule counts as a row and screws with the colors
A data row & This row should be uncolored \\
A data row & This row should be lightgrey \\
% A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
% \hline
\rowcolor{lightgray}
\rowcolors{5}{white}{lightgray}
A new heading row & This should be lightgrey regardless of whether the previous row exists.\\
A data row & This row should be uncolored \\
A data row & This row should be lightgrey \\
A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
\end{longtable}
\end{filecontents*}
\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable}
\end{document}
从第四行开始颜色是交替的。
如果前一行存在:
\documentclass[11pt]{article}
\usepackage{threeparttable} % tables with footnotes, capions all the same width
\usepackage{dcolumn} % decimal-aligned tabular math columns
\usepackage{multirow} % Allow table cells to span multiple rows
\usepackage{booktabs} % Formatting options for publication-quality tables
\usepackage{ltxtable} % long tabularx
\usepackage{colortbl}
\usepackage[table]{xcolor}
\definecolor{lightgray}{gray}{0.9}
\usepackage{filecontents}
\begin{filecontents*}{testtable.tex}
\rowcolors{1}{white}{lightgray}
\begin{longtable}{p{3cm} X}
\toprule
Heading row & This should be lightgrey \\
\hline % I use \hline because \midrule counts as a row and screws with the colors
A data row & This row should be uncolored \\
A data row & This row should be lightgrey \\
A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
\hline
%\rowcolor{lightgray}
%\rowcolors{4}{white}{lightgray}
A new heading row & This should be lightgrey regardless of whether the previous row exists.\\
A data row & This row should be uncolored \\
A data row & This row should be lightgrey \\
A data row & This row sometimes doesn't exist. It would be uncolored if it did \\
\end{longtable}
\end{filecontents*}
\begin{document}
\rowcolors{1}{}{lightgray}
\LTXtable{\textwidth}{testtable}
\end{document}