[Tex/LaTex] Color table element with siunitx S column

siunitxtables

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}

Best Answer

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