我是 Latex 新手,在表格方面遇到了一些麻烦...我想将 A 列和 B 列垂直居中。我使用命令将所有单元格垂直居中。但是当我在主表(tabularx 环境)中包含另一个表格时,它会将单元格居中,而不是表格的中间。此外,表格环境中的 h 线超出了主表的边框。
希望您能理解我的问题并帮助我。提前谢谢
\documentclass{article}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{float}
\begin{document}
\begin{tabularx}
{\textwidth}
{
|>{\hsize=.375\hsize}X|
>{\hsize=.525\hsize}X|
>{\hsize=2,1\hsize}X|
}
\hline
\textbf{Head A} &
\textbf{Head B} &
\begin{tabular}{>{\hsize=1.2\hsize}X|>{\hsize=.8\hsize}X}
\textbf{Head C} &
\textbf{Head D}\\
\end{tabular} \\
\hline
should be vertical centered to tabular &
this too &
\begin{tabular}{>{\hsize=1.2\hsize}X|>{\hsize=.8\hsize}X}
This is a long Text with multiple lines &
This is vertical centered\\
\hline
another really really really really really really really really really really really really really really really looooong Text &
AB \\
\end{tabular} \\
\end{tabularx}
\end{document}
答案1
使用tabularray
包装很简单:
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{center}
\begin{tblr}{hlines, vlines,
colspec = {X[l] X[l] X[2,j,m] X[1.5,l,m]},
row{1} = {font=\bfseries}
}
Head A & Head B & Head C & Head D \\
\SetCell[r=2]{m} Should be vertical centered to tabular
& \SetCell[r=2]{m} this too
& This is a long Text with multiple lines
& This is vertical centered \\
& & another really really really really really really really really really really really really really really really looooong Text
& AB \\
\end{tblr}
\end{center}
\end{document}
答案2
我认为使用三行表格而不是嵌套表格可以节省时间和打字量。
然后,您可以定义一个多行单元格,使前两列的内容垂直居中。
使用该nicematrix
包,\Block
命令将会执行相同的操作。
\Block{2-1}{<content>}
将把内容放置在 2 行 x 1 列的单元格中,垂直居中。
hvlines
将绘制所有垂直线和水平线。
cell-space-limits
垂直扩展单元格。
第三列的宽度是第一列和第二列的两倍。后者的宽度是固定的。内部计算由包完成,就像tabularx
does 一样。
\documentclass{article}
\usepackage{nicematrix}%<<<<<<<<<<<<<<<<<
\begin{document}
\noindent \begin{NiceTabular}[width=\linewidth]{X[l] X[l] X[2,l] w{l}{0.2\linewidth}}[hvlines, cell-space-limits=4pt]
\textbf{Head A} & \textbf{Head B} & \textbf{Head C} & \textbf{Head D}\\
\Block{2-1}{should be vertical centered to tabular} & \Block{2-1}{this too} & \Block{}{This is a long Text with multiple lines} & \Block{}{This is vertical centered}\\
& & \Block{}{another really really really really really really really really really really really really really really really looooong Text} & AB \\
\end{NiceTabular}
\end{document}