我加载xcolor
选项table
,以获得交替行颜色
但是,我无法删除第一列文本之前的颜色填充(使用时)@{}
。
在 xcolor 文档中,我看到了使用命令控制它的选项\columncolor
,但没有看到使用 的选项\rowcolors
。以下是不正确的表格(下图):
\PassOptionsToPackage{dvipsnames,svgnames*,x11names*,table}{xcolor}
\documentclass[12pt,a4paper]{report}
\usepackage{tabularx,longtable} % longtable is used in other places of the full document.
\usepackage{booktabs}
\usepackage{colortbl} % tabularx complains that \columncolor is undefined
\usepackage{xcolor}
\begin{document}
\begin{table}[h]
\rowcolors{1}{}{gray!25}%[0pt]
\footnotesize%
\begin{tabularx}{1.2\linewidth}{@{}p{21mm}*{2}{p{9mm}}p{12mm}p{14mm} *{3}{X} p{9mm} X@{}}
\toprule
{} & Ref & van Muut 2012 & (1)\newline original\newline siteMPL & (2)\newline original\newline branchMPL & (3)\newline cleaned\newline branchMPL & (4)\newline fsa\newline branchMPL & (5)\newline fsa+cleaned\newline branchMPL & (6)\newline fsa\newline Beast & (7)\newline fsa+cleaned\newline Beast \tabularnewline
\midrule
Cebidae & 16,07 & 19,10 & 18,60 & 19,97 & 19,61 & 19,60 & 18,11 & 13,84 & 12,35 \tabularnewline
Platyrrhini & 19,68 & 25,30 & 22,67 & 22,69 & 22,41 & 22,39 & 20,71 & 18,79 & 17,41 \tabularnewline
Pan & 2,82 & 2,70 & 1,41 & 1,37 & 1,29 & 1,31 & 1,17 & 1,55 & 1,31 \tabularnewline
HomoPan & 6,65 & 7,90 & 4,51 & 4,50 & 4,28 & 4,32 & 3,83 & 4,12 & 3,62 \tabularnewline
Homininae & 9,06 & 10,50 & 6,24 & 6,15 & 5,96 & 5,96 & 5,52 & 6,02 & 5,36 \tabularnewline
Hominidae & 15,76 & 20,30 & 13,06 & 13,00 & 12,69 & 12,66 & 11,79 & 11,74 & 10,37 \tabularnewline
Hominoidea & 20,19 & 24,00 & 16,37 & 15,78 & 15,32 & 15,26 & 14,63 & 14,93 & 13,46 \tabularnewline
Catarrhini & 29,44 & 32,60 & 27,60 & 27,27 & 26,69 & 26,71 & 24,71 & 24,47 & 22,67 \tabularnewline
Cercopithecidae & 19,42 & 21,90 & 14,71 & 14,39 & 13,56 & 13,54 & 12,21 & 11,79 & 10,66 \tabularnewline
Cercopithecinae & 13,75 & 16,20 & 10,40 & 10,15 & 9,33 & 9,34 & 8,15 & 7,94 & 7,10 \tabularnewline
Papionini & 12,40 & 13,20 & 6,58 & 6,42 & 5,82 & 5,78 & 4,79 & 5,67 & 5,04 \tabularnewline
Macaca & 5,28 & 8,50 & 2,16 & 2,23 & 2,04 & 2,07 & 1,84 & 2,49 & 2,17 \tabularnewline
\bottomrule
\end{tabularx}
\caption{Median speciation ages by procedure, in My.}\label{tab:median-ages}
\end{table}
\end{document}
我试过 :
\rowcolors{1}{}{gray!25}[0pt]
但命令不接受选项用 定义我的第一列
@{\columncolor{}[0pt][\tabcolsep]}p{21mm}
,但它一直在抱怨:! Undefined control sequence. <template> ...rstrutbox \d@llarbegin \columncolor {}[0pt][\tabcolsep]\relax \d@llar... l.30 \end{tabularx}
@{\setlength{\tabcolsep}{0pt}}p{21mm}
: 没有效果。在行之前插入
\fct
也会引发“未定义的控制序列”,尽管基于这个答案,这正是我需要的。! Undefined control sequence. <recently read> \fct l.30 \end{tabularx}
这些答案没有帮助:
答案1
这是您的表格的两个版本。
由于您! Undefined control sequence. <recently read> \fct
在尝试\fct
在表格中使用时提到获取,请注意,您还必须将的定义添加\cft
到文档中。在下面的例子中,我已经这样做了,并且还根据您的颜色定义调整了定义。
由于表格比文本宽度更宽,我还添加了第二个示例,其中我缩短了列标题。它们的含义在表格下方解释。此外,我使用了siunitx
以便将数字与小数点分隔符对齐。(在评论中,您提到文本块比此示例更宽,但使用\begin{tabularx}{1.2\textwidth}
总是会导致表格比文本宽度更宽,无论文本宽度的实际值是多少。)
\PassOptionsToPackage{dvipsnames,svgnames*,x11names*,table}{xcolor}
\documentclass[12pt,a4paper]{report}
\usepackage{tabularx,longtable} % longtable is used in other places of the full document.
\usepackage{booktabs}
\usepackage{xcolor}
%%% added for the second example
\usepackage{siunitx}
\usepackage{makecell}
\newcommand*{\fct}[1]{\multicolumn{1}{>{\columncolor{gray!25}\hspace*{-\tabcolsep}}l}{#1}}
\begin{document}
\begin{table}[h]
\rowcolors{1}{}{gray!25}%[0pt]
\footnotesize%
\begin{tabularx}{1.2\linewidth}{@{}l*{2}{p{9mm}}p{12mm}p{14mm} *{3}{X} p{9mm} X@{}}
\toprule
{} & Ref & van Muut 2012 & (1)\newline original\newline siteMPL & (2)\newline original\newline branchMPL & (3)\newline cleaned\newline branchMPL & (4)\newline fsa\newline branchMPL & (5)\newline fsa+cleaned\newline branchMPL & (6)\newline fsa\newline Beast & (7)\newline fsa+cleaned\newline Beast \tabularnewline
\midrule
\fct{Cebidae} & 16,07 & 19,10 & 18,60 & 19,97 & 19,61 & 19,60 & 18,11 & 13,84 & 12,35 \tabularnewline
Platyrrhini & 19,68 & 25,30 & 22,67 & 22,69 & 22,41 & 22,39 & 20,71 & 18,79 & 17,41 \tabularnewline
\fct{Pan } & 2,82 & 2,70 & 1,41 & 1,37 & 1,29 & 1,31 & 1,17 & 1,55 & 1,31 \tabularnewline
HomoPan & 6,65 & 7,90 & 4,51 & 4,50 & 4,28 & 4,32 & 3,83 & 4,12 & 3,62 \tabularnewline
\fct{Homininae} & 9,06 & 10,50 & 6,24 & 6,15 & 5,96 & 5,96 & 5,52 & 6,02 & 5,36 \tabularnewline
Hominidae & 15,76 & 20,30 & 13,06 & 13,00 & 12,69 & 12,66 & 11,79 & 11,74 & 10,37 \tabularnewline
\fct{Hominoidea} & 20,19 & 24,00 & 16,37 & 15,78 & 15,32 & 15,26 & 14,63 & 14,93 & 13,46 \tabularnewline
Catarrhini & 29,44 & 32,60 & 27,60 & 27,27 & 26,69 & 26,71 & 24,71 & 24,47 & 22,67 \tabularnewline
\fct{Cercopithecidae} & 19,42 & 21,90 & 14,71 & 14,39 & 13,56 & 13,54 & 12,21 & 11,79 & 10,66 \tabularnewline
Cercopithecinae & 13,75 & 16,20 & 10,40 & 10,15 & 9,33 & 9,34 & 8,15 & 7,94 & 7,10 \tabularnewline
\fct{Papionini} & 12,40 & 13,20 & 6,58 & 6,42 & 5,82 & 5,78 & 4,79 & 5,67 & 5,04 \tabularnewline
Macaca & 5,28 & 8,50 & 2,16 & 2,23 & 2,04 & 2,07 & 1,84 & 2,49 & 2,17 \tabularnewline
\bottomrule
\end{tabularx}
\caption{Median speciation ages by procedure, in My.}\label{tab:median-ages}
\end{table}
\begin{table}[h]
\footnotesize%
\sisetup{input-ignore={.},
input-decimal-markers={,},
output-decimal-marker = {,}}
\setlength{\tabcolsep}{0pt}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}l*{9}{S}}
\toprule
{}
& {\thead{Ref}} & {\thead{van Muut\\ 2012}} & {(1)}& {(2)} & {(3)} & {(4)} & {(5)} & {(6)} & {(7)} \\
\midrule
Cebidae & 16,07 & 19,10 & 18,60 & 19,97 & 19,61 & 19,60 & 18,11 & 13,84 & 12,35 \\
Platyrrhini & 19,68 & 25,30 & 22,67 & 22,69 & 22,41 & 22,39 & 20,71 & 18,79 & 17,41 \\
Pan & 2,82 & 2,70 & 1,41 & 1,37 & 1,29 & 1,31 & 1,17 & 1,55 & 1,31 \\
HomoPan & 6,65 & 7,90 & 4,51 & 4,50 & 4,28 & 4,32 & 3,83 & 4,12 & 3,62 \\
Homininae & 9,06 & 10,50 & 6,24 & 6,15 & 5,96 & 5,96 & 5,52 & 6,02 & 5,36 \\
Hominidae & 15,76 & 20,30 & 13,06 & 13,00 & 12,69 & 12,66 & 11,79 & 11,74 & 10,37 \\
Hominoidea & 20,19 & 24,00 & 16,37 & 15,78 & 15,32 & 15,26 & 14,63 & 14,93 & 13,46 \\
Catarrhini & 29,44 & 32,60 & 27,60 & 27,27 & 26,69 & 26,71 & 24,71 & 24,47 & 22,67 \\
Cercopithecidae & 19,42 & 21,90 & 14,71 & 14,39 & 13,56 & 13,54 & 12,21 & 11,79 & 10,66 \\
Cercopithecinae & 13,75 & 16,20 & 10,40 & 10,15 & 9,33 & 9,34 & 8,15 & 7,94 & 7,10 \\
Papionini & 12,40 & 13,20 & 6,58 & 6,42 & 5,82 & 5,78 & 4,79 & 5,67 & 5,04 \\
Macaca & 5,28 & 8,50 & 2,16 & 2,23 & 2,04 & 2,07 & 1,84 & 2,49 & 2,17 \\
\bottomrule
\end{tabular*}
(1) = original siteMPL, (2) = original branchMPL, (3) = cleaned branchMPL, (4) = fsa branchMPL, (5) = fsa+cleaned branchMPL, (6) = fsa Beast, (7) = fsa+cleaned Beast
\caption{Median speciation ages by procedure, in My.}\label{tab:median-ages}
\end{table}
\end{document}
答案2
我将其重命名\fct
为更有意义的名称,并且修复了最后一列的颜色悬垂。
\PassOptionsToPackage{dvipsnames,svgnames*,x11names*,table}{xcolor}
\documentclass[12pt,a4paper]{report}
\usepackage{tabularx,longtable} % longtable is used in other places of the full document.
\usepackage{booktabs}
\usepackage{xcolor}
\newcommand*{\killLeftColsep}[2]{%
\multicolumn{1}{>{\hspace*{-\tabcolsep}}#1}{#2}}
\newcommand*{\killRightColsep}[2]{%
\multicolumn{1}{#1<{\hspace*{-\tabcolsep}}}{#2}}
\newcommand*{\klcl}{\killLeftColsep{l}}
\newcommand*{\krcl}{\killRightColsep{l}}
\begin{document}
\begin{table}[h]
\rowcolors{1}{}{gray!25}
\footnotesize%
\begin{tabularx}{1.2\linewidth}{@{}l *{2}{p{9mm}} p{12mm} p{14mm} *{3}{X} p{9mm} X@{}}
\toprule
{} & Ref
& van Muut 2012
& (1)\newline original\newline siteMPL
& (2)\newline original\newline branchMPL
& (3)\newline cleaned\newline branchMPL
& (4)\newline fsa\newline branchMPL
& (5)\newline fsa+cleaned\newline branchMPL
& (6)\newline fsa\newline Beast
& (7)\newline fsa+cleaned\newline Beast \\ \midrule
\klcl{Cebidae} & 16,07 & 19,10 & 18,60 & 19,97 & 19,61 & 19,60 & 18,11 & 13,84 & \krcl{12,35} \\
Platyrrhini & 19,68 & 25,30 & 22,67 & 22,69 & 22,41 & 22,39 & 20,71 & 18,79 & \krcl{17,41} \\
\klcl{Pan} & 2,82 & 2,70 & 1,41 & 1,37 & 1,29 & 1,31 & 1,17 & 1,55 & \krcl{1,31} \\
HomoPan & 6,65 & 7,90 & 4,51 & 4,50 & 4,28 & 4,32 & 3,83 & 4,12 & \krcl{3,62} \\
\klcl{Homininae} & 9,06 & 10,50 & 6,24 & 6,15 & 5,96 & 5,96 & 5,52 & 6,02 & \krcl{5,36} \\
Hominidae & 15,76 & 20,30 & 13,06 & 13,00 & 12,69 & 12,66 & 11,79 & 11,74 & \krcl{10,37} \\
\klcl{Hominoidea} & 20,19 & 24,00 & 16,37 & 15,78 & 15,32 & 15,26 & 14,63 & 14,93 & \krcl{13,46} \\
Catarrhini & 29,44 & 32,60 & 27,60 & 27,27 & 26,69 & 26,71 & 24,71 & 24,47 & \krcl{22,67} \\
\klcl{Cercopithecidae} & 19,42 & 21,90 & 14,71 & 14,39 & 13,56 & 13,54 & 12,21 & 11,79 & \krcl{10,66} \\
Cercopithecinae & 13,75 & 16,20 & 10,40 & 10,15 & 9,33 & 9,34 & 8,15 & 7,94 & \krcl{7,10} \\
\klcl{Papionini} & 12,40 & 13,20 & 6,58 & 6,42 & 5,82 & 5,78 & 4,79 & 5,67 & \krcl{5,04} \\
Macaca & 5,28 & 8,50 & 2,16 & 2,23 & 2,04 & 2,07 & 1,84 & 2,49 & \krcl{2,17} \\ \bottomrule
\end{tabularx}
\caption{Median speciation ages by procedure, in My.}\label{tab:median-ages}
\end{table}
\end{document}
答案3
使用(≥ 2023-07-14{NiceTabularX}
的nicematrix
6.21),颜色填充不会有问题。
\PassOptionsToPackage{dvipsnames,svgnames*,x11names*,table}{xcolor}
\documentclass[12pt,a4paper]{report}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{nicematrix}
\begin{document}
\begin{table}[h]
\footnotesize%
\begin{NiceTabularX}{1.2\linewidth}{@{}p{21mm}*{2}{p{9mm}}p{12mm}p{14mm} *{3}{X} p{9mm} X@{}}[color-inside]
\toprule
{} & Ref & van Muut 2012 & (1)\newline original\newline siteMPL & (2)\newline original\newline branchMPL & (3)\newline cleaned\newline branchMPL & (4)\newline fsa\newline branchMPL & (5)\newline fsa+cleaned\newline branchMPL & (6)\newline fsa\newline Beast & (7)\newline fsa+cleaned\newline Beast \tabularnewline
\midrule
\rowcolors{gray!25}{}
Cebidae & 16,07 & 19,10 & 18,60 & 19,97 & 19,61 & 19,60 & 18,11 & 13,84 & 12,35 \tabularnewline
Platyrrhini & 19,68 & 25,30 & 22,67 & 22,69 & 22,41 & 22,39 & 20,71 & 18,79 & 17,41 \tabularnewline
Pan & 2,82 & 2,70 & 1,41 & 1,37 & 1,29 & 1,31 & 1,17 & 1,55 & 1,31 \tabularnewline
HomoPan & 6,65 & 7,90 & 4,51 & 4,50 & 4,28 & 4,32 & 3,83 & 4,12 & 3,62 \tabularnewline
Homininae & 9,06 & 10,50 & 6,24 & 6,15 & 5,96 & 5,96 & 5,52 & 6,02 & 5,36 \tabularnewline
Hominidae & 15,76 & 20,30 & 13,06 & 13,00 & 12,69 & 12,66 & 11,79 & 11,74 & 10,37 \tabularnewline
Hominoidea & 20,19 & 24,00 & 16,37 & 15,78 & 15,32 & 15,26 & 14,63 & 14,93 & 13,46 \tabularnewline
Catarrhini & 29,44 & 32,60 & 27,60 & 27,27 & 26,69 & 26,71 & 24,71 & 24,47 & 22,67 \tabularnewline
Cercopithecidae & 19,42 & 21,90 & 14,71 & 14,39 & 13,56 & 13,54 & 12,21 & 11,79 & 10,66 \tabularnewline
Cercopithecinae & 13,75 & 16,20 & 10,40 & 10,15 & 9,33 & 9,34 & 8,15 & 7,94 & 7,10 \tabularnewline
Papionini & 12,40 & 13,20 & 6,58 & 6,42 & 5,82 & 5,78 & 4,79 & 5,67 & 5,04 \tabularnewline
Macaca & 5,28 & 8,50 & 2,16 & 2,23 & 2,04 & 2,07 & 1,84 & 2,49 & 2,17 \tabularnewline
\bottomrule
\end{NiceTabularX}
\caption{Median speciation ages by procedure, in My.}\label{tab:median-ages}
\end{table}
\end{document}
您需要多次编译(因为nicematrix
在后台使用 PGF/Tikz 节点)。