如何为 tabularx-tables 中的多列单元格着色

如何为 tabularx-tables 中的多列单元格着色

我尝试使用这里提出的问题的答案:如何为单个多列单元格着色

但如果我使用更多行,且多列间距不同,一些彩色多列单元格的右侧仍然有白色填充。我做错了什么?

tabularx-tables 中的多列带有白色填充

\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
  \colorcell{2}{ a a a a a a a a a a a a}
    &  CLEAR
        &   \colorcell{2}{Colored Cell}
            &   CLEAR
                &   \colorcell{2}{Colored Cell}     \\
  \hline
  a a a a a a a a a a a a
    &  CLEAR
        &   CLEAR
            &   CLEAR
                &   \colorcell{4}{Colored Cell}     \\
  \hline


\end{tabularx}

\end{document}

答案1

在评估多列宽度时,您还减去了第一个和最后一个规则的厚度,这是不应该的。下面是一段简化的代码,它给出了预期的结果。我还必须定义两个变体(colorcell,以及\lcolorcell当 \multicolumn 是其行中的第一个时的情况)以避免出现双重垂直规则。

\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}[\tabcolsep]\hsize=%
 \dimexpr (\linewidth+2\tabcolsep + \arrayrulewidth)*#1-2\arrayrulewidth}C|}{#2}}

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

\begin{document}

Standard Column Color

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

\end{tabularx}

\end{document} 

在此处输入图片描述

答案2

使用(≥ 6.0 2021-08-10),您{NiceTabularX}可以nicematrix直接得到预期的结果。

输出的 PDF 在所有 PDF 查看器中都显示良好。在某些缩放级别下,垂直规则似乎不会消失。

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

\begin{document}

\begin{NiceTabularX}{\textwidth}{*{8}{X[m,c]}}[hvlines,colortbl-like]
  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}  \\
  \Block[fill=red]{1-2}{ 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-2}{Colored Cell}  \\
  a a a a a a a a a a a a
    &  CLEAR
        &   CLEAR
            &   CLEAR
                &   \Block[fill=red]{1-4}{Colored Cell}  \\
\end{NiceTabularX}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

答案3

这是表格的可转换变体。如您所见,规则是可见的。列和行的跨度可能很重要。

另外,您可以为列指定精确的值,而不是相对于边距计算它们。

在此处输入图片描述

\documentclass[11 pt, a4paper, oneside, openany]{book}
\usepackage{cals, caption}
\usepackage[svgnames]{xcolor}
\usepackage[T1]{fontenc}

\let\nc=\nullcell                        % Shortcuts
\let\sc=\spancontent

\begin{document}

\begin{table}[tbh!]
\caption{calstable can float\label{tab:calstable}}

\begin{calstable}[c]               % Centred between margins

% Defining columns relative to each other and relative to the margins
\colwidths{{\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
           {\dimexpr(\columnwidth/8)\relax}
      }
% The tabular fills the text area if sum of all columns is 8

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.4pt}          % Rule between header and body of the table
\def\cals@bgcolor{}
\def\cals@AtBeginCell{\vfil}           % Vertical centring all cell content
\setlength{\cals@paddingL}{3pt}        % cals’ tabcolsep,  
\setlength{\cals@paddingR}{3pt}


\def\lblue{\ifx\cals@bgcolor\empty     % "Switch" to turn on and off colour
    \def\cals@bgcolor{LightSteelBlue}
\else \def\cals@bgcolor{} \fi}


% R1H1
\thead{\footnotesize\bfseries
\brow
    \alignC\cell{7}
    \cell{6}
    \cell{5}
    \cell{4}
    \cell{3}
    \cell{2}
    \cell{1}
    \cell{0}
\erow
\mdseries}
\tfoot{\lastrule\strut}
%R2B1
\brow
    \lblue\cell{aaaaa\par aaaaa\par aa}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{rtb}\sc{Coloured cell}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{Coloured cell}\lblue
\erow
%R3B2
\brow
    \lblue\nc{ltb}
    \nc{rtb}\sc{aaaaaaaaaaaa}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{rtb}\sc{Coloured cell}\lblue
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{rtb}\sc{Coloured cell}\lblue
\erow
%R4B3
\brow
    \lblue\cell{aaaaa\par aaaaa\par aa}\lblue
    \cell{CLEAR}
    \cell{CLEAR}
    \cell{CLEAR}
    \lblue\nc{ltb}
    \nc{tb}
    \nc{tb}
    \nc{rtb}\sc{Coloured cell}\lblue
\erow
\makeatletter
\end{calstable}
\end{table}

\end{document}

相关内容