2
votes

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.

  • multirow: not working, because of the unknown number of rows generating a long text in a fixed width column.

  • baseline: not working, because all other columns should be top aligned.

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

1

1 Answers

0
votes

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?