1

I'm trying to develop a bit complex LaTeX longtable

I already achieved most of it but I wanted to know besides the help that I want if what I'm doing is the best way or if there isn't a simpler or a more modern way to this approach.

I have the following LaTeX table:

\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}

However, I want to center the "High" text inside the C00000 colored cell vertically, not only horizontally, but when I do that, the text wrapping breaks even more than it already is broken.

I want to have not only the "High" text centered vertically but also the "Description" and "Severity", this all without breaking the text inside "Description here" and "Severity description here", I want to fix all of this without making those columns flexible because that's the exact width of the cells "Description", "Severity", "High", "Perimeter", "Internal", "Authentication" and "No" I want.

TL;DR Center the "High" text vertically and fix the text-wrapping issue on the first 2 paragraphs without messing with the alignment of other elements in this table.

If anyone can help me on this I'll be really glad, I've been stuck at this for quit a long time, thank you!

An image with the current table is present in the attachments alongside with red arrows indicating what I want to be fixed.

Thanks! Note: I'm using XeLaTeX.

The alignments I wanted to fix

1
  • 1
    You should have a look at the tabularray package. It is much better suited for this kind of complicated tables. Commented Mar 14 at 18:03

2 Answers 2

2

NOTE: Improved solution based on the remarks of the OP below.

Here is a solution with the package 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}

enter image description here

2. Improved solution where the columns with "Description", "Severity", "Perimeter", "High" and "Internal" take on their natural width. This implies that we will have to calculate the widths of the columns ehre the text has to wrap on line breaks.

To demonstrate that the column width have been correctly calculated, I use justifying the text in these cells and I put a horizontal rule with the calculated width as its length.

\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}

enter image description here

3
  • Awesome! Thank you so much for this solution, seems to be the best one overall for what I want. However, I see that the cells where "Description", "Severity", "Perimeter", and "High" are, have their width extending dynamically on the document size, but I wanted to have a fixed width on them, when I try to do that by setting colspec to a limited size, the text-wrapping breaks on cells. How can I achieve that, setting a fixed size to these cells (e.g. setting them to the text width of the texts inside them) and not breaking the text-wrapping on the rest of the cells? Thanks in advance!
    – biscoito
    Commented Mar 15 at 0:59
  • I added an improved solution. Commented Mar 15 at 13:42
  • I don't have words of how I am grateful for this solution, your response, and your help, you've been of extreme help to me with this problem, I've been struggling a lot with it, thank you so much for helping me. It warms my heart to see that there are people like this in the world. Thank you so much!
    – biscoito
    Commented Mar 15 at 15:08
1

Here is another solution based on the original longtable solution in the question.

I did the vertical centering with the multirow package. The disadvantage is that you must manually specify the number of lines that the wrapped text next to it has, in order to vertically center an entry. This cannot be automated.

And to get the proper widths of the wrapped text entries I use similar calculations as in the longtblr solution.

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .