2

I load xcolor with option table, to get alternating row colors

However, I can't remove the color padding before the text of the first column (when using @{}.

In xcolor documentation, I see options for controlling it with the \columncolor command, but NOT with the \rowcolors. Here is the improper table (image below):

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

Left corner of the table

I tried :

  1. \rowcolors{1}{}{gray!25}[0pt] but the command does not take options

  2. defining my first column with @{\columncolor{}[0pt][\tabcolsep]}p{21mm}, but it keeps complaining:

     ! Undefined control sequence.
     <template> ...rstrutbox \d@llarbegin \columncolor 
                                                   {}[0pt][\tabcolsep]\relax \d@llar...
     l.30 \end{tabularx}
    
  3. @{\setlength{\tabcolsep}{0pt}}p{21mm}: no effect.

  4. Inserting \fct before the row throws an "undefined control sequence" as well, although based on this answer, this is exactly what I need.

     ! Undefined control sequence.
     <recently read> \fct
    

    l.30 \end{tabularx}

These answers didn't help:

https://tex.stackexchange.com/a/365988/95423

https://tex.stackexchange.com/a/31704/95423

  • Entirely unrelated to the issue itself but are you aware that your table is a lot wider than the textwidth? Also some of the column headers are in fact wider than the corresponding columns causeing cut/ overlapping text and quite a lot of overfull box warnings. – leandriis Jul 15 '20 at 10:38
  • Regarding: "Inserting \fct before the row throws an "undefined control sequence"", it might be useful to show which error message you got specifically. Which control sequence is undefined? – leandriis Jul 15 '20 at 10:41
  • @leandriis oh sorry about the width, it's expected in the original document (where \textwidth is 18.5 cm). + I added the \fct error. – PlasmaBinturong Jul 15 '20 at 10:42
  • If you remove @{} at begin column specification, it will work as you expect. However, if you like to have it, than see https://tex.stackexchange.com/questions/365685/row-colour-gaps-in-tabularx-with-aboverulesep-and/365698#365698 – Zarko Jul 15 '20 at 10:53
  • the width is too large, but I use \centerfloat to expand into both margins. – PlasmaBinturong Jul 15 '20 at 11:17

3 Answers3

2

Here are two versions of your table.

Since you mention getting ! Undefined control sequence. <recently read> \fct when trying to use \fct inside of your table, please be aware, that you also have to add the definition of \cft into your document. In the following example, I have done so and also adapted the definition according to your color definition.

Since the table is wider than the textwidth, I have also added a second example in which I have shortened the column headers. Their meaning is exaplained below the table. Additionally, I have eused siunitx in order to align the numbers with respect to the decimal separator. (In the comments you mentioned having a wider textblock than in this example, but using \begin{tabularx}{1.2\textwidth}will always result in a table that is wider than the textwidth, regardless of the actual value of textwidth.)

enter image description here

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

leandriis
  • 62,593
1

I rename \fct to more meaningful name, and also fix the color overhang at the last column.

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

enter image description here

muzimuzhi Z
  • 26,474
1

With {NiceTabularX} of nicematrix (≥ 6.21 of 2023-07-14), you have no problem with color padding.

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

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250