3

I am stuck on a "Dimension too large" problem which is occurring here in this code:

\documentclass{article} 
\usepackage{colortbl,dcolumn}
\def\a#1{%
\ifdim#1pt>2000pt\cellcolor{green}\else
\ifdim#1pt<1000pt\cellcolor{red}\else
\cellcolor{yellow}\fi\fi
#1}
\def\b#1{%
\ifdim#1pt>1000000pt\cellcolor{green}\else
\ifdim#1pt<500pt\cellcolor{red}\else
\cellcolor{yellow}\fi\fi
#1}
\begin{document}
\begin{tabular}{|c|c|}
\a{500} & \b{500}  \\
\a{1000} & \b{100}  \\
\a{300} & \b{1500}   \\
\a{150} & \b{600}  \\
\end{tabular}
\end{document}

Actually I want to color each cell in each column according to its value and when I put the range upper than some threshold it gives me this error. I found that tex dimension is limited to some extent,but Is there any alternative way to do the same functionality or solving this problem ?

  • 1
    TeX dimensions are restricted to a maximum value of 16383pt, which means about 5.72 meter... I doubt, that you really need a dimension of 1000000pt, which is about 35km ;-) –  Aug 11 '16 at 09:36
  • If only integers are concerned, you can use counters or \ifnum, which allows 2^31 - 1 as maximum values, see my answer here: http://tex.stackexchange.com/questions/270535/what-is-the-maximum-integer-that-can-be-saved-in-a-latex-counter/270536#270536 –  Aug 11 '16 at 09:43
  • Actually I don't need that large dimension , but I thought i can use this trick to color my cells based on the values of each column dynamically. So do you know any alternative solution for it? – user2373198 Aug 11 '16 at 09:46
  • What is your desired output? – Alenanno Aug 11 '16 at 09:52
  • hey thanks , i fixed the problem with your help. I wrote this code \def\c#1{% \ifnum0#1>5000000 \cellcolor{green}\else \ifnum0#1<500\cellcolor{red}\else \cellcolor{yellow}\fi\fi #1} and its works – user2373198 Aug 11 '16 at 09:56
  • 1
    If you redefine \a you break latex's accent handling – David Carlisle Aug 11 '16 at 10:05

1 Answers1

2

You can't use dimensions larger than 16384pt.

Here's a solution based on the floating point module of expl3:

\documentclass{article} 
\usepackage{colortbl,xparse}

\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\ca}{m}
 {
  \manual_cell_colors:nnnnnn { #1 } { 1000 } { 2000 } { red } { yellow } { green }
 }
\DeclareExpandableDocumentCommand{\cb}{m}
 {
  \manual_cell_colors:nnnnnn { #1 } { 500 } { 1000000 } { red } { yellow } { green }
 }
\cs_new:Nn \manual_cell_colors:nnnnnn
 {
  \fp_compare:nTF { #1 > #3 }
   {
    \cellcolor{#6}
   }
   {
    \fp_compare:nTF { #1 < #2 }
     {
      \cellcolor{#4}
     }
     {
      \cellcolor{#5}
     }
   }
  #1
 }
\ExplSyntaxOff

\begin{document}

\begin{tabular}{|c|c|}
 \ca{500} &  \cb{500} \\
\ca{1000} &  \cb{100} \\
 \ca{300} & \cb{1500} \\
 \ca{150} &  \cb{600} \\
\ca{3000} & \cb{2000000}
\end{tabular}

\end{document}

enter image description here

egreg
  • 1,121,712