我正在尝试为包含单元格的表格内的各个单元格着色multicolumn
,但是使用\colorcell
或\columncolor
会导致单元格右侧出现白色填充。
我找到了相关问题,但它们似乎都与包含的表格中的非多列单元格的着色有关multicolumns
。但我关心的是多列单元格本身的着色。
IE。
我尝试了以下方法,但都不太管用。设置\tabcolsep
为的方法0pt
最接近我想要的,但我不喜欢文本一直移动到垂直线的方式。
\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}
答案1
像这样:
在计算多列宽度时你忘记了\tabcolsep
和\arrayrulewidth
。在新 comeands 的定义中考虑它们cell
(实际上在你的 mwe 中是不必要的)- 考虑到它你的 mwe 变成:
\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
{NiceTabular}
您可以使用轻松创建该表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}
您需要进行多次编译(因为nicematrix
在后台使用了 PGF/TikZ 节点)。