我仍然有表格对齐问题。有一段时间没用过 LaTex,但搞不清楚我的问题出在哪里。
我想要以下内容(使用 excel)
然而,我在 MWE 中得到了这个:
我的 MWE:
\documentclass[12pt,oneside]{book}
\usepackage[showframe]{geometry}
\usepackage[svgnames, table]{xcolor}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\usepackage{siunitx}
\begin{document}
\begin{table}[h!]
\centering
\sisetup{table-format=2.2, table-number-alignment=center, table-column-width=2.0cm}
\begin{tabular}{lSSS}
\toprule
% \addlinespace[1.2ex]
\multirow{2.4}{*}{\textbf{Study [Method]}} & Number of Stimulus Assessed & \multicolumn{2}{c}{\textbf{Classification Temperature (\%)}}\\
\cmidrule{2-3}
&{\textbf{Dataset 1}}
& {\textbf{Dataset 2}} \\
\midrule
Study 1 [Technology Method] & 61.90 & 95.24 \\
Study 2 [Analysis Method ] & 66.67 & 100.00 \\
\bottomrule
\end{tabular}
\end{table}
\end {document}
答案1
在表格代码中使用加载包(makecell
和tabularx
)后,其代码变得更简单、更短。此外,您需要重新考虑,是否真的需要这么长的列标题:示例数据集,可以更短吗例子(对我来说有同样的含义)?
\documentclass[12pt,oneside]{book}
\usepackage{geometry}
\usepackage[svgnames, table]{xcolor}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries} % for column headers
\renewcommand\theadgape{}
\usepackage{siunitx}
%---------------- 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}[h!]
\centering
\sisetup{table-format=3.2,
table-column-width=6em}
\renewcommand\cellalign{c}
\begin{tabularx}{\linewidth}{@{}
>{\raggedright}X
S[table-format=3.0]
SS
@{}}
\toprule
\multirow{3.4}{=}{\thead{Study\\ {[Method]}}}
& {\multirow{3.4}{*}{\thead{Number of\\ Stimulus\\ Assessed}}}
& \multicolumn{2}{c}{\thead{Classification Temperature (\%)}} \\
\cmidrule{3-4}
& & {\thead{Dataset 1\\(Example)}}
& {\thead{Dataset 2\\(Example)}} \\
\midrule
Study 1 [Technology Method]
& 200 & 61.90 & 95.24 \\
Study 2 [Analysis Method ]
& 299 & 66.67 & 100.00 \\
\bottomrule
\end{tabularx}
\end{table}
\end {document}
(红线表示页面布局)
答案2
您的代码给了我一些错误但稍加修改后我得到了这个,如果它有帮助的话:
\documentclass[12pt,oneside]{book}
\usepackage[showframe]{geometry}
\usepackage[svgnames, table]{xcolor}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx, threeparttable}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\usepackage{siunitx}
\usepackage{adjustbox}
\def\arraystretch{1.5}
\begin{document}
\begin{table}[h!]
\centering
\sisetup{table-format=2.2, table-number-alignment=center, table-column-width=2.0cm}
\adjustbox{max width=\textwidth}{
\begin{tabular}{llll}
\toprule
% \addlinespace[1.2ex]
\multirow{2}{*}{\textbf{\vtop{\hbox{\strut Study}\hbox{\strut [Method]}}}} &
\multirow{2}{*}{\textbf{\vtop{\hbox{\strut Number of}\hbox{\strut Stiumuli Assessed}}}} & \multicolumn{2}{c}{\textbf{Classification Temperature}} \\ \cline{3-4}
& & \vtop{\hbox{\strut \textbf{Dataset 1}}\hbox{\strut \textbf{[Example Dataset]}}} & \vtop{\hbox{\strut \textbf{Dataset 2}}\hbox{\strut \textbf{[Example Dataset]}}} \\ \hline
{Study 1 {[}technology method{]}} & \multicolumn{1}{c}{200} & & \\
{Study 2 {[}analysis method{]}} & \multicolumn{1}{c}{200} & & \\
\bottomrule
\end{tabular}}
\end{table}
\end {document}