在 table/tabularx 上发布垂直对齐问题

在 table/tabularx 上发布垂直对齐问题

在此处输入图片描述

从图片中可以看出,我在将某些文本居中于特定单元格(大多数\multirow单元格)内时遇到了一些麻烦。我的目的是让突出显示的文本垂直和水平居中,就像在第一个单元格(即带有字符串的单元格)中已经发生的那样Attack vector。为了实现这一点,我还缺少什么?

以下是相关的 Latex 代码:

\documentclass{article}

% Packages and some configs
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{booktabs, makecell}

\usepackage{colortbl}
\definecolor{Gray}{gray}{0.95}
\newcommand{\GC}[1]{\cellcolor{Gray}} % Grey Cell

\renewcommand\tabularxcolumn[1]{m{#1}} % Vertical center alignment
\newcolumntype{C}{>{\centering\arraybackslash}X} % Horizontal center alignment

\begin{document}

\begin{tabularx}{1\textwidth}{
>{\hsize=1.2\hsize\linewidth=\hsize}C % 0.4 of the overall width
>{\hsize=0.9\hsize\linewidth=\hsize}C % 0.3 of the overall width
>{\hsize=0.9\hsize\linewidth=\hsize}C} % 0.3 of the overall width
    \toprule
    \textbf{Metric} & \textbf{Metric Value} & \textbf{Numerical Value} \\ \midrule

    \multirow{4}{*}{Attack Vector (AV)} & Network (N) & 0.85 \\
    & \GC{20} Adjacent (A) & \GC{20} 0.62 \\ 
    & Local (L) & 0.55 \\
    & \GC{20} Physical (P) & \GC{20} 0.2 \\
    
    \GC{20} & Low (L) & 0.77 \\
    \multirow{-2}{*}{\GC{20} Attack Complexity (AC)} & \GC{20} High (H) &  \GC{20} 0.44 \\

    \multirow{3}{*}{Privileges Required (PR)} & None (N) & 0.85 \\
    & \GC{20} Low (L) & \GC{20} 0.62 (or 0.68 if \textit{Scope} is \textit{Changed}) \\
    & High (H) & 0.27 (or 0.5 if \textit{Scope} is \textit{Changed}) \\

    \GC{20} & None (N) & 0.85 \\
    \multirow{-2}{*}{\GC{20} User Interaction (UI)} & \GC{20} Required (R) & \GC{20} 0.62 \\

    & None (N) & 0 \\
    & \GC{20} Low (L) & \GC{20} 0.22 \\
    \multirow{-2}{0.4\textwidth}{Confidentiality (C) Integrity (I) Availability (A)} & High (H) & 0.56 \\ 
    
    \bottomrule
\end{tabularx}

\end{document}

答案1

您的表格布局不太容易理解。因此,我建议采用以下略有不同的布局;其主要目标是简洁和易读。

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath}  % for smash[b] macro
\usepackage{booktabs} % for well-spaced horizontal rules
\usepackage{siunitx}  % for 'S' column type
\usepackage[flushleft]{threeparttable} % for 'threeparttable' env.
\renewcommand\TPTtagStyle{\textit}

\newcommand\mytab[1]{\smash[b]{%
  \begin{tabular}[t]{@{}l@{}} #1 \end{tabular}}}

\begin{document}

\begin{center}
\begin{threeparttable}
\begin{tabular}{@{} l l S[table-format=1.2] @{}} 
\toprule
Metric & Metric Value & {Numerical Value} \\ 
\midrule
Attack Vector (AV)
       & Network (N)   & 0.85 \\
       & Adjacent (A)  & 0.62 \\ 
       & Local (L)     & 0.55 \\
       & Physical (P)  & 0.2  \\ 
\addlinespace
    
Attack Complexity (AC)
       & Low (L)       & 0.77 \\ 
       & High (H)      & 0.44 \\
\addlinespace

Privileges Required (PR)
       & None (N)      & 0.85 \\
       & Low (L)       & 0.62\tnote{a} \\
       & High (H)      & 0.27\tnote{b} \\
\addlinespace

User Interaction (UI)
       & None (N)      & 0.85 \\
       & Required (R)  & 0.62 \\
\addlinespace    
     
\mytab{Confidentiality (C), Integrity (I),\\ 
       \quad Availability (A)}
       & None (N)      & 0    \\ 
       & Low (L)       & 0.22 \\
       & High (H)      & 0.56 \\ 
    
\bottomrule
\end{tabular}

\footnotesize
\begin{tablenotes}
\item[a] or 0.68 if \textit{Scope} is \textit{Changed}
\item[b] or 0.5 if \textit{Scope} is \textit{Changed}
\end{tablenotes}

\end{threeparttable}
\end{center}

\end{document}

答案2

multirow命令有一个选项可以调整单元格内容的垂直对齐方式——请参阅手册第 3 页和第 4 页。将该垂直调整选项添加到 MWE 会将两个单元格的内容移动到所需位置。请注意:在调整后的代码中,这两个添加是直观完成的。可能有一种方法可以精确测量多行单元格的高度并将其减半。但这超出了我的 LaTeX 知识范围。

\documentclass{article}

% Packages and some configs
\usepackage{amsmath}
\usepackage{multirow}
\usepackage{tabularx}
\usepackage{booktabs, makecell}

\usepackage{colortbl}
\definecolor{Gray}{gray}{0.95}
\newcommand{\GC}[1]{\cellcolor{Gray}} % Grey Cell

\renewcommand\tabularxcolumn[1]{m{#1}} % Vertical center alignment
\newcolumntype{C}{>{\centering\arraybackslash}X} % Horizontal center alignment

\begin{document}
    
    \begin{tabularx}{1\textwidth}{
            >{\hsize=1.2\hsize\linewidth=\hsize}C % 0.4 of the overall width
            >{\hsize=0.9\hsize\linewidth=\hsize}C % 0.3 of the overall width
            >{\hsize=0.9\hsize\linewidth=\hsize}C} % 0.3 of the overall width
        \toprule
        \textbf{Metric} & \textbf{Metric Value} & \textbf{Numerical Value} \\ \midrule
        
        \multirow{4}{*}{Attack Vector (AV)} & Network (N) & 0.85 \\
        & \GC{20} Adjacent (A) & \GC{20} 0.62 \\ 
        & Local (L) & 0.55 \\
        & \GC{20} Physical (P) & \GC{20} 0.2 \\
        
        \GC{20} & Low (L) & 0.77 \\
        \multirow{-2}{*}{\GC{20} Attack Complexity (AC)} & \GC{20} High (H) &  \GC{20} 0.44 \\
        
        \multirow[c]{3}{*}[-12pt]{Privileges Required (PR)} & None (N) & 0.85 \\ % [-12pt] added
        & \GC{20} Low (L) & \GC{20} 0.62 (or 0.68 if \textit{Scope} is \textit{Changed}) \\
        & High (H) & 0.27 (or 0.5 if \textit{Scope} is \textit{Changed}) \\
        
        \GC{20} & None (N) & 0.85 \\
        \multirow{-2}{*}{\GC{20} User Interaction (UI)} & \GC{20} Required (R) & \GC{20} 0.62 \\
        
        & None (N) & 0 \\
        & \GC{20} Low (L) & \GC{20} 0.22 \\
        \multirow{-2}{0.4\textwidth}[5pt]{Confidentiality (C) Integrity (I) Availability (A)} & High (H) & 0.56 \\ % [5pt] added
        
        \bottomrule
    \end{tabularx}
    
\end{document}

结果(您可能需要对给定的值进行一些调整,因为Low条目与文本的其余部分不太一致): 在此处输入图片描述

相关内容