3

I can set colors for individual cells using cellcolor in the colortbl package, and set the overhang for a whole row, but using the 'obvious' solution to set the overhang for a single cell doesn't seem to work. Is this possible? That is, I want the 1pt overhang seen in the third row to also be in the yellow cell in the fifth row too (and potentially other cells in that row to be coloured without the overhang).

failed single cell overhang

\documentclass[a5paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{colortbl}

\begin{document}

\begin{tabular}{clc}
$n$ & Text & $f(n)$ \\
\rowcolor{yellow}
1   & abc    & 2      \\
2   & abc    & 1      \\ \rowcolor{yellow}[1pt]
7   & abc    & 3      \\
8   & abc    & 2      \\
9   & abc    &\cellcolor{yellow}[1pt] 1      \\
10  & abc    & 2      \\
11  & abc    & 2      \\
\end{tabular}

\end{document}

1 Answer 1

2

Why cellcolor doesn't works as ones expected, I do not know (seems to be a bug). But your problem can be solved on the following way:

\documentclass[a5paper]{article}

\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{array}
\usepackage{colortbl}

    \begin{document}
\begin{tabular}{clc}
$n$ & Text & $f(n)$ \\
\rowcolor{yellow}
1   & abc    & 2      \\
2   & abc    & 1      \\ 
\rowcolor{yellow}[1pt]
7   & abc    & 3      \\
8   & abc    & 2      \\
9   & abc    &\multicolumn{1}{>{\columncolor{red}[1pt]}c}{1}    \\
10  & abc    & 2      \\
11  & abc    & 2      \\
\end{tabular}
    \end{document}

which gives:

enter image description here

For table writing is probably more convenient to define newcommand:

\newcommand\redcell[1]{\multicolumn{1}{>{\columncolor{red}[1pt]}c}{#1}}

and than in table use \redcell{1} instead \multicolumn{1}{>{\columncolor{red}[1pt]}c}{1}. Of course, you can change the color as well command name.

1
  • Thanks!I I was hoping for a solution that didn't require the cell contents to be given as an argument to a command (I'm writing the table in programmatically with knitr/Hmisc) but it looks like this might not be possible
    – aPaulT
    Sep 17, 2015 at 7:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .