4

The following code works perfectly:

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{collcell}

 %The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.5} %
\newcommand*{\MaxNumber}{1.0}%

%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
        \ifdim #1 pt > \MidNumber pt
            \pgfmathsetmacro{\PercentColor}{max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)} %
            \hspace{-0.33em}\colorbox{green!\PercentColor!yellow}{#1}
        \else
            \pgfmathsetmacro{\PercentColor}{max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)} %
            \hspace{-0.33em}\colorbox{red!\PercentColor!yellow}{#1}
        \fi
}

\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}

\begin{document}
\begin{table}[ht]
\begin{center}
\begin{tabular}{*{10}{R}}
              0.03 & 0.34 & 0.41 & 0.25 & 0.89 & 0.49 & 0.79 & 0.83 & 0.82 & 0.94 \\    
\end{tabular}
\end{center}
\end{table}
\end{document}

When I edit the code above to extract the data from a file rather than directly in the code, it doesn't!

    . . . 
    \begin{table}[ht]
    \begin{center}
    \begin{tabular}{*{10}{R}}
    \input{"data.tex"}  
    \end{tabular}
    \end{center}
    \end{table}
    . . . 

If I change the column type from R to c, it compiles with no errors but something in the macro is not working when the data is in a separate file. The contents of the data.tex file are as follows:

  0.03 & 0.34 & 0.41 & 0.25 & 0.89 & 0.49 & 0.79 & 0.83 & 0.82 & 0.94 \\

How can I find a workaround to this?

3

The problem is that \input is not expandable and this blocks the expansions that are needed before the collcell feature can work, so basically everything in the input file ends up in the first column, which of course will not work. See Why is \input not expandable? for more information.

Use the expandable version of \input, that is, the primitive that's saved by LaTeX in \@@input.

\begin{filecontents*}{\jobname.dat}
0.03 & 0.34 & 0.41 & 0.25 & 0.89 & 0.49 & 0.79 & 0.83 & 0.82 & 0.94 \\
\end{filecontents*}

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{collcell}

 %The min, mid and max values
\newcommand*{\MinNumber}{0.0}%
\newcommand*{\MidNumber}{0.5} %
\newcommand*{\MaxNumber}{1.0}%

%Apply the gradient macro
\newcommand{\ApplyGradient}[1]{%
  \ifdim #1 pt > \MidNumber pt
    \pgfmathsetmacro{\PercentColor}{%
      max(min(100.0*(#1 - \MidNumber)/(\MaxNumber-\MidNumber),100.0),0.00)
     }%<---- fixed
    \hspace{-0.33em}\colorbox{green!\PercentColor!yellow}{#1}%<---- fixed
  \else
    \pgfmathsetmacro{\PercentColor}{
      max(min(100.0*(\MidNumber - #1)/(\MidNumber-\MinNumber),100.0),0.00)
    }%<---- fixed
    \hspace{-0.33em}\colorbox{red!\PercentColor!yellow}{#1}%<---- fixed
  \fi
}

\newcolumntype{R}{>{\collectcell\ApplyGradient}c<{\endcollectcell}}
\renewcommand{\arraystretch}{0}
\setlength{\fboxsep}{3mm} % box size
\setlength{\tabcolsep}{0pt}

\makeatletter
\newcommand{\xinput}[1]{\@@input #1 }
\makeatother

\begin{document}
\begin{table}[ht]
\begin{center}
\begin{tabular}{*{10}{R}}
0.03 & 0.34 & 0.41 & 0.25 & 0.89 & 0.49 & 0.79 & 0.83 & 0.82 & 0.94 \\    

\xinput{\jobname.dat}
\end{tabular}
\end{center}
\end{table}
\end{document}

I used filecontents and \jobname.dat just not to clobber my files, you can pass any file name you want.

I also fixed some spurious spaces you had in the code.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.