2

I am trying to color individual cells inside of a table containing multicolumn cells, however Using \colorcell, or \columncolor results in cells which have white padding on the right hand side.

I have found relevant questions, however they all seem to be concerned with coloring non-multicolumn cells within a table containing multicolumns. However I am concerned with coloring the multicolumn cell itself.

ie.

Table cell color overlaps left cell border of first column which is multicolumn

How to color only one cell in a multicolumn table?

I have tried the following, however none of them quite work. The method with setting \tabcolsep to 0pt is the closest to wht I want, however I don't like how the text goes all the way to the vertical line.

\documentclass{report}
  \usepackage[margin=1in,headheight=.5in,headsep=0.25in]{geometry} 
  \usepackage{tabularx,colortbl,ltablex}
  \renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column
  \newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand{\cell}[2]{\multicolumn{#1}{|>{\hsize=#1\hsize}C|}{#2}}
\newcommand{\colorcell}[2]{\multicolumn{#1}{|>{\columncolor{red}\hsize=#1\hsize}C|}{#2}}
\newcommand{\colorcellb}[2]{\multicolumn{#1}{|>{\columncolor{red}[\tabcolsep][#1\tabcolsep]\hsize=#1\hsize}C|}{#2}}

\begin{document}
\keepXColumns

Standard Column Color
\begin{tabularx}{\textwidth}{|*{8}{C|}}
  \hline
  7&6&5&4&3&2&1&0\\
  \hline
  \colorcell{1}{ a a a a a a a a a a a a}&\cell{1}{CLEAR}&\colorcell{2}{Colored Cell}&\cell{1}{CLEAR}&\colorcell{3}{Colored Cell}\\
  \hline
\end{tabularx}

ColumnColor + Overhangs
\begin{tabularx}{\textwidth}{|*{8}{C|}}
  \hline
  7&6&5&4&3&2&1&0\\
  \hline
  \colorcellb{1}{ a a a a a a a a a a a a}&\cell{1}{CLEAR}&\colorcellb{2}{Colored Cell}&\cell{1}{CLEAR}&\colorcellb{3}{Colored Cell}\\
  \hline
\end{tabularx}

TabColSep=0
\setlength{\tabcolsep}{0 pt}
\begin{tabularx}{\textwidth}{|*{8}{C|}}
  \hline
  7&6&5&4&3&2&1&0\\
  \hline
  \colorcell{1}{ a a a a a a a a a a a a}&\cell{1}{CLEAR}&\colorcell{2}{Colored Cell}&\cell{1}{CLEAR}&\colorcell{3}{Colored Cell}\\
  \hline
\end{tabularx}

\end{document}

enter image description here

1
3

like this:

enter image description here

in calculation of multi column width you forgoth on \tabcolsep and \arrayrulewidth. considering them in your definition of new comeands cell (actually is unnecessary in your mwe)- considering it your mwe become:

\documentclass{report}
\usepackage[margin=1in,headheight=.5in,headsep=0.25in]{geometry}
\usepackage[table]{xcolor}
\usepackage{ltablex}
\keepXColumns
\renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column
\newcolumntype{C}{>{\centering\arraybackslash}X}

\newcommand{\cell}[2]{\multicolumn{#1}{|>{\hsize=%
    \dimexpr#1\linewidth+#1\tabcolsep+#1\tabcolsep-#1\arrayrulewidth}C|}{#2}}
\newcommand{\colorcell}[2]{\multicolumn{#1}{|>{\columncolor{red}\hsize=%
    \dimexpr#1\linewidth+#1\tabcolsep+#1\tabcolsep-#1\arrayrulewidth}C|}{#2}}

\begin{document}

Standard Column Color

\begin{tabularx}{\textwidth}{|*{8}{C|}}
  \hline
  7 & 6 & 5 & 4 & 3 & 2 & 1 & 0     \\
  \hline
  \colorcell{1}{ a a a a a a a a a a a a}
    &  CLEAR
        &   \colorcell{2}{Colored Cell}
            &   CLEAR
                &   \colorcell{3}{Colored Cell}     \\
  \hline
\end{tabularx}

\end{document}
2
  • Thank You!! I would mark as accepted, but apparently asked from an unregistered account Aug 28 '18 at 15:27
  • @DanChianucci, don't rush with acceptance :-), however register your account :-)
    – Zarko
    Aug 28 '18 at 15:30

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.