Como fazer com que a mistura de cores xcolor funcione no newcommand

Como fazer com que a mistura de cores xcolor funcione no newcommand

Quero definir a cor de uma célula da tabela usando o argumento passado para um comando, mas parece não ter nenhum efeito. Aqui está meu código:

\PassOptionsToPackage{table}{xcolor}
\documentclass{beamer}

\usepackage{xcolor}

\newcommand*{\heatmap}[1][]{\cellcolor{red!#1} {#1}}
\begin{document}

\section{Background}

\begin{frame}
    \frametitle{The size and type of repeats}
    \begin{tabular}{lcccc}
        Species & genomes & G/C & A/T & Di-\\
        Widget & 7 & \heatmap{97}\% & \heatmap{3}\% & 0\%\\
    \end{tabular}
\end{frame}

\end{document}

Minha expectativa é que as duas células com a \heatmapmacro usada saiam com diferentes tons de vermelho, mas, na verdade, ambas saiam totalmente vermelhas. Como posso fazer com que o comando funcione nessas circunstâncias?

Responder1

Trata-se de fazer com que o comando espere um argumento opcional (o que não parece apropriado neste caso). Compare a diferença:

\PassOptionsToPackage{table}{xcolor}
\documentclass{beamer}

\usepackage{xcolor}

\newcommand*{\heatmap}[1][]{\cellcolor{red!#1} {#1}}
\newcommand*{\xheatmap}[1]{\cellcolor{red!#1} {#1}}
\begin{document}

\section{Background}

\begin{frame}
    \frametitle{The size and type of repeats}
    \begin{tabular}{lcccc}
        Species & genomes & G/C & A/T & Di-\\
        Widget & 7 & \heatmap[97]\% & \heatmap[30]\% & 0\%\\
        Widget & 7 & \heatmap[57]\% & \heatmap[30!blue!30]\% & 0\%\\
        Widget & 7 & \xheatmap{97}\% & \xheatmap{30}\% & 0\%\\
    \end{tabular}
\end{frame}

\end{document}

informação relacionada