2

This question is a follow up of previous questions: Problem with vertical centering of cells of a same row of a longtable and Vertically centering subtable in cells of a main longtable.

I am using the following code:

\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}|L{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{some text, \\ some text\\some text, \\ some text\\a2} & \TB{b1\\b2\\b3} \\
        \hline
    \end{longtable}
\end{document}

However, the vertical centering is not being produced:

enter image description here

Could you please help me? Thanks!

3
  • 1
    Try replacing \begin{tabular}[#1]{@{}#3@{}}#2\end{tabular} with \smash{\begin{tabular}[#1]{@{}#3@{}}#2\end{tabular}}. Not sure why it works...
    – Mico
    Sep 9, 2023 at 12:44
  • 1
    If you change L, C and R column types to p{} and then enclose \lipsum[1][1-8] within \TB, that is \TB{\lipsum[1][1-8]}[p{\linewidth}], you will get the perfect middle alignment. For some reason, sometimes mixing multilined cells, with nested tabulars makes m{} incorrect to determine middle points.
    – Celdor
    Sep 9, 2023 at 13:13
  • Thanks, @Mico, for your idea. While it fixes the issue in the example I gave you, unfortunately, when one uses \TB to have a multine header, the header overflows the row of the header.
    – PaulS
    Sep 9, 2023 at 14:19

1 Answer 1

3

Expanding on my comment the following code works:

\documentclass{article}
\usepackage{longtable,array,xparse,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}|L{2cm}|C{2cm}|}
  \caption{Your table caption} \label{tab:my_label} \\
  \hline
  header 1  & header 2    & header 3 \\
  \hline
  \endhead
  
  \TB{\lipsum[1][1-8]}[p{\linewidth}]
            & \TB{some text, \\ some text\\some text, \\ some text\\a2}
            & \TB{b1\\b2\\b3} \\
  \hline
\end{longtable}
\end{document}

enter image description here

IMO the original code should also work, although I also get incorrect results when I sometimes mix cells with nested tabulars. Consider two packages makecell or tabularray, which likely make your life easier in these scenarios.

As to the example, it has something to do with determining correct baselines but I don't want to go into details as I am not entirely sure what happens. The code above works I think because from a parent cell perspective, there is only the middle baseline established by nested tabulars, middle because of the effect of [c]. Then, p{} lines up all three cells w.r.t baselines and hence you get the perfect middle alignment.

2
  • Thanks, @Celdor: Your approach works perfectly!
    – PaulS
    Sep 9, 2023 at 15:03
  • 1
    Np. I am glad it's working for you.
    – Celdor
    Sep 9, 2023 at 17:25

You must log in to answer this question.

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