我一直在寻找一种解决方案,使我能够垂直居中对齐multirow
跨越两个中间行的行,由于表格环境,这些行可能会跨越多行tabularx
。
其他一些尚未得到解答的问题包括这里,这里, 和这里。这些解决方案使用了我真的不想使用的fixup
参数,或者它们不太适用于我的情况。multirow
举个例子
\documentclass{article}
\usepackage{tabularx, multirow, booktabs, lipsum}
\begin{document}
\begin{table}[ht]
\begin{tabularx}{\textwidth}{XXX}
\toprule
a & the first very, very, very, very, very, very, very, very, very, very, very long line. & a \\
\multirow{2}{*}{b} & a very, very, very, very, very, very, very, very, very, very, very long line. & \multirow{2}{*}{b} \\
& another very, very, very, very, very, very, very, very, very, very, very long line. & \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
我希望“b”垂直居中对齐。我与 没有任何关系multirow
,但我与 有关系,tabularx
并且非常希望有一个自动解决方案。
答案1
这是使用 pdftex 原语,但它或类似物在其他引擎中可用。它需要运行几次才能稳定下来,并假设跨行大于放置在跨度中的条目(如果文本\centerbetween
太大,它将在没有任何警告的情况下覆盖周围的文本)。\advance\dimen@\ht\strutbox
代码中的因素可能需要调整以适应您的视觉垂直居中。如果没有它,它会将文本框的中间置于跨行的顶部和底部_baseline_之间,这通常不是您想要的。
\documentclass{article}
\usepackage{tabularx, multirow, booktabs, lipsum}
\begin{document}
\makeatletter
\def\savepos#1{\pdfsavepos\write\@auxout{%
\gdef\expandafter\string\csname savepos@#1@x\endcsname{\the\pdflastxpos}^^J%
\gdef\expandafter\string\csname savepos@#1@y\endcsname{\the\pdflastypos}}}
\def\centerbetween#1#2#3{%
\expandafter\ifx\csname savepos@#1@x\endcsname\relax
\dimen@\z@
\else
\dimen@\csname savepos@#2@y\endcsname sp %
\advance\dimen@-\csname savepos@#1@y\endcsname sp %
\advance\dimen@\ht\strutbox
\fi
\smash{\raisebox{\dimen@}{\parbox[c]{\hsize}{#3}}}}
\makeatother
\begin{table}[ht]
\begin{tabularx}{\textwidth}{XXX}
\toprule
a & the first very, very, very, very, very, very, very, very, very, very, very long line. & a \\
\strut\savepos{a}%
\centerbetween{a}{b}{bbb}
& a very, very, very, very, very, very, very, very, very, very, very long line. &
\centerbetween{a}{b}{ccc} \\
\vfill\strut\savepos{b}
& another very, very, very, very, very, very, very, very, very, very, very long line. & \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}