3

This question follows up a previous question: Vertically centering subtable in cells of a main longtable.

I am using the code:

\documentclass{article}
\usepackage{longtable}
\usepackage{array}
\usepackage{xparse}
\usepackage{lipsum}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}

\NewDocumentCommand{\TB}{O{c} m O{c}}{%
    \begin{tabular}[#1]{@{}#3@{}}#2\end{tabular}
}

\renewcommand*{\arraystretch}{1.15}

\begin{document}
    \begin{longtable}{|L{6cm}|C{2cm}|C{2cm}|}
        \caption{Your table caption} \label{tab:my_label} \\
        \hline
        header 1  & header 2    & header 3 \\
        \hline
        \endhead
        \lipsum[1]      & \TB{a1\\a2} & \TB{b1\\b2\\b3} \\
        \hline
        row2      & a3          & \TB[t]{b4\\b5} \\
        \hline
        row3      & a4          & \TB[b]{b6\\b7} \\
        \hline
    \end{longtable}
\end{document}

Unfortunately, the cells of the first row are not being vertically centered:

enter image description here

Could you please help me? Thanks!

2 Answers 2

4

I suggest you switch the underlying column type of the L, C, and R column types from p to m. (m is mnemonic for "middle", I suppose.)

enter image description here

\documentclass{article}
\usepackage{longtable,array,xparse,lipsum}

\newcolumntype{L}[1]{>{\raggedright\arraybackslash}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}m{#1}}

\NewDocumentCommand{\TB}{O{c} m O{c}}{%
    \begin{tabular}[#1]{@{}#3@{}}#2\end{tabular}
}

\renewcommand*{\arraystretch}{1.15}

\begin{document}
    \begin{longtable}{|L{6cm}|C{2cm}|C{2cm}|}
        \caption{Your table caption} \label{tab:my_label} \\
        \hline
        header 1  & header 2    & header 3 \\
        \hline
        \endhead
        
        \lipsum[1][1-8] & \TB{a1\\a2} & \TB{b1\\b2\\b3} \\
        \hline
    \end{longtable}
\end{document}
1
  • 1
    Thanks a lot, @Mico: that fixes the problem!
    – PaulS
    Sep 8 at 18:27
3

Like this?

enter image description here

(red lines indicate page layout)

By use longtblr of tabularray package, code for MWE is simple and concise:

\documentclass{article}
%--------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%
\usepackage{lipsum}                             % for dummy text
%---------------------------------------------------------------%
\usepackage{ragged2e}
\usepackage{tabularray}
\SetTblrStyle{contfoot}{font=\small\itshape}

\begin{document}
    \begin{longtblr}[
caption = {Your table caption},
  label = {tab:my_label}
                    ]{hlines, vlines,
                      colspec = {X[m, cmd={\RaggedRight\hspace{0pt}}] *{2}{Q[c, m]}},
                      rowhead = 1
                      }
header 1    &    header 2   &   header 3        \\
% table body
\lipsum[1]  &   {a1\\a2}    &   {b1\\b2\\b3}    \\
row2        &   a3          &   {b4\\b5}        \\
row3        &   a4          &   {b6\\b7}        \\
    \end{longtblr}
\end{document}

Edit:

  • As you can see, I took a liberty and a little bit redesign your table that according to my opinion make table more pretied:
    • last two columns has natural width,
    • width of the first column occupy the rest widths of the \textwidth.
  • Contents in cells are vertical centered by use of the m option in the X and Q columns specifications.
  • For more details about tabularray package you should consult package documentation which contain concise explanation with a plenty examples. For your problem may be interesting the following:
  • beside standard tabular aligning c (horizontal center), l, and r introduce f (at foot of cell), h (at top of cell) and m (at middle, vertical centered), which can be also used as options in Q and X cells (later is known from tabularx package)
  • for manual breaking cells' contents is introduced simple syntax {<first line>\\<second line>\\ ... }
  • for multi column and multirow cells are defined SetCell[c=<number od columns>]{<aligning of contents} cell content and SetCell[r=<number od columns>]{<aligning of contents} cell content
  • Other useful features are:
  • automatic adding continue to long table captions on the next pages and `table footmark continue on next page
  • repeating of rows, which contains column headers, is determined by rowhead = <number> instruction in the table preamble.
3
  • 1
    For the benefit of readers who are not (yet) familiar with the tabularray machinery and syntax, could you point out which code particle(s) specifically assure the vertical centering of the cell/column contents?
    – Mico
    Sep 8 at 19:14
  • Thanks, @Zarco, for your solution. Currently, I am not familiar with tabularray, but I intend to study it. In particular, I do not currently understand your colorpsec.
    – PaulS
    Sep 8 at 20:13
  • 1
    @Mico, thank you very much for note. I would add some more explanation at late evening. Temporary I'm on the way (till to the late of afternoon).
    – Zarko
    Sep 9 at 1:34

You must log in to answer this question.

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