3

I built this table based on an earlier Q&A:

enter image description here

As you can see there's a problem with the header.
How can I disable the highlighting for headers only?

Here's the code:

\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}
  • Please make your table compilable! – Zarko Aug 4 at 22:02
4

Like this?

enter image description here

After repairing (completing) your MWE ... I suggest to use different column types for column headers defined by \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}

Note: use of the vertical lines in table and use rules from booktabs doesn't work well together. You should consider other combination of lines (for example \hlineB{...} from boldline package):

\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}

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.