2

I want to highlight one row and one column in a table. The column is no problem. However, the colour panels of the outermost left and right cell in the row overhang the table's borders if I use @{} in the tabulardefinition.

\documentclass{scrartcl}
\usepackage{colortbl,booktabs}
\begin{document}
\begin{table}[h]
  \setlength{\aboverulesep}{0pt} % align \bottomrule nicely to coloured cell
  \setlength{\belowrulesep}{0pt} % align \toprule nicely to coloured cell
  \setlength{\extrarowheight}{.75ex} % make up for lost ruleseps

  \begin{tabular}{@{}c>{\columncolor[gray]{.9}}cc@{}} \toprule
                        a & b & c \\
                        d & e & f \\
    \rowcolor[gray]{.9} g & h & i \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

The problem is obvious:

rowcolor overhangs

Using \rowcolor[gray]{.9}[0pt][0pt] to reduce overhang is not an option as the row would then not be fully highlighted anymore.

What I tried so far

  1. Redefining the highlighted row like the following prints [0pt] into the cell and doesn't work, although it was suggested here.

    \rowcolor[gray]{.9}\cellcolor{white}\cellcolor[gray]{.9}[0pt] g & h & i \\
    
  2. Redefining the highlighted row like this also doesn't work:

    \rowcolor[gray]{.9}[0pt][0pt] g & \cellcolor[gray]{.9} h & i \\
    

    The middle cell stays highlighted like there was only \rowcolor[gray]{.9}[0pt][0pt] defined…

Is there any other possibilty to trim the colour panels to align with \bottomrule to the left and to the right?

2 Answers 2

1

One way is to fix column widths. I am not sure if this is a very elegant way.

\documentclass{scrartcl}
\usepackage{colortbl,booktabs}
\begin{document}

\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}

\begin{table}[h]
\setlength\tabcolsep{0pt}
  \begin{tabular}{C{1em}>{\columncolor[gray]{.9}} C{1em} C{1em}} \toprule
                        a  & b & c \\
                        d & e & f \\
    \rowcolor[gray]{.9} g & h & i \\
    \bottomrule
  \end{tabular}
\end{table}
\end{document}
\end{document}
2
  • However, setting \tabcolsep{0pt} brings the cells really close together. So I'd need to set them all (not only the two affected cells) to fixed width, don't I? Actually not the most elegant solution, true, but I have the C/L/R columntype in my document anyways since they're sometimes of use…
    – Jakob
    Feb 16, 2016 at 21:25
  • @Jakob Yeah the cells get close but if you increase the column width such that the cell width is greater than the cell content then your column seem separated.
    – WYSIWYG
    Feb 17, 2016 at 4:07
1
\documentclass{scrartcl}

\usepackage{booktabs}
\usepackage[table]{xcolor}

\colorlet{tablerowcolor}{yellow}%\colorlet{tablerowcolor}{gray!10}

\newcommand*{\ct}[1]{\multicolumn{1}{>{\columncolor{tablerowcolor}}c}{#1}}
\newcommand*{\fct}[1]{\multicolumn{1}{>{\columncolor{tablerowcolor}\hspace*{-\tabcolsep}}c}{#1}}
\newcommand*{\lct}[1]{\multicolumn{1}{>{\columncolor{tablerowcolor}}c<{\hspace*{-\tabcolsep}}}{#1}}
\newcommand*{\cfct}[1]{\multicolumn{1}{>{\columncolor{tablerowcolor}[\dimexpr\tabcolsep-\cmidrulekern\relax][\tabcolsep]\hspace*{-\tabcolsep}}c}{#1}}
\newcommand*{\clct}[1]{\multicolumn{1}{>{\columncolor{tablerowcolor}[\tabcolsep][\dimexpr\tabcolsep-\cmidrulekern\relax]}c<{\hspace*{-\tabcolsep}}}{#1}}

\begin{document}

\begin{table}[h]
    \setlength{\aboverulesep}{0pt} % align \bottomrule nicely to coloured cell
    \setlength{\belowrulesep}{0pt} % align \toprule nicely to coloured cell
    \setlength{\extrarowheight}{.75ex} % make up for lost ruleseps

    \begin{tabular}{ @{}r@{} }
        \\
        reference \\
        wrong alignment if overhang \\
        fitted to \texttt{\textbackslash midrule} \\
        fitted to \texttt{\textbackslash cmidrule(lr)} \\
    \end{tabular}
    %
    \begin{tabular}{ @{}*{3}{c}@{} }
        \toprule
        test    & test & test    \\
        \midrule
              a & b    &       c \\
        \ct   a & b    & \ct   c \\
        \midrule
        \fct  a & b    & \lct  c \\
        \cmidrule(lr){1-3}
        \cfct a & b    & \clct c \\
        \bottomrule
    \end{tabular}
\end{table}

\end{document}

Output

1
  • I get an "undefined sequence" error with \fct in a tabularx. Is this supposed to work only in tabular? Also can I put the \fct command inside the @{} in some way? to avoid repeating on each row... ? Jul 15, 2020 at 9:57

You must log in to answer this question.

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