4

table color

\documentclass[]{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{multicol,booktabs,tabularx}

% Table settings
\renewcommand{\aboverulesep}{1pt}
\renewcommand{\belowrulesep}{1pt}

\begin{document}
    \begin{tabularx}{\textwidth}{@{}X l@{}}
        This is Header 1 & This is Header 2 \\
        \toprule
        \rowcolor{Apricot}
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

I have the above table MWE using tabularx, and I am trying to colour the row from the table. However, due to the use of my \aboverulesep and \belowrulesep, I now have gaps above and below my table row that is not coloured.

Also, because of the use of my @{} on either side of the table to 'remove' the excess padding on the sides of the table, the row colour is being applied in those padding as well.

Qn 1: How can I colour the aforementioned gaps (arising due to the \aboverulesep) in, while maintaining the rule separation?

Qn 2: How to remove the colour from the two sides of the table?


EDIT

This is in response to Zarko's answer. I want to maintain the use of @{} on either side to remove the padding. If I were to adapt this into your answer, like so:

% @Zarko's answer
\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, cellspace, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\begin{document}
    \begin{tabularx}{\textwidth}{@{}SX Sl@{}}% <-- S is append for activate additional vertical space 
        This is Header 1 & This is Header 2 \\
        \toprule
        \rowcolor{Apricot}
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

I obtain this: enter image description here

and so my second question (Qn2) is still unresolved.. For Qn 1, I prefer to use the solution by @Skillmon with \renewcommand{\arraystretch}{1.15} as I need not add a new package.

  • 1
    You might set the \aboverulesep and \belowrulesep to 0pt and use \setstretch from the setspace-package. Then the entire line is coloured. (approx. \setstretch{1.15} seems to fit your ruleseps) – Skillmon Apr 20 '17 at 12:01
  • @Skillmon Yup that works nicely for the first question. Thank you. – ksgj1 Apr 20 '17 at 12:13
  • Just make sure, you use the \setstretch inside of a group and the group is it's own paragraph, so that \setstretch doesn't affect other text. – Skillmon Apr 20 '17 at 12:16
  • Just realized, you might use \renewcommand{\arraystretch}{1.15} instead of \setstretch. It results in the same, but is not dependand on setspace. – Skillmon Apr 20 '17 at 13:34
  • @Skillmon Indeed so! In this case, it's alright that I leave this in my preamble? i.e. it only affects my tables, not my main text body? – ksgj1 Apr 20 '17 at 14:18
3

One of possibilities is to use cellspace package:

enter image description here

First the vertical space around booktabs rules i reduced to zero pt, than is increased by macro \cellspacetoplimit and \cellspacebottomlimit to desired vertical (colored) gap:

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, cellspace, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}
\setlength\cellspacetoplimit{5pt}
\setlength\cellspacebottomlimit{5pt}

\begin{document}
   \begin{tabularx}{\textwidth}{SX Sl}% <-- S is append for activate additional vertical space 
        This is Header 1 & This is Header 2 \\
        \toprule
    \rowcolor{Apricot}
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

Addendum: works around to second part of question. It need to introduce fake column (or to have zero distance between columns or white space with width of two \tabcolsep):

enter image description here

This time without additional package but with tricks how to use \rowcolor (for details see documentation for package colortbl):

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}

\begin{document}
\setlength\tabcolsep{0pt}
\renewcommand\arraystretch{1.2}
   \begin{tabularx}{\textwidth}{ X c<{\hspace{12pt}} l }
        This is Header 1 && This is Header 2 \\
        \toprule
    \rowcolor{Apricot}%[0pt]
        This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is && This is Text 2 \\\midrule
        This is Text 1 && This is Text 2 \\\midrule
        This is Text 1 && This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}
  • I'm sorry, I want to maintain the use of @{}, that is, I don't want the padding on either side of the table. Notice that in your example now, there is spurious spacing beside all the This... and ...2's – ksgj1 Apr 20 '17 at 13:16
  • @ksgj1, you can return @{}, but it cause ... you will see what. If you like to have such table, than is ok. but people in the most cases don't want this, they say, discrapancy :) – Zarko Apr 20 '17 at 14:52
  • Hi Zarko, I shown my output above. I would like to have such a table, indeed. But I hope you understand that was part of my second question, so I would still hope to get an answer for that. That is, use the @{} and not have the row colour extend out of the table.. – ksgj1 Apr 20 '17 at 15:02
  • @ksgj1, see addendum to my answer :) – Zarko Apr 20 '17 at 15:30
  • 1
    regarding[0pt], it was left from experiments to use @{} on the beginning and end of table columns definition. since I on the end set tabcolsep to zero, it becomes surplus (i correct answer). for fake column, as I said in answer, if you not need the \tabcolsep space between columns, this columns is also surplus. I strongly encourage you to wrote own answer! Learning-by-doing is the mots effective way to learn! I successful test this almost each day :) – Zarko Apr 20 '17 at 16:35
2

Solution by OP

This is an alternative solution to @Zarko's solution with regards to my second question, on how to remove the spurious colours on either side of the table when using \rowcolor{}, while still keeping the use of @{}, in a tabularxenvironment. This has the additional benefit (compared to Zarko's answer) of not requiring to add a separate column in the middle and having to add additional alignment points throughout all the rows.

In the answer below, I have also incorporated @Skillmon's suggestion of using \renewcommand\arraystretch{1.2} with regards to my first question.

Here's the code:

\documentclass{article}
\usepackage[table,dvipsnames]{xcolor}
\usepackage{booktabs, tabularx}

% Table settings
\renewcommand{\aboverulesep}{0pt}
\renewcommand{\belowrulesep}{0pt}

\begin{document}
    \renewcommand\arraystretch{1.15}
    \begin{tabularx}{\textwidth}{@{}>{\columncolor{white}[0pt][\tabcolsep]}X >{\columncolor{white}[\tabcolsep][0pt]}l @{}}
        This is Header 1 & This is Header 2 \\
        \toprule
        \rowcolor{Apricot}
        This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1 This is Text 1  & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\\midrule
        This is Text 1 & This is Text 2 \\
        \bottomrule
    \end{tabularx}
\end{document}

table



Explanation of code

The main approach was to exploit the use of overhangs by the \columncolor command from the colortbl package, also called by the xcolor package with the table optional argument.

Note that the \columncolor command takes in optional arguments as such:

\columncolor{colour} [left overhang][right overhang]

So I just set the left overhang of the leftmost column to be 0pt, thereby getting rid of the spurious space on the left throughout the column, and right overhang to be \tabcolsep which is basically a distance separating the columns. I did the same for the right overhang of the rightmost column, set it to be 0pt, while setting its left overhang to \tabcolsep. This essentially fixes the column widths, as I want.

Obviously the colour of both columns in \columncolor have to be set to white, and one final thing to note is that when \rowcolor is called later in the table, it will override this white colour of the \columncolor, thereby still giving us the benefit of having the row colour set correctly.

Read the colortbl package manual here.

  • 1
    excellent answer! good trick with columncolor! – Zarko Apr 20 '17 at 18:56

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.