尽管关于表格单元格垂直居中的答案有几十个,但我无法(我的错)找到一个看似简单的需求的解决方案,即在同一行手工包裹的多行单元格中垂直居中单元格。
\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{array}
\begin{document}
\pagecolor{yellow!10}
\newcommand{\methoda}[1]{{\parbox[c]{\linewidth}{\centering#1}}}
\newcommand{\methodb}[3]{\begin{tabular}[c]{@{}c@{}}#1\\#2\\#3\end{tabular}}
\newcommand{\methodc}[1]{\begin{minipage}[m]{\linewidth}\centering#1\end{minipage}}
\newcommand{\methodd}[1]{{\begin{center}#1\end{center}}}
\newcommand{\methoddfix}[1]{{\setlength{\topsep}{0pt}\begin{center}#1\end{center}}}
\centering\begin{tabular}{>{\raggedright\arraybackslash}m{0.28\linewidth}>{\centering\arraybackslash}m{0.33\linewidth}}
\tabularnewline\hline
\centering Column 1 & Column 2\tabularnewline\hline
2 lines vertically centered & This cell contains more than one line of automatically wrapped text. \tabularnewline\hline
Fail of vertical centering & \methoda{Hand-wrapped\\ multiline\\ method A} \tabularnewline\hline
Vert. center. fail & \methodb{Hand-wrapped}{multiline}{method B} \tabularnewline\hline
Fail of vertical centering & \methodc{Hand-wrapped multiline\\ method C\\ too much padding} \tabularnewline\hline
Vert. center. OK & \methodd{Hand-wrapped multiline\\ method D\\ too much padding} \tabularnewline\hline
Success column 1, fail column 2 & \methoddfix{Hand-wrapped multiline\\ method Dfix\\ cannot remove\\ bottom padding} \tabularnewline\hline
Just a last row & Just a last row \tabularnewline\hline
\end{tabular}
\end{document}
我的真实世界表是一个longtable
利用multirow
和可能tabularx
或其他一些额外的高级包,并用 XeLaTeX 编译。
在此先感谢您的帮助。
答案1
带有makecell
或嵌套的tabular
s 和p
类型第二列:
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{makecell}
\begin{document}
With a multicolumn around the automatically wrapped columns:
\centering\begin{tabular}{>{\raggedright\arraybackslash}m{0.28\linewidth}>{\centering\arraybackslash}p{0.33\linewidth}}
\hline
some automatically wrapped text & \makecell[cc]{Some \\ manually \\ linebroken \\ text \\ over multiple lines}\\ \hline
some automatically wrapped text & \begin{tabular}[c]{@{}c@{}}Some \\ manually \\ linebroken \\ text \\ over multiple lines\end{tabular}\\ \hline
some automatically wrapped text & \multicolumn{1}{>{\centering\arraybackslash}m{0.33\linewidth}}{some longer but also automatically wrapped text}\\ \hline
\end{tabular}
With a multicolumn around the manually wrapped columns:
\begin{tabular}{>{\raggedright\arraybackslash}m{0.28\linewidth}>{\centering\arraybackslash}m{0.33\linewidth}}
\hline
some automatically wrapped text & \multicolumn{1}{>{\centering\arraybackslash}p{0.33\linewidth}}{\makecell[cc]{Some \\ manually \\ linebroken \\ text \\ over multiple lines}}\\ \hline
some automatically wrapped text & \multicolumn{1}{>{\centering\arraybackslash}p{0.33\linewidth}}{\begin{tabular}[c]{@{}c@{}}Some \\ manually \\ linebroken \\ text \\ over multiple lines\end{tabular}}\\ \hline
some automatically wrapped text & some longer but also automatically wrapped text\\ \hline
\end{tabular}
\end{document}
更新:要结合手动和自动换行符,您可以将初始列定义更改为包含\let\newline\\\arraybackslash\hspace{0pt}
而不是仅仅包含\arraybackslash
。
\documentclass[12pt]{article}
\usepackage{array}
\usepackage{makecell}
\begin{document}
\begin{tabular}{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{0.28\linewidth}>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{0.33\linewidth}}
\hline
some automatically wrapped text & some longer but also automatically wrapped text\\ \hline
some automatically wrapped text & some\newline automatically wrapped text with a\newline few \newline manual linebreaks\\ \hline
manual \newline linebreak & more text here\\
\hline
\end{tabular}
\end{document}