我有一张带有合并单元格的小表格。只要我不使用某种颜色填充背景,文本就会正常显示。但是当我提供颜色时,可见文本会被截断,并出现一条细线。

如何为包含合并单元格的整列着色?

\documentclass{article}

\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{xcolor}
\begin{document}

\newcommand{\TableMarks}{\multirow{2}{*}{\centering \textbf{Title}}}
\newcommand{\TableTotal}{\multirow{2}{*}{\cellcolor{lightgray}\centering \textbf{Total}}}
\newcommand{\TableTM}{\multirow{2}{*}{\cellcolor{lightgray}\centering \textbf{30}}}

\newcolumntype{a}{>{\columncolor{gray}}c}

\begin{center}
    \begin{tabular}{|a|l|l|l|l|l|l|l|}
    \hline
    \TableMarks & & A1 & A2 & A3 & A4 & \TableTM & \TableTotal  \\ 
    \cline{3-6} 
      &  & & & & & & \\ 
      \hline
    \end{tabular}
\end{center}

\end{document}

1

  • 2
    这个问题类似于:。如果您认为它们不同,请问题,说明它们有何不同和/或该问题的答案对您的问题无济于事。


    – 


最佳答案
3

您可以\multirow使用负行数设置“反向”:

\documentclass{article}

\usepackage{multirow}
\usepackage[table]{xcolor}

\begin{document}

\newcommand{\TableMarks}{\multirow{2}{*}{\centering \textbf{Title}}}
\newcommand{\TableTotal}{\multirow{2}{*}{\cellcolor{lightgray}\centering \textbf{Total}}}
\newcommand{\TableTM}{\multirow{2}{*}{\cellcolor{lightgray}\centering \textbf{30}}}

\newcolumntype{a}{>{\columncolor{gray}}c}

\begin{tabular}{|a|l|l|l|l|l|l|l|}
  \hline
  \TableMarks & & A1 & A2 & A3 & A4 & \TableTM & \TableTotal  \\ 
  \cline{3-6} 
  & & & & & & & \\ 
  \hline
\end{tabular}

\bigskip

\begin{tabular}{ *{8}{ | l } | }
  \hline
    \cellcolor{lightgray}                                & & A1 & A2 & A3 & A4 & 
    \cellcolor{lightgray} & 
    \cellcolor{lightgray}                                    \\ 
  \cline{3-6} 
  \multirow{-2}{*}{\cellcolor{lightgray}\bfseries Title} & &    &    &    &    & 
    \multirow{-2}{*}{\cellcolor{lightgray}\bfseries Total} & 
    \multirow{-2}{*}{\cellcolor{lightgray}\bfseries 30}      \\ 
  \hline
\end{tabular}

\end{document}

1

  • 虽然我已经使用了\cellcolor{lightgray}第一列,但您也可以通过定义的a列来设置/着色它。


    – 

您还可以使用tabularray

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularray}
\begin{document}
\begin{center}
    \begin{tblr}{
        colspec={c*{7}{l}},
        hlines,
        vlines,
        column{1}={gray},
        column{Y,Z}={lightgray},
        cell{1}{1,2,Y,Z}={r=2}{font=\bfseries},
        abovesep=3pt
        }
        Title & & A1 & A2 & A3 & A4 & 30 & Total \\ 
        & & & & & & & \\ 
    \end{tblr}
\end{center}
\end{document} 

{NiceTabular}nicematrix

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{center}
\begin{NiceTabular}{*{8}{l}}[hvlines]
\Block[fill=gray]{2-1}{\textbf{Title}} & \Block{2-1}{} & A1 & A2 & A3 & A4 &
   \Block[fill=lightgray]{2-1}{\textbf{30}} & \Block[fill=lightgray]{2-1}{\textbf{Total}} \\
\\
\end{NiceTabular}
\end{center}

\end{document}