仅在特定列和行上的一个单元格上垂直对齐高级长表

仅在特定列和行上的一个单元格上垂直对齐高级长表

我正在尝试开发一个有点复杂的 LaTeX longtable

我已经实现了大部分目标,但除了我想要的帮助之外,我还想知道我所做的是否是最好的方法,或者是否没有更简单或更现代的方法。

我有以下 LaTeX 表:

\documentclass{article}

% Packages
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{plex-sans}
\usepackage{longtable}

\renewcommand{\familydefault}{\sfdefault}

\title{Table test}
\author{Me}
\date{\today}

\begin{document}

\maketitle

\begin{longtable}{|p{\dimexpr 0.25\linewidth-2\tabcolsep}|p{\dimexpr 0.25\linewidth-2\tabcolsep}|p{\dimexpr 0.25\linewidth-2\tabcolsep}|p{\dimexpr 0.25\linewidth-2\tabcolsep}|}    
    \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{\cellcolor[HTML]{E7E6E6}\textbf{Resume}} \\ 
    \hline
    \multicolumn{1}{|l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \textbf{Description}}} & \multicolumn{3}{p{\dimexpr 0.75\linewidth-2\tabcolsep}|}{Description here. This text is longer and should automatically wrap to the next line, ensuring that the content does not extend beyond the cell boundaries.} \\ \hline
    \endfirsthead
    %
    \endhead
    %
\multicolumn{1}{|l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \textbf{Severity}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{C00000}{\color[HTML]{FFFFFF} \textbf{High}}} & \multicolumn{2}{p{\dimexpr 0.5\linewidth-2\tabcolsep}|}{Severity description here. This text needs to be detailed enough to involve word wrapping within the cell.} \\ \hline
    \multicolumn{1}{|l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \textbf{Perimeter}}} & \multicolumn{1}{l|}{\textbf{Internal}} & \multicolumn{1}{l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \textbf{Authentication}}} & \textbf{No} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{\cellcolor[HTML]{E7E6E6}\textbf{TECHNICAL DETAILS}} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{Technical details here. This field must support a significant amount of text and must allow detailed discussion of the technical aspects of the topic in question.} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{\cellcolor[HTML]{E7E6E6}\textbf{RECOMMENDATION}} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{Recommendation here. Recommendations should be detailed, providing clear and concise instructions to mitigate identified risks or issues.} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{\cellcolor[HTML]{E7E6E6}\textbf{AFFECTED SERVICES}} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{https://google.com} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{https://google.com} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{\cellcolor[HTML]{E7E6E6}\textbf{REFERENCES}} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{https://google.com} \\ \hline
    \multicolumn{4}{|p{\dimexpr \linewidth-2\tabcolsep}|}{https://google.com} \\ \hline
    \caption{Vulnerability sheet}
    \label{tab:Vulnerability sheet}\\
\end{longtable}

\end{document}

但是,我想将“高”文本垂直居中在 C00000 彩色单元格内,而不仅仅是水平居中,但是当我这样做时,文本换行会比已经损坏的更加严重。

我不仅希望“高”文本垂直居中,还希望“描述”和“严重性”也垂直居中,而这一切都不会破坏“此处的描述”和“此处的严重性描述”内的文本,我想修复所有这些问题而不使这些列变得灵活,因为这就是我想要的单元格“描述”、“严重性”、“高”、“周长”、“内部”、“身份验证”和“否”的确切宽度。

TL;DR 将“高”文本垂直居中,并修复前两段的文本换行问题,而不会影响此表中其他元素的对齐方式。

如果有人能帮助我,我会非常高兴,我已经被这个问题困扰很长时间了,谢谢!

附件中有一张包含当前表格的图像,旁边还有红色箭头指示我想要修复的内容。

谢谢!注意:我正在使用 XeLaTeX。

我想修复的对齐

答案1

注意:根据以下 OP 的评论改进解决方案。

这是一个带有软件包的解决方案tabularray

\documentclass{article}

% Packages
\usepackage{xcolor}
\usepackage{plex-sans}
\usepackage{tabularray}

\renewcommand{\familydefault}{\sfdefault}

\title{Table test}
\author{Me}
\date{\today}

\begin{document}

\maketitle

\definecolor{dark}{HTML}{808080}
\definecolor{dred}{HTML}{C00000}
\definecolor{light}{HTML}{E7E6E6}

\begin{longtblr}[%
  caption=Vulnerability sheet,
  label=tab:Vulnerability sheet]
  {hlines,vlines,colspec={*4{m{\dimexpr 0.25\linewidth-2\tabcolsep}}}
  }
  \SetCell[c=4]{wd=\dimexpr \linewidth-2\tabcolsep,bg=light} \textbf{Resume} & & & \\
  \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Description} & \SetCell[c=3]{m,wd=\dimexpr 0.75\linewidth-2\tabcolsep} Description here. This text is longer and should automatically wrap to the next line, ensuring that the content does not extend beyond the cell boundaries. \\
  \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Severity} & \SetCell[c=1]{c,m,bg=dred,fg=white} \textbf{High} & \SetCell[c=2]{m,wd=\dimexpr 0.5\linewidth-2\tabcolsep} Severity description here. This text needs to be detailed enough to involve word wrapping within the cell. \\
  \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Perimeter} & \SetCell[c=1]{l,m} \textbf{Internal} & \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Authentication} & \textbf{No} \\
  \SetCell[c=4]{l,bg=light} \textbf{TECHNICAL DETAILS} & & & \\
  \SetCell[c=4]{wd=\dimexpr \linewidth-2\tabcolsep} Technical details here. This field must support a significant amount of text and must allow detailed discussion of the technical aspects of the topic in question. \\
  \SetCell[c=4]{l,bg=light} \textbf{RECOMMENDATION} & & & \\
  \SetCell[c=4]{wd=\dimexpr \linewidth-2\tabcolsep} Recommendation here. Recommendations should be detailed, providing clear and concise instructions to mitigate identified risks or issues. \\
  \SetCell[c=4]{l,bg=light} \textbf{AFFECTED SERVICES} \\
  \SetCell[c=4]{wd=\dimexpr \linewidth-2\tabcolsep} https://google.com & & & \\
  \SetCell[c=4]{wd=\dimexpr \linewidth-2\tabcolsep} https://google.com & & & \\
  \SetCell[c=4]{l,bg=light} \textbf{REFERENCES} & & & \\
  \SetCell[c=4]{wd=\dimexpr \linewidth-2\tabcolsep} https://google.com & & & \\
  \SetCell[c=4]{wd=\dimexpr \linewidth-2\tabcolsep} https://google.com & & & \\
\end{longtblr}

\end{document}

在此处输入图片描述

2. 改进解决方案,其中“描述”、“严重性”、“周长”、“高”和“内部”列采用其自然宽度。这意味着我们必须计算文本必须在换行符处换行的列宽。

为了证明列宽计算正确,我使用这些单元格中的文本进行对齐,并放置一条以计算出的宽度作为其长度的水平线。

\documentclass{article}

% Packages
\usepackage{xcolor}
\usepackage{plex-sans}
\usepackage{tabularray}

\renewcommand{\familydefault}{\sfdefault}

\title{Table test}
\author{Me}
\date{\today}

\begin{document}

\maketitle

\definecolor{dark}{HTML}{808080}
\definecolor{dred}{HTML}{C00000}
\definecolor{light}{HTML}{E7E6E6}

% The first solution used \tabcolsep to calculate column widths. These
% calculations were leftovers from the longtable solution. However
% \tabcolsep is not used in tabularray. Instead, tabularray has \leftsep and
% \rightsep, but these are only valid inside a cell, because they can be
% different for each cell. The use of \tabcolsep still worked in the
% first solution because by default it has the same value as \leftsep
% and \rightsep (6pt, also set by the colsep parameter).
% To make thinks a bit cleaner we define a length \colsep instead.
% But because we always used 2\tabcolsep, we make \colsep twice as
% big (or more precise, equal to \leftsep+\rightsep).

\newlength\colsep
\setlength\colsep{12pt}

% We also have to take into account the width of the vertical lines
\newlength\vlinewidth
\setlength\vlinewidth{0.4pt}

% We now define length parameters for the various column widths.

\newlength\WOne % column with "Description" etc.
\newlength\WOnea % column with "High"
\newlength\WTwo %  2 columns next to "High"
\newlength\WThree % 3 columns next to "Description"
\newlength\WFour % 4 columns total width

% Calculate the various widths. Take into account the colseps and the
% vertical line widths.
                 
\settowidth\WOne{\textbf{Description}} % This is the longest of the three Description/Severity/Perimeter
\settowidth\WOnea{\textbf{Internal}} % This is the longest of the two High/Internal
% NOTE: If you want to let LaTeX calculate the longest one, you can use
% something like:
% \settowidth\WOne{%
%   \begin{tblr}{colsep=0pt,l}
%     \textbf{Description}\\
%     \textbf{Severity}\\
%     \textbf{Perimeter}
%   \end{tblr}%
% }

\setlength\WFour{\dimexpr\linewidth-\colsep-2\vlinewidth}
\setlength\WTwo{\dimexpr\linewidth-\WOne-\WOnea-3\colsep-4\vlinewidth}
\setlength\WThree{\dimexpr\linewidth-\WOne-2\colsep-3\vlinewidth}

\begin{longtblr}[%
  caption=Vulnerability sheet,
  label=tab:Vulnerability sheet]
  {hlines,vlines,colspec={lllm{\dimexpr 0.25\linewidth-\colsep}}
  }
  \SetCell[c=4]{wd=\WFour,bg=light} \textbf{Resume} & & & \\
  \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Description} & \SetCell[c=3]{j,m,wd=\WThree}
   {Description here. This text is longer and should automatically wrap to the next
    line, ensuring that the content does not extend beyond the cell boundaries.\\
    \rule{\WThree}{1pt}}\\
  \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Severity} & \SetCell[c=1]{c,m,bg=dred,fg=white} \textbf{High} & \SetCell[c=2]{j,m,wd=\WTwo}
   {Severity description here. This text needs to be detailed enough to
    involve word wrapping within the cell.\\
    \rule{\WTwo}{1pt}} \\
  \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Perimeter} & \SetCell[c=1]{l,m} \textbf{Internal} & \SetCell[c=1]{l,m,bg=dark,fg=white} \textbf{Authentication} & \textbf{No} \\
  \SetCell[c=4]{l,bg=light} \textbf{TECHNICAL DETAILS} & & & \\
  \SetCell[c=4]{j,wd=\WFour} {Technical details here. This field must
    support a significant amount of text and must allow detailed discussion
    of the technical aspects of the topic in question.\\
    \rule{\WFour}{1pt}} \\
  \SetCell[c=4]{l,bg=light} \textbf{RECOMMENDATION} & & & \\
  \SetCell[c=4]{j,wd=\WFour} Recommendation here. Recommendations should be detailed, providing clear and concise instructions to mitigate identified risks or issues. \\
  \SetCell[c=4]{l,bg=light} \textbf{AFFECTED SERVICES} \\
  \SetCell[c=4]{wd=\WFour} https://google.com & & & \\
  \SetCell[c=4]{wd=\WFour} https://google.com & & & \\
  \SetCell[c=4]{l,bg=light} \textbf{REFERENCES} & & & \\
  \SetCell[c=4]{wd=\WFour} https://google.com & & & \\
  \SetCell[c=4]{wd=\WFour} https://google.com & & & \\
\end{longtblr}

\noindent\rule{\linewidth}{1pt}

\end{document}

在此处输入图片描述

答案2

这是基于longtable问题原始解决方案的另一种解决方案。

我使用包进行了垂直居中multirow。缺点是您必须手动指定旁边换行文本的行数,才能垂直居中条目。这无法自动完成。

为了获得换行文本条目的正确宽度,我使用了与longtblr解决方案中类似的计算方法。

\documentclass{article}

% Packages
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{plex-sans}
\usepackage{longtable}
\usepackage{multirow}

\renewcommand{\familydefault}{\sfdefault}

\title{Table test}
\author{Me}
\date{\today}

\begin{document}

\maketitle

% Calculate the various widths. Take into account the \tabcolseps and the
% vertical line widths.

\newlength\vlinewidth
\setlength\vlinewidth{0.4pt}

% We now define length parameters for the various column widths.

\newlength\WOne % column with "Description" etc.
\newlength\WOnea % column with "High"
\newlength\WTwo %  2 columns next to "High"
\newlength\WThree % 3 columns next to "Description"
\newlength\WFour % 4 columns total width
                 
\settowidth\WOne{\textbf{Description}} % This is the longest of the three Description/Severity/Perimeter
\settowidth\WOnea{\textbf{Internal}} % This is the longest of the two High/Internal
% NOTE: If you want to let LaTeX calculate the longest one, you can use
% something like:
% \settowidth\WOne{%
%   \setlength\tabcolsep{0pt}%
%   \begin{tabular}{l}
%     \textbf{Description}\\
%     \textbf{Severity}\\
%     \textbf{Perimeter}
%   \end{tabular}%
% }

\setlength\WFour{\dimexpr\linewidth-2\tabcolsep-2\vlinewidth}
\setlength\WTwo{\dimexpr\linewidth-\WOne-\WOnea-6\tabcolsep-4\vlinewidth}
\setlength\WThree{\dimexpr\linewidth-\WOne-4\tabcolsep-3\vlinewidth}

\begin{longtable}{|l|l|l|p{\dimexpr 0.25\linewidth-2\tabcolsep}|}    
    \hline
    \multicolumn{4}{|p{\WFour}|}{\cellcolor[HTML]{E7E6E6}\textbf{Resume}} \\ 
    \hline
    \multicolumn{1}{|l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \multirow{3}*{\textbf{Description}}}} & \multicolumn{3}{p{\WThree}|}{Description here. This text is longer and should automatically wrap to the next line, ensuring that the content does not extend beyond the cell boundaries.} \\ \hline
    \endfirsthead
    %
    \endhead
    %
\multicolumn{1}{|l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \multirow{6}*{\textbf{Severity}}}} & \multicolumn{1}{c|}{\cellcolor[HTML]{C00000}{\color[HTML]{FFFFFF} \multirow{6}*{\textbf{High}}}} & \multicolumn{2}{p{\WTwo}|}{Severity description here. This text needs to be detailed enough to involve word wrapping within the cell. We make this text a bit longer to show that, since we get 6 lines of text now, the multirows on the left must also specify 6 in their first argument.} \\ \hline
    \multicolumn{1}{|l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \textbf{Perimeter}}} & \multicolumn{1}{l|}{\textbf{Internal}} & \multicolumn{1}{l|}{\cellcolor[HTML]{808080}{\color[HTML]{FFFFFF} \textbf{Authentication}}} & \textbf{No} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{\cellcolor[HTML]{E7E6E6}\textbf{TECHNICAL DETAILS}} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{Technical details here. This field must support a significant amount of text and must allow detailed discussion of the technical aspects of the topic in question.} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{\cellcolor[HTML]{E7E6E6}\textbf{RECOMMENDATION}} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{Recommendation here. Recommendations should be detailed, providing clear and concise instructions to mitigate identified risks or issues.} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{\cellcolor[HTML]{E7E6E6}\textbf{AFFECTED SERVICES}} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{https://google.com} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{https://google.com} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{\cellcolor[HTML]{E7E6E6}\textbf{REFERENCES}} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{https://google.com} \\ \hline
    \multicolumn{4}{|p{\WFour}|}{https://google.com} \\ \hline
    \caption{Vulnerability sheet}
    \label{tab:Vulnerability sheet}\\
\end{longtable}

\end{document}

在此处输入图片描述

相关内容