【问题标题】:Latex table: Vertically center text in one column, other columns top aligned with fixed width乳胶表:在一列中垂直居中文本,其他列顶部对齐固定宽度
【发布时间】:2019-12-16 21:57:43
【问题描述】:

我尝试将乳胶表的第一列中的文本垂直居中,而其他列的文本长度未知。其他列应顶部对齐。

我已经尝试过使用 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}

我希望文本“居中”在该行中垂直居中。

【问题讨论】:

    标签: latex


    【解决方案1】:

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

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

    那么,有人知道更简单直接的解决方案吗?

    【讨论】:

      【解决方案2】:

      nearly identical question 在 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}
      
      

      【讨论】:

        【解决方案3】:

        使用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}
        

        【讨论】: