thinkingeye
thinkingeye

Reputation: 234

Latex table: Vertically center text in one column, other columns top aligned with fixed width

I try to vertically center a text in the first column of a latex table with unknown length of the text of other columns. The other columns should be top aligned.

I already tried with tabular, tabularx, tabu table environment. All the approaches I have found in the internet for vertically center something are using the baseline or some kind of multirow environment.

\documentclass{article}
\begin{document}

\begin{tabular} {| p{2cm} p{2cm} p{2cm} |}
    \hline
    centered & This is a long top aligned text, dynamically length. &  This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment. \\
    \hline
\end{tabular}

\end{document}

I want that the text "centered" is vertically centered in this row.

Upvotes: 3

Views: 10196

Answers (3)

With the tabularray package it is easy to set the alignment for each column separately:

\documentclass{article}

\usepackage{tabularray}

\begin{document}

\begin{tblr}{| Q[2cm,valign=m] Q[2cm,valign=h] Q[2cm,valign=h] |}
    \hline
    centered & This is a long top aligned text, dynamically length. &  This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment. \\
    \hline
\end{tblr}

\end{document}

enter image description here

Upvotes: 0

MOzSalles
MOzSalles

Reputation: 205

A nearly identical question has several answers in TexStackExchange.

Also: https://www.latex-tables.com/ suggests using vcell for this kind of thing

\documentclass{article}
\usepackage{vcell}

\begin{document}

\begin{table}
\centering
\begin{tabular}{| p{2cm} p{2cm} p{2cm} |} 
\hline
\vcell{centered}
& \vcell{This is a long top aligned text, dynamically length.}
& \vcell{This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment.}  \\
[-\rowheight]
\printcellmiddle
& \printcelltop
& \printcelltop                                                                \\
\hline
\end{tabular}
\end{table}

\end{document}

Upvotes: 1

thinkingeye
thinkingeye

Reputation: 234

Meanwhile I came around with a workaround. I call it workaround because I think there should exist a simpler solution to this "easy" problem.

\documentclass{article}

\usepackage{adjustbox}
\usepackage{calc}
\usepackage{makecell}

\begin{document}

\newdimen\widthcola
\setlength{\widthcola}{2cm}

\newdimen\widthcolb
\setlength{\widthcolb}{2cm}

\newdimen\widthcolc
\setlength{\widthcolc}{2cm}

\newcommand{\tabline}[3]{
    \newdimen\heightcella
    \setlength{\heightcella}{\totalheightof{\makecell[t{p{\widthcola}}]{#1}}}

    \newdimen\heightcellb
    \setlength{\heightcellb}{\totalheightof{\makecell[t{p{\widthcolb}}]{#2}}}

    \newdimen\heightcellc
    \setlength{\heightcellc}{\totalheightof{\makecell[t{p{\widthcolc}}]{#3}}}

    \newdimen\heightcellmax
    \setlength{\heightcellmax}{\heightcella}
    \setlength{\heightcellmax}{\maxof{\heightcellmax}{\heightcellb}}
    \setlength{\heightcellmax}{\maxof{\heightcellmax}{\heightcellc}}

    \adjustbox{padding=0mm, margin=0mm, raise=-0.5\heightcellmax+0.5\heightcella}{\makecell[t{p{\widthcolc}}]{#1}} & \makecell[t{p{\widthcolc}}]{#2} & \makecell[t{p{\widthcolc}}]{#3} \\
}

\begin{tabular} {| p{\widthcola} p{\widthcolb} p{\widthcolc} |}
    \hline
    \tabline{centered}{This is a long top aligned text, dynamically length.}{This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environemnt.}
    \hline
\end{tabular}

\end{document}

So, does anybody know a simpler and more straightforward solution?

Upvotes: 0

Related Questions