表格 \rowcolor \columncolor \multirow 和 \multicolumn 对齐

表格 \rowcolor \columncolor \multirow 和 \multicolumn 对齐

我正在尝试在 LaTeX 中从 Word 重新创建此表格:

在此处输入图片描述

我已经接近了,但是“有效性”部分没有正确对齐,并且“概率”部分没有完全填充颜色。

在此处输入图片描述

这是我目前拥有的代码,有人能给我指出正确的方向吗?

\documentclass[11pt]{article}
\usepackage{multirow}
\usepackage[table]{xcolor}  

\begin{document}
\begin{center}
\begin{table}[!htbp]
\noindent
\begin{tabular}{|c|p{3cm}|p{3cm}|p{3cm}|}
\hline
\multirow{2}{3cm}{\centering \cellcolor{gray!50} Effectiveness of Controls} &    \multicolumn{3}{p{9cm}|}{\centering \cellcolor{gray!50} Probability of Threat Occurrence (Natural or Environmental Threats) or Threat Motivation and Capability (Human Threats)} \\
\cline{2-4}
\rowcolor{gray!50}
& Low & Moderate & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} Low & Moderate & High & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} Moderate & Low & Moderate & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} High & Low & Low & Moderate \\
\hline
\end{tabular}
\caption{Risk Classifications}
\label{tab:xyz}

\end{table}
\end{center}
\end{document}

答案1

您可以Effectiveness...通过

\multirow{2}{\dimexpr0.25\textwidth-2\arrayrulewidth-2\tabcolsep\relax}[-0.5cm]{...

注意[-0.5cm]并调整垂直定位。我已调整列宽,使其与文本宽度相适应。

\documentclass[11pt,draft]{article}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{center}
\begin{table}[!htbp]
\noindent
\begin{tabular}{|c|C{\dimexpr0.25\textwidth-\arrayrulewidth-2\tabcolsep\relax}
           |C{\dimexpr0.25\textwidth-\arrayrulewidth-2\tabcolsep\relax}
           |C{\dimexpr0.25\textwidth-\arrayrulewidth-2\tabcolsep\relax}|}
\hline
\multirow{2}{\dimexpr0.25\textwidth-2\arrayrulewidth-2\tabcolsep\relax}[-0.5cm]{\centering \cellcolor{gray!50} Effectiveness of Controls} &    \multicolumn{3}{p{\dimexpr 0.75\textwidth-\arrayrulewidth-2\tabcolsep\relax}|}{\cellcolor{gray!50}\centering Probability of Threat Occurrence (Natural or Environmental Threats) or Threat Motivation and Capability (Human Threats)} \\
\cline{2-4}
\cellcolor{gray!50}
& \cellcolor{gray!50} Low & \cellcolor{gray!50} Moderate & \cellcolor{gray!50} High \\
\hline
%\rowcolor{white!50}
\cellcolor{gray!50} Low & Moderate & High & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} Moderate & Low & Moderate & High \\
\hline
\rowcolor{white!50}
\cellcolor{gray!50} High & Low & Low & Moderate \\
\hline
\end{tabular}
\caption{Risk Classifications}
\label{tab:xyz}

\end{table}
\end{center}
\end{document}

在此处输入图片描述

答案2

这种类型的表格非常容易构建卡路里。如你所见,灰色中没有白线,所有规则同样可见:

\documentclass{article}
\usepackage{cals, caption, xcolor}

\let\nc=\nullcell           % CALS Shortcuts for spanning cells
\let\sc=\spancontent


\begin{document}

\begin{table}[!htbp]
\begin{calstable}[c]  % Centred CALS tabular

% Calculate the column width
\colwidths{{\dimexpr(\columnwidth)/4\relax}
            {\dimexpr(\columnwidth)/4\relax}
            {\dimexpr(\columnwidth)/4\relax}
            {\dimexpr(\columnwidth)/4\relax}
    }

% The tabular fills the text area if sum of all columns is 4

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside horizontal frame rules
\def\cals@framecs@width{0,4pt}   % Outside vertical frame rules
\def\cals@bodyrs@width{0.4pt}    % Rule between header and tabular body
\def\cals@cs@width{0.4pt}        % Inside vertical rules
\def\cals@rs@width{0.4pt}        % Inside horizontal rules
\def\cals@bgcolor{}              % To avoid undefined

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

% R1H1     % Just a comment: Row one, header row one
\thead{
\brow
    \gray\alignC\nc{lrt}    % Switch to gray background and centred text, works until switched off
    \nc{ltb}
    \nc{tb}
    \nc{rtb}\sc{\vfil Probability of Threat Occurrence (Natural or Environmental Threats) or Threat Motivation and Capability (Human Threats)}
\erow
%R2H2       % Row two, header row two
\brow
    \nc{lrb}\sc{\vfil Effectiveness of Controls}  % "vfil" centres the cell content vertically
    \cell{\vfil Low}
    \cell{\vfil Moderate}
    \cell{\vfil High}\gray % Always reset colour at end of header 
\erow
}  % closing 'thead' clause
%R3B1     % Row three, body row one
\brow
    \gray\cell{Low}\gray   % Switch to gray background for first cell (column 1)
    \cell{\vfil Moderate}
    \cell{\vfil High}
    \cell{\vfil High}
\erow
%R4B2
\brow
    \gray\cell{Moderate}\gray
    \cell{\vfil Low}
    \cell{\vfil Moderate}
    \cell{\vfil High}
\erow
%R5B3
\brow
    \gray\cell{High}\gray
    \cell{\vfil Low}
    \cell{\vfil Low}
    \cell{\vfil Moderate}
\erow
\makeatletter
\end{calstable}\par
\caption{Risk Classifications}
\label{tab:xyz}
\end{table}
\end{document}

在此处输入图片描述

答案3

{NiceTabular}您可以使用绘制此表格nicematrix

\documentclass[11pt]{article}
\usepackage{nicematrix}

\begin{document}

\begin{table}[!htbp]
\centering
\begin{NiceTabular}{cccc}[hvlines,columns-width=3cm] 
\CodeBefore
  \rowcolor{gray!50}{1,2} 
  \columncolor{gray!50}{1}
\Body
  \Block{2-1}{Effectiveness of\\ Controls} &    
  \Block{1-3}{Probability of Threat Occurrence (Natural or\\
            Environmental Threats) or Threat Motivation and\\
             Capability (Human Threats)} \\
      & Low & Moderate & High \\
  Low & Moderate & High & High \\
  Moderate & Low & Moderate & High \\
  High & Low & Low & Moderate \\
\end{NiceTabular}
\caption{Risk Classifications}
\label{tab:xyz}
\end{table}

\end{document}
  • 您需要多次编译。
  • colortbl在某些 PDF 查看器(例如 Adob​​e Reader)中使用时,规则似乎不会在某些缩放级别消失。
  • 您不会像colortbl在某些 PDF 查看器(例如 Firefox 中使用的 SumatraPDF 或 PDF.js)中使用时那样看到细白线(单元格 1-1 和 1-2 之间)。

上述代码的输出

相关内容