![多行单元格,每行的文本垂直对齐](https://linux22.com/image/463658/%E5%A4%9A%E8%A1%8C%E5%8D%95%E5%85%83%E6%A0%BC%EF%BC%8C%E6%AF%8F%E8%A1%8C%E7%9A%84%E6%96%87%E6%9C%AC%E5%9E%82%E7%9B%B4%E5%AF%B9%E9%BD%90.png)
是否有可能实现像下图这样的表格,而不需要像我在代码中所做的那样手动划分行标题?
明确地说,我追求的是以下内容。
- 表格宽度设置为
\linewidth
。(我曾经tabularx
这样做过。) - 表格单元格之间的垂直空间增加了(我已经使用了
\renewcommand{\arraystretch}{2}
。) - 行标题(第 1 列)太长时会自动换行。(我手动伪造了这一点。)
- 行标题的线条应与表格主体中的数字对齐。(我通过手动划分行标题并将各部分输入到不同的行来伪造这一点。)
- 换行的行标题应该有一个悬挂缩进。(我通过添加 手动伪造了这一点
\hspace{1em}
。)
\documentclass{article}
\usepackage{showframe}
\usepackage{tabularx}
\usepackage{booktabs}
\begin{document}
\begin{table}[ht]
\renewcommand{\arraystretch}{2} % Exaggregated for the sake of clarity
\begin{tabularx}{\linewidth}{@{} X *{4}{c} @{}}
\toprule
& A & B & C & D \\
\midrule
The first row heading runs over one line & 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
[1em]
The third row heading is slightly longer and & 123 & 123 & 123 & 123 \\
\hspace{1em} runs over two lines & (456) & (456) & (456) & (456) \\
[1em]
The third row heading is actually quite & 123 & 123 & 123 & 123 \\
\hspace{1em} outrageously long and runs over a total of & (456) & (456) & (456) & (456) \\
\hspace{1em} three lines \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
答案1
- 抱歉,我想要的表格设计对我来说不是很清楚..
- 以下是我的三个猜测……
\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}[ht]
\begin{tblr}{colspec={@{} X[l, cmd=\hangindent 1em\hangafter 1]
*{4}{c} @{}},
cell{odd}{1} = {r=2}{},
rowsep=1ex,
vspan = even,
}
The first row heading runs over one lin
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
\end{tblr}
\end{table}
\noindent
or
\begin{table}[ht]
\begin{tblr}{colspec={@{} X[l, cmd=\hangindent 1em\hangafter 1]
*{4}{c} @{}},
cell{odd}{1} = {r=2}{},
row{odd[3]} = {abovesep=2ex},
vspan = even,
}
The first row heading runs over one lin
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
\end{tblr}
\end{table}
\noindent
or
\begin{table}[ht]
\begin{tblr}{colspec={@{} X[l, cmd=\hangindent 1em\hangafter 1]
*{4}{c} @{}},
cell{odd}{1} = {r=2}{},
rowsep = 1pt,
row{odd[3]} = {abovesep=2ex},
}
The first row heading runs over one lin
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
\end{tblr}
\end{table}
\end{document}
(红线表示文本块边框)
附录:
现在我猜你会想这样的事情:
\documentclass{article}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{tabularray}
\UseTblrLibrary{booktabs}
\begin{document}
\begin{table}[ht]
\begin{tblr}{colspec={@{} X[l, h, cmd=\hangindent 1em\hangafter 1]
*{4}{Q[c, h]} @{}},
cell{odd}{1} = {r=2}{},
rowsep = 1pt,
row{odd[3]} = {abovesep=2ex},
% vspan = even,
}
The first row heading runs over one lin
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The second row heading is slightly longer and runs over two lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
The third row heading is actually quite outrageously long and runs over a total of three lines
& 123 & 123 & 123 & 123 \\
& (456) & (456) & (456) & (456) \\
\end{tblr}
\end{table}
\end{document}