1

I want to create the table below without having to manually calculate where row breaks fall or how many lines a row heading will occupy (and of course without the blinking green lines :-P).

Desired result

Let me first try to describe the table to you. It has

  1. a fixed table width set to \linewidth
  2. row headings (the first column) that span two rows of the table body
  3. automatic row wrapping of too-long row headings
  4. hanging indent for the wrapped row headings
  5. increased row spacing in the table body
  6. line spacing of the row headings to match the increased row spacing in the table body

My best attempt

Best attempt

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

My best attempt got me

  • (1) and (3) using tabularx and its X column
  • (4) using \raggedright\hangindent=1em
  • (5) using \renewcommand{\arraystretch}{2}
  • (6) using \setlength{\baselineskip}{2\baselineskip}

but (2) still doesn't work. As can be seen, the row height of the final row is not adjusted to account for the row heading continuing another line.

The manual solution would be to add an extra line before the \bottomrule and specifying that the final row heading runs for 3 lines. But that would require me to calculate how many lines a row heading will occupy, which is exactly what I don't want to do.

N.B. This is a new attempt at an earlier question that was closed for lack of clarity. From Zarko's answer to that question I get the impression that perhaps tabularray could deal with this sort of thing.

3 Answers 3

3

You can create this table only at TeX primitive level:

\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 
}
1
  • Sadly, this is beyond me. I wouldn’t know how to adapt it to anything that I could regularly use.
    – Fredrik P
    Sep 27, 2023 at 18:49
2

just slightly modified my answer from addendum to you previous (now deleted) question:

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

Added option font=\linespread{1.25}\selectfont is based on trial and errors.

enter image description here

I expect that appearing of the last row of table will not fulfill your expectation.

BTW, to my opinion, your table design effort doesn't lead to nice looking table. Even worse, tables becomes a bit unclear. I would rather stick to my code in addendum on your previous, now deleted question .

But all is up to you. Hopefully someone else will better understand your question and provide what you after ...

1
  • Thanks! Although I'm not particularly fond of magic numbers that set me on the right path. As for the appearance of the table, I'm trying to conform with CMOS 3.70, which says "if both the stub and one or more cells contain more than one line, the first lines are aligned throughout the body of the table."
    – Fredrik P
    Apr 19, 2023 at 9:46
0

Thanks to Zarko's answer, I was able to string something together.

The important parts are setting the table's stretch to 2 as well as its abovesep and belowsep to 0 and the X column's font to \setlength{\baselineskip}{2\baselineskip}.

And then the row stuff is just trying to make the table look as close as possible to the goal set out by the question.

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

Output

You must log in to answer this question.

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