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

Bobyandbob
  • 4,899

2 Answers2

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}
Zarko
  • 296,517
0

You can easily create that table with {NiceTabular} of nicematrix.

\documentclass{report}
\usepackage[margin=1in,headheight=.5in,headsep=0.25in]{geometry}
\usepackage{nicematrix}

\begin{document}

\noindent \begin{NiceTabular}{*{8}{X[c,m]}}[hvlines,color-inside] 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0 \ \cellcolor{red} a a a a a a a a a a a a & CLEAR & \Block[fill=red]{1-2}{Colored Cell} & & CLEAR & \Block[fill=red]{1-3}{Colored Cell} & \end{NiceTabular}

\end{document}

You need several compilations (because nicematrix uses PGF/TikZ nodes under the hood).

Output of the above code

F. Pantigny
  • 40,250