垂直居中单元格并固定高度?

垂直居中单元格并固定高度?

考虑解决方案https://tex.stackexchange.com/a/12762/4011解决表格中单元格高度固定的问题。这实际上会在单元格内容上方添加一些空间,因此不能保证它始终垂直居中。仅将 p-cell 更改为 m-cell 并不能解决这个问题。那么我该如何修改它以确保每个单元格内容都垂直居中?

\documentclass[a4paper]{article}
\usepackage{array}  

\begin{document} 

\newcolumntype{C}[1]{%
 >{\vbox to 5ex\bgroup\vfill\centering}%
 p{#1}%
 <{\egroup}}  

\begin{tabular}{|c | C{3cm} | C{5cm} |} 
 \hline 
 Short Text & Short Text & Short Text \tabularnewline \hline
Short Text  & Short Text & Loooooooooo  oooooooooooong Text \tabularnewline \hline  
Short Text  & Short Text & Loooooooooooooooooooooong Text \tabularnewline \hline 
\end{tabular}    
\end{document}

答案1

您可以\vfill在 之前添加\egroup,但是您会发现结果太高,大约\baselineskip,因此您也应该备份这个数量:

示例输出

\documentclass[a4paper]{article}
\usepackage{array}  

\begin{document} 

\showoutput

\newcolumntype{C}[1]{%
 >{\vbox to 5ex\bgroup\vfill\centering\arraybackslash}%
 p{#1}%
 <{\vskip-\baselineskip\vfill\egroup}}  

 \begin{tabular}{|c | C{3cm} | C{5cm} |}
   \hline 
   Short Text & Short Text & Short Text \\ \hline
   Short Text  & Short Text & Loooooooooo  oooooooooooong Text \\ \hline  
   Short Text  & Short Text & Loooooooooooooooooooooong Text \\ \hline
   Short Text  & Several pieces of text to fill up this box & Not much
   \\ \hline   
 \end{tabular}    
\end{document}

相关内容