Programando a cor da célula da tabela com base no conteúdo

Programando a cor da célula da tabela com base no conteúdo

Eu construí esta tabela com base em uma anteriorPerguntas e respostas:

insira a descrição da imagem aqui

Como você pode ver, há um problema com o cabeçalho.
Como posso desativar o destaque apenas para cabeçalhos?

Aqui está o código:

\documentclass{article}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{xcolor}

\makeatletter
\newcommand*{\yncellcolor}{}
\def\yncellcolor\ignorespaces{\@ifnextchar{1}{\cellcolor{green!20}}{\@ifnextchar{0}{\cellcolor{red!20}}{}}}
\newcolumntype{C}{>{\yncellcolor}c}
\makeatother

\begin{document}
\begin{table}[htp]
\caption{XOR}
\begin{center}
\begin{tabular}{CCCCCCCCCCCCCCCCCC}

\toprule
$Bit$       &   1   &   2   &   3   &   4   &   5   &   6   &   7   &   8   \\
\midrule 
$A$         &   0   &   1   &   0   &   1   &   0   &   1   &   0   &   1   \\
$B$         &   1   &   0   &   1   &   0   &   1   &   0   &   1   &   0   \\
$\bigoplus$ &   1   &   1   &   1   &   1   &   1   &   1   &   1   &   1   \\
\bottomrule

\end{tabular}
\end{center}
\label{XOR}
\end{table}

\end{document}

Responder1

Assim?

insira a descrição da imagem aqui

Depois de reparar (concluir) seu MWE... sugiro usar diferentes tipos de colunas para cabeçalhos de colunas definidos por \multicolumn{1}{c}{...}.

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{booktabs}

\makeatletter
\newcommand*{\yncellcolor}{}
\def\yncellcolor\ignorespaces{\@ifnextchar{1}{\cellcolor{green!20}}{\@ifnextchar{0}{\cellcolor{red!20}}{}}}
\newcolumntype{C}{>{\yncellcolor}c}
\makeatother
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % <--- new

\begin{document}
    \begin{table}[htp]
\caption{XOR}
\centering
\begin{tabular}{l| *{8}{C}}
    \toprule
$Bit$       &\mc{1} &\mc{2} &\mc{3} &\mc{4} &\mc{5} &\mc{6} &\mc{7} &\mc{8} \\ % <--- changed
    \midrule
$A$         &   0   &   1   &   0   &   1   &   0   &   1   &   0   &   1   \\
$B$         &   1   &   0   &   1   &   0   &   1   &   0   &   1   &   0   \\
$\bigoplus$ &   1   &   1   &   1   &   1   &   1   &   1   &   1   &   1   \\
    \bottomrule
\end{tabular}
\label{XOR}
    \end{table}
\end{document}

Nota: o uso das linhas verticais na tabela e as regras de uso booktabsnão funcionam bem juntos. Você deve considerar outra combinação de linhas (por exemplo, \hlineB{...}do boldlinepacote):

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{boldline}

\makeatletter
\newcommand*{\yncellcolor}{}
\def\yncellcolor\ignorespaces{\@ifnextchar{1}{\cellcolor{green!20}}{\@ifnextchar{0}{\cellcolor{red!20}}{}}}
\newcolumntype{C}{>{\yncellcolor}c}
\makeatother
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % <--- new

\begin{document}
    \begin{table}[htp]
\caption{XOR}
\centering
\begin{tabular}{>{\rule[-1ex]{0pt}{4ex}$}l<{$} | *{8}{C}}
    \hlineB{2}
\mathrm{Bit} &\mc{1} &\mc{2} &\mc{3} &\mc{4} &\mc{5} &\mc{6} &\mc{7} &\mc{8} \\ % <--- changed
    \hlineB{1.5}
A            &   0   &   1   &   0   &   1   &   0   &   1   &   0   &   1   \\
B            &   1   &   0   &   1   &   0   &   1   &   0   &   1   &   0   \\
\bigoplus    &   1   &   1   &   1   &   1   &   1   &   1   &   1   &   1   \\
    \hlineB{2}
\end{tabular}
\label{XOR}
    \end{table}
\end{document}

insira a descrição da imagem aqui

informação relacionada