我正在尝试制作一个单元格中包含多行值的表格。以下是 MWE:
\documentclass[12pt]{article}
\usepackage[a4paper, total={170mm,262mm}, left=20mm, top=20mm]{geometry}
\usepackage{booktabs}
\usepackage{multirow}
\pagestyle{empty}
\begin{document}
\begin{table}[ht]
\centering
\caption{Studies that made a quantitative estimate of the parameters for the analysis on the blah blah blah blah}
\begin{tabular}{p{3.8cm}p{2cm}p{4cm}p{5cm}}
\hline
{\bf Study} & {\bf Study} & {\bf Lead time} & {\bf Methodology for}\\
{\bf location} & {\bf Period} & {\bf (days)} & {\bf qualifying lead time.}\\
\hline
City 1, Country 1 & Jun--Aug 2020 & 2 & \textit{Method 1}: attribute 1 vs attribute 2 \\
City 2, Country 2 (detail A, detail b) & Aug 2020--Jan 2021 &
0 to 6 (different sublocations) & \textit{Method 2}: attribute 1 vs attribute 2 given a certain preprocessing (7-day) \\
\hline
\end{tabular}
\end{table}
\end{document}
与上述内容不同,我希望获得一个具有以下特点的整洁的表格:
- 表格跨越文本(行)宽度
- 表格标题和正文之间的合理间距
- 当内容超出列宽时(包括表格标题),将自动换行。
- 第一列:我希望城市和国家/地区分行显示。如果还有其他文本,则应将其放在更下方,并进行自动换行。
- 所有内容都左对齐(如果我们有居中对齐的列标题,那就没问题)
- 无欠满/过满警告
- 行与行之间有足够的空间(如果添加更多行,上面的表格看起来会很糟糕)
理想表格的示例(在 MS-Word 中生成),但没有标题,如下所示:
有人能建议如何获得这样的表格吗?
答案1
通过使用tabularray
和booktabs
包可以轻松满足您的要求:
\documentclass[12pt]{article}
\usepackage[showframe, % in real document remove this option
a4paper, total={170mm,262mm}, left=20mm, top=20mm]{geometry}
\usepackage{tabularray} % new
\UseTblrLibrary{booktabs} % booktabs package load as tabularray library
\usepackage[skip=1ex,
font=small,
labelfont=bf]{caption} % for caption formating
\begin{document}
\begin{table}[ht]
\caption{Studies that made a quantitative estimate of the parameters for the analysis on the blah blah blah blah}
\begin{tblr}{colspec = {@{} X[1.8, l] X[2, l] X[2, l] X[2.5,l] @{}},
rowsep=5pt,
row{1} = {font=\bfseries,rowsep=2pt},
}
\toprule
{Study\\ location}
& {Study\\ Period}
& {Lead time\\ days}
& {Methodology for\\ qualifying lead time.} \\
\midrule
{City 1,\\ Country 1}
& Jun--Aug 2020
& 2 & \textit{Method 1}: attribute 1 vs attribute 2 \\
{City 2,\\ Country 2\\
(detail A, detail b)}
& Aug 2020--Jan 2021
& 0 to 6 (different sublocations)
& \textit{Method 2}: attribute 1 vs attribute 2 given a certain preprocessing (7-day) \\
\bottomrule
\end{tblr}
\end{table}
\end{document}
答案2
这是一个解决方案,它使用tabularx
环境(目标宽度为\textwidth
)和所有四列的可变宽度版本的X
列类型。还请注意 (a) 明智地使用\newline
指令来创建换行符之内单元格和(b)使用\addlinespace
在行之间插入空白。
\documentclass[12pt]{article}
\usepackage[a4paper, total={170mm,262mm}, left=20mm, top=20mm]{geometry}
\usepackage{booktabs}
% \usepackage{multirow} % (not needed)
% new code:
\usepackage[skip=0.333\baselineskip]{caption} % space below caption
\usepackage{tabularx} % for 'tabularx' env. and 'X' column type
\newcolumntype{L}[1]{%
>{\raggedright\arraybackslash\hsize=#1\hsize}X}
\usepackage{newtxtext,newtxmath} % optional (Times Roman font clones)
\pagestyle{empty} % why?
\begin{document}
\begin{table}[htbp]
\caption{Studies that made a quantitative estimate of the parameters for the analysis on the blah blah blah blah}
\begin{tabularx}{\textwidth}{@{} L{0.8}L{0.6}L{0.9}L{1.7} @{}} % 0.8+0.6+0.9+1.7 = 4 = # of X-type columns
\toprule
\bfseries Study location & \bfseries Study Period & \bfseries Lead time, in days & \bfseries Methodology for qualifying lead time\\
\midrule
City 1, \newline Country 1 & Jun--Aug 2020 & 2 & \textit{Method 1}: Attribute 1 vs attribute 2 \\
\addlinespace
City 2, \newline Country 2 \newline (detail A, detail B) & Aug 2020--\hspace{0pt}Jan 2021 &
0 to 6 (various sublocations) & \textit{Method 2}: Attribute 1 vs attribute 2 given a certain preprocessing (7-day) \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}