10

I have a table with numbers that I have aligned using the S column type provided by the siunitx package. I need to make one of the table elements red, but when I do that, the number that is red is no longer aligned.

What is the right way to do this? MWE below.

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

\begin{document}
\begin{table} \centering
    \caption{Table with \texttt{c} column type.}
    \begin{tabular}{cc}
        \hline
        Symbol & \multicolumn{1}{c}{Value} \\
        \hline
        $\pi$ & 3.141592654 \\
        $2\pi$ & 6.283185307 \\
        $4\pi$ & 12.56637061 \\
        $8\pi$ & {\color{red}25.13274123} \\
        $e$   & 2.71828182845904524 \\
        $\sqrt{2}$ & 1.41421356237309505 \\
        \hline
    \end{tabular}
\end{table}

\begin{table} \centering
    \caption{Table with \texttt{S} column type.}
    \begin{tabular}{cS}
        \hline
        Symbol & \multicolumn{1}{c}{Value} \\
        \hline
        $\pi$ & 3.141592654 \\
        $2\pi$ & 6.283185307 \\
        $4\pi$ & 12.56637061 \\
        $8\pi$ & {\color{red}25.13274123} \\
        $e$   & 2.71828182845904524 \\
        $\sqrt{2}$ & 1.41421356237309505 \\
        \hline
    \end{tabular}
\end{table}
\end{document}

1 Answer 1

8

Simply remove the braces around the \color{red}25.13274123. They are not necessary, and content wrapped in braces are not considered numbers I think, hence not aligned as such.

Also, the \multicolumn is not necessary, it is enough to wrap Value in braces. The reason, as I understand it, is that the e in Value could be considered a part of an exponential number, e.g. 3e8, and siunitx will then try to align it. To protect it from alignment, wrap it in braces. This is mentioned in the manual, section 4.6 Tabular material (the last sentence starting on page 13).

\documentclass{article}
\usepackage{siunitx}
\usepackage{xcolor}
\begin{document}
\begin{table} \centering
    \caption{Table with \texttt{S} column type.}
    \begin{tabular}{cS}
        \hline
        Symbol & {Value} \\
        \hline
        $\pi$ & 3.141592654 \\
        $2\pi$ & 6.283185307 \\
        $4\pi$ & 12.56637061 \\
        $8\pi$ & \color{red}25.13274123 \\
        $e$   & 2.71828182845904524 \\
        $\sqrt{2}$ & 1.41421356237309505 \\
        \hline
    \end{tabular}
\end{table}
\end{document}

enter image description here

6
  • I think siunitx, by default, will center-set items that are surrounded curly braces, regardless of the contents of the group.
    – Mico
    Commented Aug 14, 2012 at 17:34
  • Well that was so easy, I'm almost embarrassed to have asked the question. Thanks!
    – jlconlin
    Commented Aug 14, 2012 at 17:35
  • Here is an example of a siunitx S column where this coloring not works: link, see the first answer, the wrong coloring is an open issue.
    – Michael.H
    Commented Apr 30, 2023 at 18:33
  • @Michael.H Yes it does work, but not together with the >{\usebox{\saveboxTimes}} in the column specification. I don't know how to fix that though. Commented May 2, 2023 at 17:38
  • @Torbjørn T. I tried to input the content of the usebox directly in the code. But that leads to error messages. So I do not know what you mean with ´Yes it does work, but not together with the usebox´.
    – Michael.H
    Commented May 2, 2023 at 18:48

You must log in to answer this question.

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