在学习期间,我使用某些表格创建软件计算的标准表格基本可以满足需求。现在,我的教授敦促我以“类似出版物”的格式准备论文。
对我来说最困难的事情是创建美观的表格。
例如参见这个:
\begin{table}[h]
\scalebox{0.9}{
\begin{tabular}{@{}lccccc@{}}
\toprule
& \multicolumn{1}{l}{} & \multicolumn{4}{c}{Total economic damage (mUS\$)} \\ \midrule
& \multicolumn{1}{l}{Risk factor} & \multicolumn{1}{l}{Upstream Losses} & \multicolumn{1}{l}{Downstream Losses} & \multicolumn{1}{l}{Total Losses} & \multicolumn{1}{l}{Ratio} \\ \midrule
Scenario 1 & \begin{tabular}[c]{@{}c@{}}Supervisory control \\ and data acquisition\\ network\end{tabular} & 23.221 & 14.876 & 38.097 & 1.56 \\
Scenario 2 & Cloud service failure & 722 & 1.196 & 1.918 & 0.60 \\
Scenario 3 & \begin{tabular}[c]{@{}c@{}}Health sector and \\ hospitals\end{tabular} & 28.487 & 10.771 & 39.257 & 2.64 \\
Scenario 4 & Municipal services & 23.257 & 9.105 & 32.361 & 2.55 \\
Scenario 5 & Telecommunications & 1.521 & 1.593 & 3.113 & 0.95 \\
Scenario 6 & Cross-sector attack & 34.879 & 37.669 & 72.458 & 0.93 \\ \bottomrule
\end{tabular}}
\caption{xxx}
\end{table}
对于学生论文来说,这看起来还不错。但是,我确实想努力让它看起来更专业。当查看它时,美学效果有点不对劲(例如,行/列之间的空间有时会根据分页符而变大/变小)。
我可以尝试哪些修复方法来实现此目的?
我将不胜感激任何帮助
答案1
我的建议是:
- 只
\multicolumn
在真正需要的地方使用。在您的示例中,使用它来跨越单个列几乎没有意义(我真的想知道为什么这么多人突然这样做......我们应该责怪 ChatGPT 吗?)。 - 嵌套表格也是一样,
p
这里最好选择列类型,这样也能解决单元格内行距过宽的问题。 - 谈到列类型:既然您正在处理数字,为什么不尝试
siunitx
允许您很好地排列在小数点分隔符处对齐的数字的包(我知道,在这里这不是严格必要的,但也许对于其他用例,这是一个好主意)。 - 仅在需要分组的列上绘制横跨最后四列的水平线。
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{array, booktabs, siunitx}
\begin{document}
\renewcommand{\arraystretch}{1.25}
\begin{tabular}{ @{} l >{\raggedright\arraybackslash}p{7em} *{4}{S[table-format=3.3]} @{} }
\toprule
& & \multicolumn{4}{c}{Total economic damage (mUS\$)} \\
\cmidrule{3-6}
& {Risk Factor} & {Upstream Losses} & {Downstream Losses} & {Total Losses} & {Ratio} \\
\midrule
Scenario 1 & Supervisory control and data acquisition network
& 23.221 & 14.876 & 38.097 & 1.56 \\
Scenario 2 & Cloud service failure
& 722.000 & 1.196 & 1.918 & 0.60 \\
Scenario 3 & Health sector and hospitals
& 28.487 & 10.771 & 39.257 & 2.64 \\
Scenario 4 & Municipal services
& 23.257 & 9.105 & 32.361 & 2.55 \\
Scenario 5 & Telecommunications
& 1.521 & 1.593 & 3.113 & 0.95 \\
Scenario 6 & Cross-sector attack
& 34.879 & 37.669 & 72.458 & 0.93 \\
\bottomrule
\end{tabular}
\end{document}
如果允许在列标题中使用换行符,则对齐效果可能会有所改善(感谢 barbara beeton 的提示):
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{array, booktabs, siunitx}
\NewDocumentCommand{\headercell}{ O{4.5em} m }{
\begin{tabular}{ @{} >{\centering\arraybackslash}p{#1} @{} }
#2
\end{tabular}
}
\begin{document}
\renewcommand{\arraystretch}{1.25}
\begin{tabular}{ @{} l >{\raggedright\arraybackslash}p{9em} *{4}{S[table-format=3.3]} @{} }
\toprule
& & \multicolumn{4}{c}{Total economic damage (mUS\$)} \\
\cmidrule{3-6}
& Risk Factor & {\headercell{Upstream Losses}} & {\headercell[5.5em]{Downstream Losses}} & {\headercell[3em]{Total Losses}} & {Ratio} \\
\midrule
Scenario 1 & Supervisory control and data acquisition network
& 23.221 & 14.876 & 38.097 & 1.56 \\
Scenario 2 & Cloud service failure
& 722.000 & 1.196 & 1.918 & 0.60 \\
Scenario 3 & Health sector and hospitals
& 28.487 & 10.771 & 39.257 & 2.64 \\
Scenario 4 & Municipal services
& 23.257 & 9.105 & 32.361 & 2.55 \\
Scenario 5 & Telecommunications
& 1.521 & 1.593 & 3.113 & 0.95 \\
Scenario 6 & Cross-sector attack
& 34.879 & 37.669 & 72.458 & 0.93 \\
\bottomrule
\end{tabular}
\end{document}
在这里,你实际上也可以使用单元格\multicolumn
宏(在此上下文中创建自定义宏需要\NewExpandableDocumentCommand
):
\documentclass{article}
\usepackage{array, booktabs, siunitx}
\NewExpandableDocumentCommand{\headercell}{ O{4.5em} m }{
\multicolumn{1}{ >{\centering\arraybackslash}b{#1} }{#2}
}
\begin{document}
\renewcommand{\arraystretch}{1.25}
\begin{tabular}{ @{} l >{\raggedright\arraybackslash}p{9em} *{4}{S[table-format=3.3]} @{} }
\toprule
& & \multicolumn{4}{c}{Total economic damage (mUS\$)} \\
\cmidrule{3-6}
& Risk Factor & \headercell{Upstream Losses} & \headercell[5.5em]{Downstream Losses} & \headercell[3em]{Total Losses} & {Ratio} \\
\midrule
Scenario 1 & Supervisory control and data acquisition network
& 23.221 & 14.876 & 38.097 & 1.56 \\
Scenario 2 & Cloud service failure
& 722.000 & 1.196 & 1.918 & 0.60 \\
Scenario 3 & Health sector and hospitals
& 28.487 & 10.771 & 39.257 & 2.64 \\
Scenario 4 & Municipal services
& 23.257 & 9.105 & 32.361 & 2.55 \\
Scenario 5 & Telecommunications
& 1.521 & 1.593 & 3.113 & 0.95 \\
Scenario 6 & Cross-sector attack
& 34.879 & 37.669 & 72.458 & 0.93 \\
\bottomrule
\end{tabular}
\end{document}
最后,使用 amazing 包的方法tabularray
:
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{tabularray}
\UseTblrLibrary{booktabs, siunitx}
\begin{document}
\begin{tblr}{
% define columns Q[em] is like >{\raggedright\arraybackslash}p{9em}
colspec = { l Q[9em] *{4}{S[table-format=3.3]} },
% disable siunitx from header rows
row{1-2} = {guard},
% colsep to zero in outer columns, @{} in traditional column definition
column{1} = {leftsep = 0pt},
column{Z} = {rightsep = 0pt},
% set \arraystretch
stretch = 1.25,
% set cell 1-3 to multicol
cell{1}{3} = {c = 4}{c},
% set vertical alignment of second row to bottom
cell{2}{1-Z} = {f},
% set horizontal alignment of header cells of last four columns to center
cell{2}{3-Z} = {c}
}
\toprule
& & Total economic damage (mUS\$) \\
\cmidrule{3-6}
& Risk Factor & {Upstream \\ Losses} & {Downstream \\ Losses} & {Total \\ Losses} & Ratio \\
\midrule
Scenario 1 & Supervisory control and data acquisition network
& 23.221 & 14.876 & 38.097 & 1.56 \\
Scenario 2 & Cloud service failure
& 722.000 & 1.196 & 1.918 & 0.60 \\
Scenario 3 & Health sector and hospitals
& 28.487 & 10.771 & 39.257 & 2.64 \\
Scenario 4 & Municipal services
& 23.257 & 9.105 & 32.361 & 2.55 \\
Scenario 5 & Telecommunications
& 1.521 & 1.593 & 3.113 & 0.95 \\
Scenario 6 & Cross-sector attack
& 34.879 & 37.669 & 72.458 & 0.93 \\
\bottomrule
\end{tblr}
\end{document}
答案2
与 Jasper Habicht (+1) 提出的表格相同,但用{NiceTabular}
构建nicematrix
。
\documentclass{article}
\usepackage{nicematrix, booktabs, siunitx}
\begin{document}
\renewcommand{\arraystretch}{1.25}
\begin{NiceTabular}{ @{} l p[l]{9em} *{4}{S[table-format=3.3]} @{} }
\toprule
& & \Block{1-4}{Total economic damage (mUS\$)} \\
\cmidrule{3-6}
& Risk Factor
& \Block[b]{}{Upstream\\ Losses}
& \Block[b]{}{Downstream\\ Losses}
& \Block[b]{}{Total\\ Losses} & {Ratio} \\
\midrule
Scenario 1 & Supervisory control and data acquisition network
& 23.221 & 14.876 & 38.097 & 1.56 \\
Scenario 2 & Cloud service failure
& 722.000 & 1.196 & 1.918 & 0.60 \\
Scenario 3 & Health sector and hospitals
& 28.487 & 10.771 & 39.257 & 2.64 \\
Scenario 4 & Municipal services
& 23.257 & 9.105 & 32.361 & 2.55 \\
Scenario 5 & Telecommunications
& 1.521 & 1.593 & 3.113 & 0.95 \\
Scenario 6 & Cross-sector attack
& 34.879 & 37.669 & 72.458 & 0.93 \\
\bottomrule
\end{NiceTabular}
\end{document}