23

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.

3
23

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

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

4
  • who's setting \dimen@ there? (seems to work but???) Jan 7 '13 at 10:01
  • @DavidCarlisle \tabular* does: \setlength\dimen@{#1} Jan 7 '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 Jan 7 '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. Jan 8 '13 at 22:17
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.