5

Given is a table with factors from x1 to x1000000000 then x0.1 and x0.01 in the fourth column.

The S column of siunitx takes care of the alignment.

Unexpectedly, however, the font color no longer works in the S column. You can see this in the line with a black background, where x1 is not colored white as desired. Of course it's just a small thing, but all previous attempts by me to change that were unsuccessful.

I check this but that does not help. I have @koleygr to thank for a partial solution to my problem.

Why is this and how can it be repaired?

\documentclass[varwidth, margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{colortbl, siunitx, tabularx, booktabs, xcolor}
\begin{document}
\newsavebox\saveboxTimes
\savebox{\saveboxTimes}{$\times\,$}
\begin{tabular}{lcc>{\usebox{\saveboxTimes}}S[table-format={\usebox{\saveboxTimes}11.2},table-align-text-before=false]}
\toprule 
Farbe & 1. Ring & 2. Ring & \multicolumn{1}{c}{{3. Ring}}\tabularnewline
\midrule
\rowcolor{black!60}\color{white}schwarz & \color{white}0 & \color{white}0 & \color{white}1\tabularnewline
\rowcolor{brown!50}braun & 1 & 1 & 10\tabularnewline
\rowcolor{red!50}rot & 2 & 2 & 100\tabularnewline
\rowcolor{orange!50}orange & 3 & 3 & 1000\tabularnewline
\rowcolor{yellow!50}gelb & 4 & 4 & 10000\tabularnewline
\rowcolor{green!30}grün & 5 & 5 & 100000\tabularnewline
\rowcolor{blue!40}blau & 6 & 6 & 1000000\tabularnewline
\rowcolor{violet!30}violett & 7 & 7 & 10000000\tabularnewline
\rowcolor{gray!30}grau & 8 & 8 & 100000000\tabularnewline
\rowcolor{white}weiß & 9 & 9 & 1000000000\tabularnewline
\rowcolor{ColorGold}gold & - & - & 0.1\tabularnewline
\rowcolor{ColorSilber}silber & - & - & 0.01\tabularnewline
\bottomrule
\end{tabular}% 

enter image description here

1
  • Got the tip to put the content of the usebox directly into the code. This leads to error messages. Do not know why.
    – Michael.H
    May 2 at 18:53

3 Answers 3

7
+50

Switching from tabularx to the more adaptable tabularray solved the problem neatly. The transformed code is:

\documentclass[varwidth, margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}

\usepackage{tabularray}
\UseTblrLibrary{siunitx,booktabs}
\usepackage[table]{xcolor}

\begin{document}

    \begin{talltblr}[
        caption = Widerstand Farbcodes
        ]{
            colspec = {lcc Q[si={table-format=13.2, table-align-text-before=false},c]},
            cell{2-Z}{4} = {preto=$\times$\space},
            row{1} = {guard},
            row{2} = {bg=black!60, fg=white},
            row{3} = {brown!50},
            row{4} = {red!50},
            row{5} = {orange!50},
            row{6} = {yellow!50},
            row{7} = {green!30},
            row{8} = {blue!40},
            row{9} = {violet!30},
            row{10} = {gray!30},
            row{11} = {white},
%           row{12} = {ColorGold},
%           row{13} = {ColorSilber},
            hline{1,2,Z} = {1pt}
        }
    Farbe   &   1. Ring &   2. Ring &   3. Ring     \\
    schwarz &   0       &   0       &   1           \\
    braun   &   1       &   1       &   10          \\
    rot     &   2       &   2       &   100         \\
    orange  &   3       &   3       &   1000        \\
    gelb    &   4       &   4       &   10000       \\
    grün    &   5       &   5       &   100000      \\
    blau    &   6       &   6       &   1000000     \\
    violett &   7       &   7       &   10000000    \\
    grau    &   8       &   8       &   100000000   \\
    weiß    &   9       &   9       &   1000000000  \\
    gold    &   -       &   -       &   0.1         \\
    silber  &   -       &   -       &   0.01        \\
\end{talltblr}% 

\end{document}

Note: Unfortunately the colors ColorGold and ColorSilber generated errors (not recognized), so these are commented out in their respective row descriptions.

Resistance values

3
  • 1
    You might want to change the caption to Kennzeichnung von Widerständen =) ... nice solution! May 4 at 0:36
  • How can I get rid of the caption? It should be noted that the headline should neither shown nor that the caption counter should incremented. Found \DefTblrTemplate{caption}{default}{} here link The caption disappears, but the table caption counter is incremeted. The next caption number is one more than it should be. I need a global solution, because I set my captions the old fashion way with the caption command in the rest of the document.
    – Michael.H
    May 27 at 8:55
  • To remove the caption from the table, you can just comment out the caption entry inside the square brackets. Adding label=none in that section will prevent the numbering of the table and the increase of the table counter (according to page 39 of the manual of tabularray)
    – alchemist
    May 28 at 7:17
6

First of all, I think a problem of your code is that you place the $\times$ inside a \savebox which essentially prevents the contents from being affected by \color settings outside of this box. I would therefore suggest that you don't use such a box in this case.

I don't fully understand why the coloring becomes broken. Somehow, the siunitx package seems not to be able to process a \color command if this is not the only thing which is placed before the number in the cell that should be formatted. If you place something like \color{white}\times in front of a number in an S type cell, only the stuff before the number will be affected by the \color setting, but not the formatted number.

Compare the following MWE:

\documentclass[margin=10pt]{standalone}
\usepackage{xcolor, siunitx}
\begin{document}
\begin{tabular}{
    S[table-format={{\times}2.2}]
}
\color{red} 99.99 \\
\color{red}\times 99.99 \\
\end{tabular}% 
\end{document}

enter image description here

However, it is possible to use the option color to set the color for the formatted number. You only need to set the correct value in one of the cells before the S type cell. For example like this:

\documentclass[margin=10pt]{standalone}
\usepackage{colortbl, siunitx, booktabs, xcolor}

\def\Scolcolor{black}

\begin{document}
\begin{tabular}{
    l c c 
    >{\color{\Scolcolor}\times\,}S[
        table-format={{\times\,}11.2}, 
        table-align-text-before=false, 
        color=\Scolcolor
    ]
}
\toprule 
Farbe & 1. Ring & 2. Ring & \multicolumn{1}{c}{{3. Ring}}\tabularnewline
\midrule
\rowcolor{black!60}\gdef\Scolcolor{white}%
    \color{white}schwarz & \color{white}0 & \color{white}0 & 1\tabularnewline
\rowcolor{brown!50}\gdef\Scolcolor{black}%
    braun  & 1 & 1 & 10\tabularnewline
\rowcolor{red!50}rot & 2 & 2 & 100\tabularnewline
\rowcolor{orange!50}orange & 3 & 3 & 1000\tabularnewline
\rowcolor{yellow!50}gelb & 4 & 4 & 10000\tabularnewline
\rowcolor{green!30}grün & 5 & 5 & 100000\tabularnewline
\rowcolor{blue!40}blau & 6 & 6 & 1000000\tabularnewline
\rowcolor{violet!30}violett & 7 & 7 & 10000000\tabularnewline
\rowcolor{gray!30}grau & 8 & 8 & 100000000\tabularnewline
\rowcolor{white}weiß & 9 & 9 & 1000000000\tabularnewline
\rowcolor{yellow!10}gold & - & - & 0.1\tabularnewline
\rowcolor{gray!10}silber & - & - & 0.01\tabularnewline
\bottomrule
\end{tabular}% 
\end{document}

enter image description here


If you are not bound to use a regular tabular, you could also try the nicematrix package (but, here as well, you need to un-box the \times in order to make things work):

\documentclass[margin=10pt]{standalone}
\usepackage{nicematrix, booktabs, siunitx}

\begin{document}
\NewDocumentCommand{\timesprefix}{}{%
    \ensuremath\times\,%
}
\begin{NiceTabular}{
    l c c 
    >{\timesprefix}S[table-format={{\timesprefix}11.2}, table-align-text-before=false]
}
\CodeBefore
    \rowcolor{black!60}{2}
    \rowcolor{brown!50}{3}
    \rowcolor{red!50}{4}
    \rowcolor{orange!50}{5}
    \rowcolor{yellow!50}{6}
    \rowcolor{green!30}{7}
    \rowcolor{blue!40}{8}
    \rowcolor{violet!30}{9}
    \rowcolor{gray!30}{10}
    \rowcolor{white}{11}
    \rowcolor{yellow!10}{12}
    \rowcolor{gray!10}{13}
\Body
\toprule 
Farbe & 1. Ring & 2. Ring & \multicolumn{1}{c}{{3. Ring}}\tabularnewline
\midrule
\RowStyle[color=white]{}
schwarz & 0 & 0 & 1 \\
braun & 1 & 1 & 10 \\
rot & 2 & 2 & 100 \\
orange & 3 & 3 & 1000 \\
gelb & 4 & 4 & 10000 \\
grün & 5 & 5 & 100000 \\
blau & 6 & 6 & 1000000 \\
violett & 7 & 7 & 10000000 \\
grau & 8 & 8 & 100000000 \\
weiß & 9 & 9 & 1000000000 \\
gold & - & - & 0.1 \\
silber & - & - & 0.01 \\
\bottomrule
\end{NiceTabular}% 
\end{document}

enter image description here

Some other packages that provide macros to set the text color for the whole row could also be of use.

1
  • nice answer, sorry I did not read it before posting mine... about "a \savebox which essentially prevents the contents from being affected by \color " I think you can remove the "essentially" because \savebox hard-codes a reset to "current color" i.e. color at time of definition at very start of the box. At least this is my understanding of the \color@setgroup->\begingroup \set@color and \set@color->\special {color push \current@color }\aftergroup \reset@color. (which is all done at time of definition so the TeX box contains the color push/pop specials with a set color)
    – user691586
    May 8 at 10:45
4

Use a \multicolumn to redefine the column type for that cell.

\documentclass[varwidth, margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{colortbl, siunitx, tabularx, booktabs, xcolor}
\begin{document}
\newsavebox\saveboxTimes
\savebox{\saveboxTimes}{$\times\,$}
\begin{tabular}{lcc>{\usebox{\saveboxTimes}}S[table-format={\usebox{\saveboxTimes}11.2},table-align-text-before=false]}
\toprule 
Farbe & 1. Ring & 2. Ring & \multicolumn{1}{c}{{3. Ring}}\tabularnewline
\midrule
\rowcolor{black!60}\color{white}schwarz & \color{white}0 & \color{white}0 &
% HERE ARE THE CHANGES
\multicolumn{1}{S[table-format={{$\times\,$}11.2},table-align-text-before=false,color=white]}{\textcolor{white}{$\times\,$}1}%
% END OF CHANGES
\tabularnewline
\rowcolor{brown!50}braun & 1 & 1 & 10\tabularnewline
\rowcolor{red!50}rot & 2 & 2 & 100\tabularnewline
\rowcolor{orange!50}orange & 3 & 3 & 1000\tabularnewline
\rowcolor{yellow!50}gelb & 4 & 4 & 10000\tabularnewline
\rowcolor{green!30}grün & 5 & 5 & 100000\tabularnewline
\rowcolor{blue!40}blau & 6 & 6 & 1000000\tabularnewline
\rowcolor{violet!30}violett & 7 & 7 & 10000000\tabularnewline
\rowcolor{gray!30}grau & 8 & 8 & 100000000\tabularnewline
\rowcolor{white}weiß & 9 & 9 & 1000000000\tabularnewline
%\rowcolor{ColorGold}gold & - & - & 0.1\tabularnewline
%\rowcolor{ColorSilber}silber & - & - & 0.01\tabularnewline
\bottomrule
\end{tabular}% 
\end{document}

I commented out the last two rows as this preamble does not define the ColorGold and ColorSilber in my locale.

colored table with S column and special cell

2
  • Reading it only now It may be that this is quite related to answer by @JasperHabicht
    – user691586
    May 8 at 7:37
  • This is also a nice approach. The approach is indeed similar, as you also colorize the \times and the numbers separately (which is, I guess, the only way to go if one wants to stick to a regular tabular). May 8 at 7:50

You must log in to answer this question.

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