Latex表格:一个列中的文本垂直居中,其他列固定宽度并顶部对齐

3
我试图在一个LaTeX表格的第一列中垂直居中文本,而其他列的文本长度是未知的。其他列应该顶部对齐。 我已经尝试过tabular、tabularx和tabu表格环境。我在互联网上找到的所有垂直居中某些内容的方法都使用基线或某种多行环境。
- 多行:不起作用,因为不知道生成固定宽度列中长文本的行数。 - 基线:不起作用,因为所有其他列应该顶部对齐。
\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}

我希望这一行的文字 "centered" 在垂直方向上居中。
3个回答

1
一段英文文本的翻译:

在TexStackExchange上有一个几乎相同的问题有几个答案。

此外:https://www.latex-tables.com/ 建议使用vcell来处理这种情况。

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


0

使用tabularray包可以轻松地为每个列设置对齐方式:

\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


0

与此同时,我想到了一个解决方法。我称之为“解决方法”,因为我认为应该存在一种更简单的解决方案来解决这个“简单”的问题。

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

所以,有人知道一个更简单和直接的解决方案吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接