0

I am trying to implement a kind-of-conditional coloring for a table based on this answer (the preferable ones above in this thread don't work out as my number format is german so I have commas as decimal separators); however, there is another (presumably small) issue with this suggestion: My numbers which I like to have colored range from 0.01 to approx. 1. However, if I replace the

\ifdim#1pt<5pt\cellcolor{green}

with smaller numbers (e.g. 0.1pt), there is an error stating "Illegal parameter number in definition of zz".

Any ideas on how I can solve this?

Thanks a lot!

Edit: MWE resulting in 55 errors, the only difference from the original code I got from the answer is that I changed 1pt to .1pt:

\documentclass{article}

\usepackage{colortbl,dcolumn}

\def\zz#1{% \ifdim#0.1pt<5pt\cellcolor{green}\else \ifdim#1pt<50pt\cellcolor{yellow}\else \cellcolor{red}\fi\fi #1}

\begin{document}

\begin{tabular}{*3{D,,{2.2}}c} \zz {0,2} &\zz {3,04} &\zz {5,44} \ \zz {1,01}&\zz {77,5} &\zz {77,94} \ \zz {3,42}&\zz {4,04} &\zz {51,04} \end{tabular}

\end{document}

Removing the # before the 0.1pt resolves the error, however then it just doesn't work as intended.

Sempft
  • 21
  • 2
    there is no error from \ifdim0.001pt<5pt yes \else no\fi If you want help with an error please supply a small complete test file that makes the error then people can debug it for you – David Carlisle Feb 18 '21 at 10:03
  • 1
    the error message you quote is unrelated to code shown and means you have \newcommand\zz[1]{...#2} with a definition with1 declared argument where you try to use a second argument. – David Carlisle Feb 18 '21 at 10:05
  • thanks for your fast replies! I added an MWE showing the error(s). – Sempft Feb 18 '21 at 10:15
  • \ifdim#1pt is not # followed by 1pt but it means #1 (the first parameter) followed by pt, so \ifdim#0.1pt is wrong. However, I'm not sure what your are trying to do. – campa Feb 18 '21 at 10:22
  • Can you please state what comparisons you're trying to do? Not in TeX language, but in plain words. – egreg Feb 18 '21 at 10:24
  • Your input to \zz is a pair of numbers. What do they both represent? – Steven B. Segletes Feb 18 '21 at 10:25
  • @StevenB.Segletes forget brexit: that is a single number with a decimal comma – David Carlisle Feb 18 '21 at 10:25
  • ohhhh thanks @campa I interpreted the #1pt<... as a range in between which the color is set as desired. Now if i knew how to set this as "answered" i'd be even happier. – Sempft Feb 18 '21 at 10:26
  • @egreg I was trying to compare the number in the field to (several) intervals where each interval leads to a different coloring of the field. – Sempft Feb 18 '21 at 10:28
  • @Sempft That's clear, but what comparisons are you doing? – egreg Feb 18 '21 at 10:28
  • @DavidCarlisle Thanks for the clarification for us rebels across the pond. – Steven B. Segletes Feb 18 '21 at 10:29
  • @egreg I was trying to compare the number in the table to several intervals to determine the color of the field. I misunderstood the #1pt<5pt as the interval and did not understand that #1 is the argument which is passed to the comparison. Now I got it, the problem is solved and I do not find the "solved"-button :( – Sempft Feb 18 '21 at 10:31

2 Answers2

1

I misunderstood the term #1pt<5pt as the interval, while #1 corresponds to the number which is passed to the comparison. Therefore

\def\zz#1{%
\ifdim#1pt<0.1pt\cellcolor{green}\else
\ifdim#1pt<0.5pt\cellcolor{yellow}\else
\cellcolor{red}\fi\fi
#1}

works as intended. Thanks for your comments.

Sempft
  • 21
0

The following adds two new column types called \sempftcolored and \SempftColored. With those you can set an alignment and colour your cells according to some colour ranges you can either specify with \sempftsetcolors or in the first and second argument of \SempftColored.

It uses a loop to find the correct cell colour. The list of colour specifications being used in the first argument of \sempftsetcolors or \SempftColored should have a comma separated list, each containing of two groups, the first being a relation (e.g., {<0.1} for smaller than 0.1) and the second the colour specification which should be applied. The second argument of \sempftsetcolors and \SempftColored is the colour which is applied if none of the relations matched.

For the relations you can use =, >=, <=, != (and theoretically a few more which you will not need). The first matched relation is used (so order in \sempftsetcolors matters).

The argument of \sempftcolored and the last argument of \SempftColored are the cell alignments which should be applied.

\sempftcellcolor (the macro used to evaluate the relations and sets the cellcolor) works with both , (a comma) and . (a period) as the decimal separator of the argument (the cell content in a \sempftcolored column), whereas the numbers used in the colour lists must use a period.

\documentclass{article}

\usepackage[table]{xcolor} % just for more named colours \usepackage{dcolumn,collcell,array}

%\usepackage{xparse} % not needed with newer LaTeX versions

\ExplSyntaxOn \seq_new:N \l_sempft_color_ranges_seq \tl_new:N \l_sempft_color_else_tl \NewDocumentCommand \sempftsetcolors { m m } { \seq_set_from_clist:Nn \l_sempft_color_ranges_seq {#1} \tl_set:Nn \l_sempft_color_else_tl {#2} } \NewExpandableDocumentCommand \sempftcellcolor { m } { \sempft_color_loop:e { \sempft_ensure_decimal_period:n {#1} } #1 } \cs_new:Npn \sempft_color_loop:n #1 { \seq_map_tokens:Nn \l_sempft_color_ranges_seq { __sempft_color_loop:nn {#1} } \use:n { \cellcolor { \l_sempft_color_else_tl } } } \cs_generate_variant:Nn \sempft_color_loop:n { e } \cs_new:Npn __sempft_color_loop:nn #1#2 { __sempft_color_loop:nnn {#1} #2 } \cs_new:Npn __sempft_color_loop:nnn #1#2#3 { \fp_compare:nT { #1 #2 } { \cellcolor {#3} \seq_map_break:n { \use_none:nn } } } % auxiliary function to parse floats correctly regardless of the decimal % separator being a period or comma \cs_new:Npn \sempft_ensure_decimal_period:n #1 { __sempft_ensure_decimal_period_auxi:w \q_nil #1 \q_mark , \q_stop } \cs_new:Npn __sempft_ensure_decimal_period_auxi:w #1 , { __sempft_ensure_decimal_period_auxii:w #1 . } \cs_new:Npn __sempft_ensure_decimal_period_auxii:w #1 \q_mark #2 \q_stop { \exp_not:o { \use_none:n #1 } } \ExplSyntaxOff

\sempftsetcolors{{<0.1}{green}, {<0.5}{yellow}, {<1}{orange}}{red}

\newcolumntype\sempftcolored[1] {>{\collectcell\sempftcellcolor}#1<{\endcollectcell}} \newcolumntype\SempftColored[3] {>{\sempftsetcolors{#1}{#2}\collectcell\sempftcellcolor}#3<{\endcollectcell}}

\begin{document}

\begin{tabular} { \sempftcolored{D,,{1.2}} \SempftColored{{<10}{green},{<20}{orange}}{red}{D,,{2.1}} } 0,44 & 5,2 \ 0,94 & 15,3 \ 0,04 & 20,4 \ 1,45 & 30,5 \ \end{tabular}

\end{document}

enter image description here

Skillmon
  • 60,462