5

I'd like to highlight a couple of columns in a table I have. I'm using booktabs in order to access better horizontal rules, however this also has the side effect of introducing extra (uncoloured) space around the horizontal rules. Replacing \toprule and \bottomrule eliminates this unwanted/uncoloured space---is there any way I can persuade xcolor (colortbl) to colour this space as well?

MWE

\documentclass{article}
\usepackage{siunitx} % Formatting for units
\usepackage{booktabs} % Better formatting for tables
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % Use colour!


\begin{document}

\renewcommand{\arraystretch}{1.1}
\sisetup{table-format=1.4,table-sign-mantissa}
\setlength{\tabcolsep}{3pt}
\begin{tabular}{@{}lS[table-format=2,table-sign-mantissa]S[table-format=2.4,table-sign-mantissa]S*{2}{>{\columncolor{pink}}S}*{5}{S}S[table-format=1,table-sign-mantissa]@{}}
    \toprule
    $x$ & -2 & -1.9 & -1.8 & -1.7 & -1.6 & -1.5 & -1.4 & -1.3 & -1.2 & -1.1 & -1\\
    $\mathrm{f}(x)$ & -19 & -12.2610 & -6.8957 & -2.6986 & 0.5142 & 2.9063 & 4.6218 & 5.7871 & 6.5117 & 6.8895 & 7\\
    \bottomrule
\end{tabular}

\end{document}
3

As you've noticed, the rules drawn by the macros of the booktabs package don't work very well if entire columns have to be colored.

As an alternative, you could insert (typographic) struts to preserve the good spacing generated by \toprule and \bottomrule while getting the entire cell heights colored in. The directives \Tstrut (short for "top strut") and \Bstrut (short for "bottom strut") may be inserted anywhere in a given row, but I would suggest you insert them at the start of the row; that way, it's not as easy to miss their presence. The height of the "top" strut and the depth of the "bottom" strut were chosen to mimic closely the spacing generated by \toprule and \bottomrule, respectively.

enter image description here

\documentclass{article}
\usepackage[margin=1in,a4paper]{geometry} % set paper size and margins
\usepackage{siunitx}  % Formatting for units
\sisetup{table-format=-1.4,table-sign-mantissa}
\usepackage{booktabs} % Nicely formatted table rules
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % Use colour

\renewcommand{\arraystretch}{1.1}
\setlength{\tabcolsep}{3pt}


% Define "struts", as suggested by Claudio Beccari in TeX and TUG News, Vol. 2, 1993.
\newcommand\Tstrut{\rule{0pt}{3ex}}       % "top" strut
\newcommand\Bstrut{\rule[-1.4ex]{0pt}{0pt}} % "bottom" strut
\newcommand{\TBstrut}{\Tstrut\Bstrut} % top&bottom struts

\begin{document}
\noindent
with \verb+\toprule+ and \verb+\bottomrule+

\noindent
\begin{tabular}{@{} l S[table-format=-2.0] S[table-format=-2.4]
          S *{2}{>{\columncolor{pink}}S} *{5}{S} S[table-format=-1.0] @{}}
    \toprule
    $x$ & -2 & -1.9 & -1.8 & -1.7 & -1.6 & -1.5 & -1.4 & -1.3 & -1.2 & -1.1 & -1\\
    $\mathrm{f}(x)$ & -19 & -12.2610 & -6.8957 & -2.6986 & 0.5142 & 2.9063 & 4.6218 & 5.7871 & 6.5117 & 6.8895 & 7\\
    \bottomrule
\end{tabular}

\bigskip\noindent
with typographic struts and \verb+\hline+ directives:

\noindent
\begin{tabular}{@{} l S[table-format=-2.0] S[table-format=-2.4]
          S *{2}{>{\columncolor{pink}}S} *{5}{S} S[table-format=-1.0] @{}}
    \hline
    \Tstrut$x$ & -2 & -1.9 & -1.8 & -1.7 & -1.6 & -1.5 & -1.4 & -1.3 & -1.2 & -1.1 & -1 \\
    \Bstrut$\mathrm{f}(x)$ & -19 & -12.2610 & -6.8957 & -2.6986 & 0.5142 & 2.9063 & 4.6218 & 5.7871 & 6.5117 & 6.8895 & 7 \\
    \hline
\end{tabular}

\end{document}
7
  • That certainly works, but it loses the effect of \toprule and \bottomrule, which I need in order to remain consistent with other table in the same document. Also, it doesn't seem to 'expand' correctly if the value of the \arraystretch is changed.
    – JustinT
    Jul 6 '15 at 10:57
  • @JustinT - Which "effect" of \toprule and \bottomrule do you have in mind? About the parameter \arraystretch: It should probably be kept constant for the entire document; having different values of \arraystretch for different tabular-like enviroment should generally be avoided.
    – Mico
    Jul 6 '15 at 11:00
  • \hline gives a much thinner line than \toprule and \bottomrule. I'm not intending to use a different \arraystretch in this document, but highlighting parts of a table is something which I might need/want to do in other documents as well!
    – JustinT
    Jul 6 '15 at 11:04
  • @JustinT - To set the width of \hline equal to that of \toprule (and \bottomrule), simply issue the instruction \setlength\arrayrulewidth{0.08em}.
    – Mico
    Jul 6 '15 at 11:15
  • Ok, that works - but it shows another (slight) problem. With the thicker lines, it is apparent (even at 'standard' zoom) that the highlighting overlaps the \toprule by a small amount. Is there anything that will prevent this, or will force the rule to be drawn on top of the highlighting?
    – JustinT
    Jul 6 '15 at 11:37
2

A solution replacing booktabs with boldline (for variable-width horizontal lines) and cellspace (for vertical padding of cells):

\documentclass{article}
\usepackage{geometry}
\usepackage{siunitx} % Formatting for units
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % Use colour!
\usepackage{cellspace, boldline}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{2pt}

\begin{document}

\sisetup{table-format=1.4,table-sign-mantissa}
\setlength{\tabcolsep}{3pt}
\begin{tabular}{@{}ClS[table-format=2,table-sign-mantissa]S[table-format=2.4,table-sign-mantissa]S*{2}{>{\columncolor{pink}}S}*{5}{S}S[table-format=1,table-sign-mantissa]@{}}
    \hlineB{2}
    $x$ & -2 & -1.9 & -1.8 & -1.7 & -1.6 & -1.5 & -1.4 & -1.3 & -1.2 & -1.1 & -1\\
    $\mathrm{f}(x)$ & -19 & -12.2610 & -6.8957 & -2.6986 & 0.5142 & 2.9063 & 4.6218 & 5.7871 & 6.5117 & 6.8895 & 7\\
    \hlineB{2}
\end{tabular}

\end{document} 

enter image description here

4
  • I think you copied a wrong example code. It doesn't use boldline or cellspace.
    – Ignasi
    Jul 6 '15 at 11:36
  • @Ignasi: I guess I messed up with my clipboard manager. Corrected my answer. Thank you for pointing the problem!
    – Bernard
    Jul 6 '15 at 11:40
  • @Bernard That works, but... it seems to introduce extra space between the rows, if the spacing between the data and horizontal rules is the same as produced by booktabs.
    – JustinT
    Jul 6 '15 at 14:01
  • It does. That's the cellspace package that adds some padding at the top and at the bottom of the rows. You may change the values (they're minimal vertical spacing and added if necessary) of \cellspacetoplimit and \cellspacebottomlimit. Generally, LaTeX' vertical spacing is too tight. If you don't want it, remove cellspace loading, and the Cpre-qualifier for the first column, and manually add some spacingabove the rules with something like \\[3pt].
    – Bernard
    Jul 6 '15 at 14:12

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.