将多列多行的长表设置为页面宽度

将多列多行的长表设置为页面宽度

我有一张长表(见片段),其结构如旁边的图像所示(该表实际上要长得多,但它只是一遍又一遍相同的结构,混合了multicolumns 和multirows)。

\documentclass{article}
\usepackage[usenames]{color}% Para poder poner colores al texto :)
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{longtable}
\usepackage{float}
\begin{document}
\begin{longtable}[c]{|ll|ll|}
\hline
\rowcolor[HTML]{9B9B9B} 
\multicolumn{2}{|c|}{\cellcolor[HTML]{9B9B9B}\textbf{SQuaRE Norm}} &
  \multicolumn{2}{c|}{\cellcolor[HTML]{9B9B9B}\textbf{XXXXXXXXXXXXX}} \\ \hline
\endhead
%
\rowcolor[HTML]{C0C0C0} 
\multicolumn{1}{|c|}{\cellcolor[HTML]{C0C0C0}\textbf{Characteristic}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{C0C0C0}\textbf{Sub-characteristic}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{C0C0C0}\textbf{Proposed metric}} &
  \multicolumn{1}{c|}{\cellcolor[HTML]{C0C0C0}\textbf{Proposed sub-characteristic}} \\ \hline
\multicolumn{1}{|l|}{} &
   &
  \multicolumn{1}{l|}{XXXXX XXXXX XXXXXXXXXXXXXX} &
  XXXXXXXXXXXXXX XXXXXXXXXXXXX \\ \cline{3-4} 
\multicolumn{1}{|l|}{} &
  \multirow{-2}{*}{XXXXXXXXX} &
  \multicolumn{1}{l|}{XXXX XXXXXXXXXXX} &
  XXXXXX XXXXXXXX XXXXXXXXXXX \\ \cline{2-4} 
\multicolumn{1}{|l|}{} &
   &
  \multicolumn{1}{l|}{XXXXXXX XXXXXXXXXXX} &
  XXXXXXXXXX XXXXXXXXXXXXXXXXX \\ \cline{3-4} 
\multicolumn{1}{|l|}{} &
  \multirow{-2}{*}{XXXX} &
  \multicolumn{1}{l|}{XXX XX XXXXXXXX XXXXXXX} &
  XXXXXXX XXXXXXXXX \\ \cline{2-4} 
\multicolumn{1}{|l|}{\multirow{-5}{*}{XXXXXXX XXXXXXXXXX}} &
  XXXXXXXXX &
    \multicolumn{1}{l|}{-} &
  - \\ \hline

\end{longtable}

\end{document}

在此处输入图片描述

我希望它适合页面宽度而不超出页面宽度,但我尝试了互联网上看到的多种解决方案,但都没有成功。我使用这个生成了这个表编辑,如果它有任何意义的话。我也尝试过诸如tabularx和之类的东西tabu,但这通常会让我得到如下结果。

在此处输入图片描述

答案1

  • 全部删除\multicolumn{1}{...}{...},它仅覆盖表序言中的列规范……
  • 两种解决方法:
    • 使用xltabular
    • 使用tabularray包(我更喜欢)
\documentclass{article}
\usepackage{geometry}
\usepackage[table,xcdraw]{xcolor}
\usepackage{longtable, multirow, xltabular}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}

\usepackage{tabularray}

\begin{document}
\begingroup
    \small
    \setlength\tabcolsep{3pt}
\begin{xltabular}{\linewidth}{  |>{\hsize=0.5\hsize}L
                                |>{\hsize=0.5\hsize}L
                                |L | L| }
\caption{My caption, used \texttt{xltabular}.}
\label{my-label}    \\
    \hline
\rowcolor[HTML]{9B9B9B}
\multicolumn{2}{|c|}{\cellcolor[HTML]{9B9B9B}\textbf{SQuaRE Norm}} 
        & \multicolumn{2}{c|}{\cellcolor[HTML]{9B9B9B}\textbf{XXXXXXXXXXXXX}} \\ \hline
\endhead
%
\rowcolor[HTML]{C0C0C0}
\textbf{Characteristic}     
    & \textbf{Sub-characteristic}
        & \textbf{Proposed metric} 
            & \textbf{Proposed sub-characteristic}  \\ 
    \hline
    &   & XXXXX XXXXX XXXXXXXXXXXXXX 
            & XXXXXXXXXXXXXX XXXXXXXXXXXXX          \\ 
   \cline{3-4}
    & \multirow{-2}{=}{XXXXXXXXX} 
        & XXXX XXXXXXXXXXX
            & XXXXXX XXXXXXXX XXXXXXXXXXX           \\ 
    \cline{2-4}
    &   & XXXXXXX XXXXXXXXXXX
            & XXXXXXXXXX XXXXXXXXXXXXXXXXX          \\ 
    \cline{3-4}
    & \multirow{-2}{=}{XXXX} 
        & XXX XX XXXXXXXX XXXXXXX
            & XXXXXXX XXXXXXXXX                     \\ 
    \cline{2-4}
\multirow{-9}{=}{XXXXXXX XXXXXXXXXX}
    & XXXXXXXXX 
        & --    & --                                \\ 
    \hline
\end{xltabular}

%%%%%%%%
\begin{longtblr}[
caption = {My caption, used \texttt{longtblr}.},
  label = {my-label},
                ]{hlines, vlines,
                 colspec = {*{2}{X[l]} *{2}{X[2,l]} },
                 row{1}  = {font=\bfseries, c, bg=black!60},
                 row{2}  = {font=\bfseries, c, m, bg=black!30},
                 rowhead=1
                 }
\SetCell[c=2]{c}    SQuaRE Norm
    &   &   \SetCell[c=2]{c}    XXXXXXXXXXXXX 
            &                                       \\
Characteristic
    & Sub-characteristic 
        & Proposed metric 
            & Proposed sub-characteristic           \\
\SetCell[r=5]{c}    XXXXXXX XXXXXXXXXX 
    &   \SetCell[r=2]{c}    XXXXXXXXX  
        & XXXXX XXXXX XXXXXXXXXXXXXX
            & XXXXXXXXXXXXXX XXXXXXXXXXXXX          \\
    &   & XXXX XXXXXXXXXXX
            & XXXXXX XXXXXXXX XXXXXXXXXXX           \\
    &   \SetCell[r=2]{c}    XXXX 
       & XXXXXXX XXXXXXXXXXX
            & XXXXXXXXXX XXXXXXXXXXXXXXXXX          \\
    &   & XXX XX XXXXXXXX XXXXXXX
            & XXXXXXX XXXXXXXXX                     \\
    & XXXXXXXXX
        & --    & --                                \\
\end{longtblr}

\end{document}

在此处输入图片描述

第一个表需要两到三个编译,第二个表只需要一个。

附录:
在评论中,您要求更改表格格式。要求并不完全清楚,所以我猜您对第二个表格感兴趣,比如减小baseline单元格内容和边框之间的大小和垂直距离:

\documentclass{article}
\usepackage{geometry}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}

\begin{document}
    \begingroup
\small\linespread{0.84}\selectfont
\begin{longtblr}[
caption = {My caption, used \texttt{longtblr}.},
  label = {my-label},
                ]{hlines, vlines,
                 colspec = {*{2}{X[l]} *{2}{X[2,l]} },
                 row{1}  = {font=\bfseries, c, bg=black!60},
                 row{2}  = {font=\bfseries, c, m, bg=black!30},
                 rowsep = 1pt,                 
                 rowhead=1
                 }
\SetCell[c=2]{c}    SQuaRE Norm
    &   &   \SetCell[c=2]{c}    XXXXXXXXXXXXX
            &                                       \\
Characteristic
    & Sub-characteristic
        & Proposed metric
            & Proposed sub-characteristic           \\
\SetCell[r=5]{c}    XXXXXXX XXXXXXXXXX
    &   \SetCell[r=2]{c}    XXXXXXXXX
        & XXXXX XXXXX XXXXXXXXXXXXXX
            & XXXXXXXXXXXXXX XXXXXXXXXXXXX          \\
    &   & XXXX XXXXXXXXXXX
            & XXXXXX XXXXXXXX XXXXXXXXXXX           \\
    &   \SetCell[r=2]{c}    XXXX
       & XXXXXXX XXXXXXXXXXX
            & XXXXXXXXXX XXXXXXXXXXXXXXXXX          \\
    &   & XXX XX XXXXXXXX XXXXXXX
            & XXXXXXX XXXXXXXXX                     \\
    & XXXXXXXXX
        & --    & --                                \\
\end{longtblr}
    \endgroup
\end{document}

在我看来,这张表不太好看:

在此处输入图片描述

相关内容