4

I want to highlight a few rows in a table, and used \rowcolor as per the first table. Well, this results in the row coloring being wider than I desire, so I thought the simple fix would be to adjust the overhang of the row via the optional parameter

\rowcolor{cyan!20}[1pt]

But, this yields the result shown in the second table. The overhang was applied to each cell -- I just want to control the overhang of the entire row, not each cell. That is, I want a solid colored row, but with the overhang adjusted on the left and the right of the row.

enter image description here

Code:

\documentclass{article}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{colortbl}

\begin{document}

\begin{tabular}{clc}
$n$ & Text & $f(n)$ \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\rowcolor{cyan!20}
1   & abc    & 2      \\
2   & abc    & 1      \\ \rowcolor{cyan!20}
7   & abc    & 3      \\
8   & abc    & 2      \\
9   & abc    & 1      \\
10  & abc    & 2      \\
11  & abc    & 2      \\
\bottomrule
\end{tabular}
\quad
\begin{tabular}{clc}
$n$ & Text & $f(n)$ \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\rowcolor{cyan!20}[1pt]%  <---- only change from above is the "[0pt]"
1   & abc    & 2      \\
2   & abc    & 1      \\ \rowcolor{cyan!20}
7   & abc    & 3      \\
8   & abc    & 2      \\
9   & abc    & 1      \\
10  & abc    & 2      \\
11  & abc    & 2      \\
\bottomrule
\end{tabular}
\end{document}
  • 6
    That's a bit rubbish isn't it: who designed that interface? – David Carlisle Jun 25 '14 at 20:41
5

\rowcolor is implemented as "do this column colour in every cell of the row" which isn't what you want for the overhang, so:

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{colortbl}

\begin{document}

\begin{tabular}{clc}
$n$ & Text & $f(n)$ \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\rowcolor{cyan!20}
1   & abc    & 2      \\
2   & abc    & 1      \\ \rowcolor{cyan!20}
7   & abc    & 3      \\
8   & abc    & 2      \\
9   & abc    & 1      \\
10  & abc    & 2      \\
11  & abc    & 2      \\
\bottomrule
\end{tabular}
\quad
\begin{tabular}{clc}
$n$ & Text & $f(n)$ \\
\cmidrule(lr){1-1}
\cmidrule(lr){2-2}
\cmidrule(lr){3-3}
\rowcolor{cyan!20}
\multicolumn{1}{>{\columncolor{cyan!20}[1pt][\tabcolsep]}c}{1}
   & abc    &
\multicolumn{1}{>{\columncolor{cyan!20}[\tabcolsep][1pt]}c}{2}
     \\
2   & abc    & 1      \\ \rowcolor{cyan!20}
7   & abc    & 3      \\
8   & abc    & 2      \\
9   & abc    & 1      \\
10  & abc    & 2      \\
11  & abc    & 2      \\
\bottomrule
\end{tabular}
\end{document}
  • Very interesting that the choice of color specified for \columncolor in the \multicolumn does not matter!! – Peter Grill Jun 25 '14 at 22:16

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.