我有这个代码:
\documentclass[article]{elsarticle}
\begin{document}
\begin{table}[H]
\centering
\caption{Material properties of the PETG and PETG+CF for different designs}
\resizebox{\columnwidth}{!}{%
\begin{tabular}{c|ccccc|cc|}
\cline{2-8}
& & & \bf{PETG+CF} & & & \bf{PETG} & \\ \cline{2-8}
& \multicolumn{1}{c|}{Un-Notched} & \multicolumn{1}{c|}{Notched ($90^\circ$)} & \multicolumn{1}{c|}{Notched ($0^\circ$)} & \multicolumn{1}{c|}{Notched (Fluid)} & Notched (LSC) & \multicolumn{1}{c|}{Un-Notched} & Notched ($0^\circ$) \\ \hline
\multicolumn{1}{|c|}{Modulus (GPa)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & & \multicolumn{1}{c|}{} & \\ \hline
\multicolumn{1}{|c|}{Strength (MPa)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & & \multicolumn{1}{c|}{} & \\ \hline
\multicolumn{1}{|c|}{Toughness (j.m$^{-3}$)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & & \multicolumn{1}{c|}{} & \\ \hline
\multicolumn{1}{|c|}{Failure strain (mm/mm)} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & \multicolumn{1}{c|}{} & & \multicolumn{1}{c|}{} & \\ \hline
\end{tabular}
}
\label{table:3}
\end{table}
\end{document}
我想让第一行两列的文字居中。我应该怎么做?
答案1
- 使用
\resizebox
是个非常糟糕的主意。使用它,您将无法控制字体大小(在这种情况下,字体将不可读。更好的方法是使用较小的字体,减少 的宽度\tabcolsep
,并将列标题写成两行(或更多行)。 - 不幸的是,不知道空单元格的内容是什么。如果它们是数字,您可以考虑使用包
S
中定义的列类型siunitx
。如果不是,那么可以使用文本扩展表格宽度。在这种情况下,可以使用规定宽度的列,例如p{<width>}
。 - 如果您删除垂直线并使用
booktabs
包中定义的规则,您将获得更专业的表格外观(参见第二个示例)。 - 对于单位的书写,使用
siunitx
包中定义的单位是明智的。
第一个例子:
\documentclass{elsarticle}
\usepackage{geometry}
\usepackage{makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\usepackage{siunitx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
{\multicolumn{#1}{c|}{#2}}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\begin{document}
\begin{table}[ht]
\centering
\caption{Material properties of the PETG and PETG+CF for different designs}
\label{table:3}
\small
\setlength\tabcolsep{5pt}
\setcellgapes{3pt}
\makegapedcells
\begin{tabular}{| *{8}{c|} }
\cline{2-8}
\mcc{}
& \mcc[5]{\thead{PETG+CF}}
& \mcc[2]{\thead{PETG}} \\
\cline{2-8}
\mcc{}
& \makecell{Un-\\ Notched}
& \makecell{Notched\\ (\SI{90}{\degree})}
& \makecell{Notched\\ (\SI{0}{\degree})}
& \makecell{Notched\\ (Fluid)}
& \makecell{Notched\\ (LSC)}
& \makecell{Un-\\ Notched}
& \makecell{Notched\\ (\SI{0}{\degree})} \\
\hline
Modulus (GPa) & & & & & & & \\ \hline
Strength (MPa) & & & & & & & \\ \hline
Toughness (j.m$^{-3}$) & & & & & & & \\ \hline
Failure strain (mm/mm) & & & & & & & \\ \hline
\end{tabular}
\end{table}
\end{document}
编辑1:
第二个例子,假设文档有两列。由于表格的宽度与文本宽度相当,因此无法将其分成一列,因此唯一合理的解决方案是将其写在两列上。在这种情况下,stfloats
如果您希望将表格放在插入文本的同一页上(如果有足够的空间),该包会很方便:
\documentclass[twocolumn]{elsarticle}
\usepackage{stfloats}
\usepackage{booktabs, makecell}
\renewcommand\theadfont{\bfseries}
\renewcommand\theadgape{}
\usepackage{siunitx}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{1}m}
{\multicolumn{#1}{c}{#2}}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%
\usepackage{lipsum}
\begin{document}
\begin{table}[ht]
\centering
\caption{Material properties of the PETG and PETG+CF for different designs}
\label{table:3}
\small
\setlength\tabcolsep{5pt}
\setcellgapes{3pt}
\makegapedcells
\begin{tabular}{ *{8}{c} }
\cmidrule[\heavyrulewidth]{2-8}
& \mcc[5]{\thead{PETG+CF}}
& \mcc[2]{\thead{PETG}} \\
\cmidrule{2-6}\cmidrule(l){7-8}
& \makecell{Un-\\ Notched}
& \makecell{Notched\\ (\SI{90}{\degree})}
& \makecell{Notched\\ (\SI{0}{\degree})}
& \makecell{Notched\\ (Fluid)}
& \makecell{Notched\\ (LSC)}
& \makecell{Un-\\ Notched}
& \makecell{Notched\\ (\SI{0}{\degree})} \\
\hline
Modulus (GPa) & & & & & & & \\
Strength (MPa) & & & & & & & \\
Toughness (j.m$^{-3}$) & & & & & & & \\
Failure strain (mm/mm) & & & & & & & \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
(红线表示页面布局)
答案2
您可以使用\multicolumn{...}{...}
任何地方,而不仅仅是在表格标题中。使用它来重新定义本地文本的对齐方式。