2

I am using siunitx to align my columns by the decimal point. I want to highlight one of the columns in my table (edited: make a column have a coloured background). I was wondering if siunitx has a function for this. So far, I have only managed to change the font colour like this:

\documentclass[]{article}

\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{color}

\begin{document}

\begin{tabular}{S[table-format=3.2]S[table-
format=3.2]S[table-format=3.2,color=red]}
    \toprule
    1 &       2 &       3  \\
    1.0 &     2.0 &     3.0 \\
    \bottomrule
\end{tabular}
\end{document}

Which produces this result:

Table with red text.

It would be really great if someone knows of a soft green colour that will also print ok in grayscale.

2

your solution doesn't work. simple solution, which will also colored fonts in column header, is:

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\begin{document}

\begin{tabular}{S[table-format=3.2]
                S[table-format=3.2]
  >{\color{red}}S[table-format=3.2]}% correct way to prescribe font color
    \toprule
    1 &       2 &       3  \\
    1.0 &     2.0 &     3.0 \\
    \bottomrule
\end{tabular}
\end{document}

enter image description here

more advance solution, where you can select, which cells will have content in red:

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{xcolor}
\usepackage{etoolbox}           % <--
\newrobustcmd{\R}{\color{red}}  % <--

\begin{document}
\begin{tabular}{S[table-format=3.2]
                S[table-format=3.2]
                S[detect-weight,% <--
                  table-format=3.2]}
    \toprule
    1   &   2   &     3  \\
    1.0 &   2.0 & \R  3.0 \\
    \bottomrule
\end{tabular}
\end{document}

enter image description here

edit: from your comment follows, that you actually like to have colored column background in some "soft green" color. for colors please look in documentation for xcolor package, which detailed describe predefined colors by name or how to define own. shortly, color you should select yourself, the best is use simple gray as i use in mwe below.

note: using colored column doesn't doing well with rules frombooktabs!

enter image description here

to have column color till to rule, you have two possibilities: (i) change rule to \hline, or (ii) redefine booktabs rules that you remove added vertical space around it:

\setlenght\aboverulesep{0pt}
\setlength\belowrulesep{0pt}

(not considered in mwe below)

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage[table]{xcolor}% <--- changed

\begin{document}

\begin{tabular}{S[table-format=3.2]
                S[table-format=3.2]
>{\columncolor{gray!20}}S[table-format=3.2]}
    \toprule
    1 &       2 &       3  \\
    1.0 &     2.0 &     3.0 \\
    \bottomrule
\end{tabular}
\end{document}
| improve this answer | |
  • Thanks for the correction. What I want to do though is shade the background of the whole column... I think I was unclear. – nicolejane33 Feb 22 '18 at 15:20
  • @NicoleAndrew, than please edit you question and correct your information, what is the problem :-) – Zarko Feb 22 '18 at 15:24

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.