自动扩展多行单元格第二行的高度以适合内容

自动扩展多行单元格第二行的高度以适合内容

我想创建下面的表格,而不必手动计算行分隔符的位置或行标题将占用多少行(当然没有闪烁的绿线:-P)。

期望结果

首先,让我试着向你描述一下这张表。它有

  1. 固定表格宽度设置为\linewidth
  2. 横跨表体两行的行标题(第一列)
  3. 自动换行太长的行标题
  4. 换行标题的悬挂缩进
  5. 增加表体行距
  6. 行标题的行距与表体中增加的行距相匹配

我最好的尝试

最佳尝试

\documentclass{article}
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
  \begin{table}[ht]
    \renewcommand{\arraystretch}{2}
    \renewcommand{\multirowsetup}{\raggedright\hangindent=1em\setlength{\baselineskip}{2\baselineskip}}
    \begin{tabularx}{\linewidth}{@{} X *{4}{c} @{}}
      \toprule
                                                                                                                       &   A   &   B   &   C   &   D   \\
      \midrule
      \multirow[t]{2}{=}{The first row heading runs over one line}                                                     &  123  &  123  &  123  &  123  \\
                                                                                                                       & (456) & (456) & (456) & (456) \\
      \multirow[t]{2}{=}{The second row heading is slightly longer and runs over two lines}                            &  123  &  123  &  123  &  123  \\
                                                                                                                       & (456) & (456) & (456) & (456) \\
      \multirow[t]{2}{=}{The third row heading is really quite outrageously long and runs over a total of three lines} &  123  &  123  &  123  &  123  \\
                                                                                                                       & (456) & (456) & (456) & (456) \\
      \bottomrule
    \end{tabularx}
  \end{table}
\end{document}

我尽了最大努力

  • (1)和(3)利用tabularx及其X
  • (4)使用\raggedright\hangindent=1em
  • (5)使用\renewcommand{\arraystretch}{2}
  • (6)使用\setlength{\baselineskip}{2\baselineskip}

但 (2) 仍然不起作用。可以看出,最后一行的行高没有调整以适应行标题延伸另一行。

手动解决方案是在之前添加一行\bottomrule,并指定最后一行标题占 3 行。但这需要我计算行标题将占用多少行,而这正是我不想做的。

NB:这是一次新的尝试先前的问题由于缺乏明确性,该组织关闭了Zarko 对此问题的回答我的印象是也许tabularray可以处理这类事情。

答案1

您只能在 TeX 原始级别创建此表:

\def\p #1#2#3#4#5#6#7#8#9{%
   \hbox to\hsize{%
      \vtop{\leftskip=5pt \parindent=-5pt \hsize=.55\hsize #1\par\kern-\prevdepth}%
      \hfil
      \vtop{\pp{#2}{#3}{#4}{#5}\pp{#6}{#7}{#8}{#9}\kern-\prevdepth}%
   }
   \prevdepth=0pt
}
\def\pp #1#2#3#4{\hbox to.4\hsize{\px{#1}\px{#2}\px{#3}\px{#4}}}
\def\px #1{\hfil\hbox to0pt{\hss#1\hss}\hfil}
\def\tr {\vskip.5\baselineskip \hrule \vskip.5\baselineskip}

\vbox{\baselineskip=20pt
   \tr
   \hfill \pp {A} {B} {C} {D}
   \tr
   \p {The first row heading runs over one line}
      {123} {123} {123} {123} {(456)} {(456)} {(456)} {(456)} 
   \p {The second row heading is slightly longer and runs over two lines}
      {123} {123} {123} {123} {(456)} {(456)} {(456)} {(456)}
   \p {The third row heading is really quite outrageously long and runs over a total of three lines}
      {123} {123} {123} {123} {(456)} {(456)} {(456)} {(456)}
   \tr 
}

答案2

只是稍微修改了我对你之前的问题(现已删除)的附录中的回答:

\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
  \begin{table}[ht]
    \begin{tblr}{colspec={@{}  X[l, h, cmd=\hangindent 1em\hangafter 1,
                                       font=\linespread{1.25}\selectfont]
                          *{4}{Q[c, h]} @{}},
                 cell{odd}{1} = {r=2}{},
                 row{odd[3]}  = {abovesep=2ex},
                }
    \toprule
The first row heading runs over one line
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
    &  123  &  123  &  123  &  123  \\
    & (456) & (456) & (456) & (456) \\
    \bottomrule
    \end{tblr}
  \end{table}
\end{document}  

添加的选项font=\linespread{1.25}\selectfont基于反复试验。

在此处输入图片描述

我预计表格最后一行的出现不会满足你的期望。

顺便说一句,在我看来,你的表格设计工作并没有带来好看的表格。更糟糕的是,表格变得有点不清楚。我宁愿坚持使用你之前(现已删除)问题的附录中的代码。

但一切都取决于你。希望其他人能更好地理解你的问题并提供你所需要的内容...

答案3

谢谢Zarko 的回答,我就能把一些东西串起来了。

重要的部分是设置表的stretch2以及它的abovesepbelowsep0Xfont\setlength{\baselineskip}{2\baselineskip}

然后row所做的只是试图使表格看起来尽可能接近问题所设定的目标。

\documentclass{article}
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
  \begin{table}[ht]
    \begin{tblr}{
      colspec={
        @{}
        X[
          l,
          h,
          cmd={\hangindent=1em\hangafter=1},
          font={\setlength{\baselineskip}{2\baselineskip}},
        ]
        *{4}{Q[c, h]}
        @{}
      },
      stretch=2,
      cell{even}{1} = {r=2}{},
      rows = {abovesep=0pt, belowsep=0pt},
      row{1} = {abovesep=3pt, belowsep=1.5pt},  % magic numbers
      row{2} = {abovesep=3pt},  % magic number
      row{Z} = {belowsep=1.5pt},  % magic number
    }
      \toprule
      & A & B & C & D \\
      \midrule
  The first row heading runs over one line
      &  123  &  123  &  123  &  123  \\
      & (456) & (456) & (456) & (456) \\
  The second row heading is slightly longer and runs over two lines
      &  123  &  123  &  123  &  123  \\
      & (456) & (456) & (456) & (456) \\
  The third row heading is really quite outrageously long and runs over a total of three lines
      &  123  &  123  &  123  &  123  \\
      & (456) & (456) & (456) & (456) \\
      \bottomrule
    \end{tblr}
  \end{table}
\end{document}

输出

相关内容