25

I would like to have the first row of all my tables colored with a certain color (below yellow) but somehow below thing is not working as expected

This:

\documentclass[a4paper, 12pt]{report}
\usepackage{colortbl}

\begin{document}

\begin{tabular*}{0.90\textwidth}{@{\extracolsep{\fill}}ll}
\rowcolor{yellow}
OS & Features \\ \hline
iOS & blah blah.\\
Android & blah blah.\\
\end{tabular*}

\end{document}

Results in this:

enter image description here

Which is undesired because of the blank space between OS and Features Additionally I would like to make the hline a bit wider so it matches the width of the yellow area, or make the yellow area a bit narrower so it matches the horizontal line. How can i do this?

Any help is high appreciated.

Moriambar
  • 11,466
nacho4d
  • 11,093

3 Answers3

25

use package tabularx instead, it is a better choice than the star version of tabular

\documentclass[12pt]{report}
\usepackage{tabularx,colortbl}

\begin{document}

\begin{tabularx}{0.90\textwidth}{Xl}
\rowcolor{yellow}
OS      & Features \\ \hline
iOS     & blah blah.\\
Android & blah blah.\\
\end{tabularx}

\end{document}

enter image description here

Moriambar
  • 11,466
  • This works across columns, but not so well across rows. It aligns well between \hline, but doesn't align well with \toprule or \bottomrule from the (very popular) booktabs package (there is some white space between the rules and the color). – PatrickT Mar 03 '24 at 13:49
15

I do not know how to patch \rowcolor (and its children), but I provide a new \Rowcolor[<pad>]{<color>} macro:

  • <color> stands for the color used, and
  • <pad> for the padding before the first column (usual \tabcolsep, which is the default) This is the width of the stuff that is @{dded} before the first row.

A note on the latter sentence: You specified @{\extracolsep{\fill}}ll which removed the \tabcolsep before the first column. The original \rowcolor can’t handle that as well, see your picture, which is the reason you asked for making the rule longer when you have in fact said, that it should be shorter! (More on that: colortbl: \rowcolor in tables with \begin{tabular}{@{}ccc@{}})

If you use something like @{}<…>, you need to use \Rowcolor[]{<color>} (which is the same as \Rowcolor[0pt]{<color>}).

Code

\documentclass[a4paper, 12pt]{report}
\usepackage{colortbl}
\usepackage{etoolbox}
\makeatletter
\newlength{\qrr@dimen@}
\expandafter\pretocmd\csname tabular*\endcsname{\setlength{\qrr@dimen@}{#1}}{}{}
\newcommand*{\Rowcolor}[2][\tabcolsep]{%
    \ifx\relax#1\relax\else
        \kern-\the\dimexpr#1\relax
    \fi
    \makebox[0pt][l]{%
        \fboxsep=0pt
        \colorbox{#2}{%
            \strut\kern\qrr@dimen@
        }%
    }%
    \ifx\relax#1\relax\else
        \kern\the\dimexpr#1\relax
    \fi
    \ignorespaces
}
\makeatother
\begin{document}
\noindent
\begin{tabular*}{.9\linewidth}{l@{\extracolsep{\fill}}l}
    \rowcolor{yellow} \multicolumn{1}{>{\columncolor{green}[\tabcolsep][\tabskip]}l}{OS} &  Features   \\ \hline
    \Rowcolor{yellow} OS &  Features   \\ \hline
    iOS                  &  blah blah. \\
    Android              &  blah blah. \\
\end{tabular*}
\end{document}

Output

enter image description here

Moriambar
  • 11,466
Qrrbrbirlbel
  • 119,821
  • who's setting \dimen@ there? (seems to work but???) – David Carlisle Jan 07 '13 at 10:01
  • @DavidCarlisle \tabular* does: \setlength\dimen@{#1} – Qrrbrbirlbel Jan 07 '13 at 14:14
  • so it does, sneaky (and of course bad style to assume a scratch register is safe not that there is any alternative here:-). I had a horrible feeling it might have been me that added that setting but it was Rainer so I might be forgiven for forgetting:-) 1998-05-13 Rainer Schoepf latex-bugs@latex-project.org
    • lttab.dtx Use \setlength to set size of p-column and for

    argument of tabular* environment, so that the benefits of the calc package apply. Note this won't work if the first column is a p column as \dimen@ will then be the column width as noted in Rainer's change log

    – David Carlisle Jan 07 '13 at 14:33
  • @DavidCarlisle Well, there is one alternative: I have updated my answer with an extra dimension. Anyway, I have never used used tabular* before; I think that the tabularx environment is superior too. – Qrrbrbirlbel Jan 08 '13 at 22:17
1

You can use {NiceTabular*} of nicematrix (and the key colortbl-like in order to specify the color of the row with the same syntax as colortbl). You have directly the expected output.

\documentclass[a4paper, 12pt]{report}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{0.90\textwidth}{@{\extracolsep{\fill}}ll}[colortbl-like] \rowcolor{yellow} OS & Features \ \hline iOS & blah blah.\ Android & blah blah.\ \end{NiceTabular}

\end{document}

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

Output of the above code

F. Pantigny
  • 40,250