表格对齐再次出现问题

表格对齐再次出现问题

我想要一张如下的表格。这是来自在此处输入链接描述

我希望我的Dataset 1Dataset 2在一行,而不是在两行。我尝试删除,\\但结果很奇怪。另外,我希望表格中的内容位于框的中间。

在此处输入图片描述

我的 MWE:

\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}



\begin{document}

\begin{table}[h!]
\centering
\sisetup{table-format=3.2,
             table-column-width=6em}
\renewcommand\cellalign{c}

\begin{threeparttable} 
\begin{tabularx}{\linewidth}{@{}
                    >{\raggedright}X
                    S[table-format=3.0]
                    SS
                             @{}}
\toprule
\multirow{3.4}{=}{\thead{Activity\\ {(Ingredients)}}} 
    &   {\multirow{3.4}{*}{\thead{Number of\\ Activity \\ Assessed}}} 
        &   \multicolumn{2}{c}{\thead{Classification Temperature (\%)}} \\
    \cmidrule{3-4}
    &       &   {\thead{Dataset  \\ 1\tnote{a}  }}
    &   {\thead{Dataset \\ 2\tnote{b}    }}  \\
     \midrule
\textit{Activity 1} \\(Fish puree)  
    & 10   & 98.25 & 77.52  \\
\textit{Activity 2} \\ (Tomatoes and Onion) 
    & 10   & 52.85 & 42.50    \\

    
    \bottomrule
\end{tabularx}
 \footnotesize 
\begin{tablenotes}
 \item[a] \textit{Poofiling dataset}

  \end{tablenotes}

 \end{threeparttable} 

    \end{table}
\end {document}

答案1

以下是表格的两个版本:

在此处输入图片描述

\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}



\begin{document}

\begin{table}[h!]
  \renewcommand{\tabularxcolumn}[1]{m{#1}}
  \centering
  \sisetup{table-format=2.2,
           table-column-width=7em}
  \renewcommand\cellalign{c}
  \begin{threeparttable} 
    \begin{tabularx}{\linewidth}{@{}>{\raggedright}XcSS@{}}
      \toprule
      \multirow{2}{=}{\thead{Activity\\ {(Ingredients)}}} 
        &   {\multirow{2}{*}{\thead{Number of\\ Activity Assessed}}} 
        &   \multicolumn{2}{c}{\thead{Classification Temperature (\%)}} \\
      \cmidrule{3-4}
        &       &   {\thead{Dataset  1\tnote{a}}}
        &   {\thead{Dataset 2\tnote{b}}}  \\
      \midrule
      \textit{Activity 1} \\(Fish puree)  
        & 10   & 98.25 & 77.52  \\
      \textit{Activity 2} \\ (Tomatoes and Onion) 
        & 10   & 52.85 & 42.50    \\    
      \bottomrule
    \end{tabularx}
    \footnotesize 
    \begin{tablenotes}
      \item[a] \textit{Poofiling dataset}
    \end{tablenotes}
  \end{threeparttable} 
\end{table}
    
    
    
\begin{table}[h!]
  \renewcommand{\tabularxcolumn}[1]{m{#1}}
  \centering
  \sisetup{table-format=2.2,
           table-column-width=7em}
  \renewcommand\cellalign{c}
  \begin{threeparttable} 
    \begin{tabularx}{\linewidth}{@{}>{\raggedright}XcSS@{}}
      \toprule
      \multirow{3.4}{=}{\thead{Activity\\ {(Ingredients)}}} 
        &   {\multirow{3.4}{*}{\thead{Number of\\ Activity\\  Assessed}}} 
        &   \multicolumn{2}{c}{\thead{Classification Temperature (\%)}} \\
      \cmidrule{3-4}
        &       &   {\thead{Dataset  1\tnote{a}\\\quad}}
        &   {\thead{Dataset 2\tnote{b}\\\quad}}  \\
      \midrule
      \textit{Activity 1} \\(Fish puree)  
        & 10   & 98.25 & 77.52  \\
      \textit{Activity 2} \\ (Tomatoes and Onion) 
        & 10   & 52.85 & 42.50    \\    
      \bottomrule
    \end{tabularx}
    \footnotesize 
    \begin{tablenotes}
      \item[a] \textit{Poofiling dataset}
    \end{tablenotes}
  \end{threeparttable} 
\end{table}
    
    
\end{document}

答案2

以下是一个在很多方面与唯一的那个由 @leandriis 提供。差异如下:(i) 表格主体中的行之间有额外的空白,(ii)\tnote环境中指令和脚注标记使用的字体形状tablenotes\textit,(iii) 较少使用\thead,以及 (iv) 明确计算第 3 列和第 4 列所需的宽度。

在此处输入图片描述

\documentclass[12pt,oneside]{book}
\usepackage{geometry}
\usepackage[svgnames, table]{xcolor}

\usepackage{makecell}
\renewcommand\theadfont{\small\bfseries} % for column headers
\renewcommand\theadgape{}

\usepackage{ragged2e, booktabs, multirow, tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}

\usepackage[flushleft]{threeparttable}
\renewcommand\TPTtagStyle{\textit}

\usepackage{siunitx,calc}

\newlength\mylen
\begin{document}

\begin{table}[h!]
%\centering % <-- not needed
\settowidth\mylen{\textbf{Classification Temperature} (\%)}
\setlength\mylen{\dimexpr\mylen/2-\tabcolsep\relax}
\sisetup{table-format=2.2,table-column-width=\mylen}

\renewcommand\cellalign{c}

\begin{threeparttable} 
\begin{tabularx}{\linewidth}{@{}
          >{\raggedright}X S[table-format=3.0] S S @{}}
\toprule
   \multirow{2.2}{*}{\thead[l]{Activity\\{(Ingredients)}}} 
& \multicolumn{1}{c}{{\multirow{2.2}{*}{\thead{Number of\\ activities assessed}}}}
& \multicolumn{2}{c@{}}{\textbf{Classification Temperature} (\%)} \\
\cmidrule(l){3-4}
& & {\textbf{Dataset 1}\tnote{a}} & {\textbf{Dataset 2}\tnote{b}}  \\
\midrule
\textit{Activity 1}\\(Fish puree)         & 10 & 98.25 & 77.52\\ 
\addlinespace
\textit{Activity 2}\\(Tomatoes and Onion) & 10 & 52.85 & 42.50\\

\bottomrule
\end{tabularx}

\smallskip\footnotesize 
\begin{tablenotes}
\item[a] Profiling dataset
\item[b] Something else.
\end{tablenotes}
\end{threeparttable} 
\end{table}

\end {document}

相关内容