表格中各种对齐方式的问题以及放错 \omit 的警告

表格中各种对齐方式的问题以及放错 \omit 的警告

我在尝试排版表格时遇到多个问题。以下是 MWE:

\documentclass{article}
\usepackage[inner=1.5in,outer=1in,top=1.0in,bottom=1.25in,a4paper]{geometry}
\usepackage{amsmath,bm,booktabs,multirow,multicol,tabularx,siunitx}
\usepackage{graphicx}
\usepackage{arydshln}
\sisetup{
    exponent-product={\times},
    retain-explicit-plus,
    input-open-uncertainty  = ,
    input-close-uncertainty = ,
    table-align-text-pre    = false,
    table-align-text-post = false,
    round-mode=places,
    round-precision=3,
    table-space-text-pre    = (
}

\begin{document}
\begin{table}[t]
\sisetup{round-precision=2}
\centering
\caption{Table Caption}
\resizebox{\textwidth}{!}{%
\begin{tabular}{llSSS}
\toprule
\multirow{3}[0]{*}{\textbf{Group}} 
&   \multirow{3}[0]{*}{\textbf{Attribute}} 
& {\multirow{3}[0]{*}{\multicolumn{1}{c}{\begin{tabular}{c}
        \textbf{A long one:}\\ 
        \textbf{three line}\\ 
        \textbf{heading}
        \end{tabular}}}} 
&   \multicolumn{2}{c}{\begin{tabular}{c}
        \textbf{Another one: two}\\ 
        \textbf{line heading}
        \end{tabular}} \\ 
  \cmidrule(lr){4-5} 
    &   &   & {$\bm\mu$} & {$\bm\sigma$} \\
\addlinespace
\midrule
\multirow{3}{*}{Category One}
    & Part 1    & 9.40  & 1.25  & 99.23 \\
    & Part 2    & 30.93 & 3.92  & 22.43 \\
    & Part 3    & 11.33 & 55.22 & 1.11  \\ 
\midrule
\multirow{4}{*}{Category Two}
    & Part 1    & 22.33 & 33.44 & 44.44 \\
    & Part 2    & 22.77 & 55.66 & 33.33 \\
    & Part 3    & 11.33 & 66.66 & 88.66 \\
    & Part 4    & 23.34  & 33.44  & 15.22 \\ 
\midrule
\multirow{5}[0]{*}{
\begin{tabular}[c]{@{}l@{}}
Two line\\ 
Category Name\\
\end{tabular}} 
& Part 1    & 22.22 & 33.44 & 44.55 \\
& Part 2    & 88.77 & 77.66 & 66.55 \\
& Part 3    & 6.66  & 22.11 & 55.55 \\
& Part 4    & 33.33  & 39.44  & 13.33 \\
& Part 5    & 2.22 & 1.11 & 6.66 \\ 
\midrule
\multirow{3}{*}{Category Last}     
& Part 1    & 44.55 & 66.33 & 44.55 \\
& Part 2    & 11.22 & 44.44 & 33.66 \\
& Part 3    & 1.22  & 11.11  & 5.65  \\ 
\bottomrule
\end{tabular}%
}
\end{table}

\end{document}

siunitx请注意,我在 MWE 中使用。

  1. 前两列的值左对齐。
  2. 其余三列中的值在小数点对齐(因此,列类型为“S”)
  3. 即使值是右对齐的,只要列标题与其下方的值对齐,我也就没问题。

以下是生成的表格的屏幕截图。屏幕截图中指出的问题: 在此处输入图片描述

此外,我还收到“放错\omit”的警告。

有人能解决这些问题吗?

答案1

以下是您可以使用 做的{NiceTabular}事情nicematrix

\documentclass{article}
\usepackage[inner=1.5in,outer=1in,top=1.0in,bottom=1.25in,a4paper]{geometry}
\usepackage{booktabs,siunitx}
\usepackage{graphicx}
\usepackage{nicematrix}
\sisetup{
    exponent-product={\times},
    retain-explicit-plus,
    input-open-uncertainty  = ,
    input-close-uncertainty = ,
    table-align-text-pre    = false,
    table-align-text-post = false,
    round-mode=places,
    round-precision=3,
    table-space-text-pre    = (
}

\begin{document}
\begin{table}[t]
\sisetup{round-precision=2}
\centering
\caption{Table Caption}

\begin{NiceTabular}{llSSS}[columns-width=15mm]
\toprule
\RowStyle{\bfseries}
\Block{2-1}{Group} 
& \Block{2-1}{Attribute} 
& \Block{2-1}{A long one:\\ three line\\ heading} 
& \Block{1-2}{Another one: two\\ line heading} \\
\cmidrule(lr){4-5} 
\RowStyle{\mathversion{bold}}
    &   &   & {$\mu$} & {$\sigma$} \\
\addlinespace
\midrule
\Block{3-1}{Category One}
    & Part 1    & 9.40  & 1.25  & 99.23 \\
    & Part 2    & 30.93 & 3.92  & 22.43 \\
    & Part 3    & 11.33 & 55.22 & 1.11  \\ 
\midrule
\Block{4-1}{Category Two}
    & Part 1    & 22.33 & 33.44 & 44.44 \\
    & Part 2    & 22.77 & 55.66 & 33.33 \\
    & Part 3    & 11.33 & 66.66 & 88.66 \\
    & Part 4    & 23.34  & 33.44  & 15.22 \\ 
\midrule
\Block{5-1}{Two line\\ Category Name}
& Part 1    & 22.22 & 33.44 & 44.55 \\
& Part 2    & 88.77 & 77.66 & 66.55 \\
& Part 3    & 6.66  & 22.11 & 55.55 \\
& Part 4    & 33.33  & 39.44  & 13.33 \\
& Part 5    & 2.22 & 1.11 & 6.66 \\ 
\midrule
\Block{3-1}{Category Last}     
& Part 1    & 44.55 & 66.33 & 44.55 \\
& Part 2    & 11.22 & 44.44 & 33.66 \\
& Part 3    & 1.22  & 11.11  & 5.65  \\ 
\bottomrule
\end{NiceTabular}%
\end{table}

\end{document}

您需要多次编译(因为nicematrix在后台使用 PGF/Tikz 节点)。

上述代码的输出

答案2

我提出了这个解决方案,它依赖于makecell包。我添加了一些代码简化和改进:

\documentclass{article}
\usepackage[inner=1.5in,outer=1in,top=1.0in,bottom=1.25in,a4paper]{geometry}
\usepackage{amsmath,bm,booktabs,multirow,multicol,tabularx,siunitx}
\usepackage{makecell, caption}
\renewcommand{\theadfont} {\small\bfseries}
\renewcommand{\theadset}{\renewcommand{\arraystretch}{0.9}}
\usepackage{graphicx}
\usepackage{arydshln}
\sisetup{
    exponent-product={\times},
    retain-explicit-plus,
    input-open-uncertainty = ,
    input-close-uncertainty = ,
    table-align-text-pre = false,
    table-align-text-post = false,
    round-mode=places,
    round-precision=3,
    table-space-text-pre = (
}

\begin{document}

\begin{table}[t]
\sisetup{round-precision=2, table-column-width=16mm}
\centering
\caption{Table Caption}
\begin{tabular}{llSSS}
\toprule
\multirow{3}[0]{*}{\textbf{Group}}
& \multirow{3}[0]{*}{\textbf{Attribute}}
& {\multirowcell{3}[1.35ex]{\thead{A long one:\\ three line \\ heading}}}
& \multicolumn{2}{c}{\thead{one: two\\ line heading} }\\[-0.5ex]
  \cmidrule(l{1em}r{1em}){4-5}
    & & & $\bm\mu$ & $\bm\sigma$ \\
\addlinespace
\midrule
\multirow{3}{*}{Category One}
    & Part 1 & 9.40 & 1.25 & 99.23 \\
    & Part 2 & 30.93 & 3.92 & 22.43 \\
    & Part 3 & 11.33 & 55.22 & 1.11 \\
\midrule
\multirow{4}{*}{Category Two}
    & Part 1 & 22.33 & 33.44 & 44.44 \\
    & Part 2 & 22.77 & 55.66 & 33.33 \\
    & Part 3 & 11.33 & 66.66 & 88.66 \\
    & Part 4 & 23.34 & 33.44 & 15.22 \\
\midrule
\multirowcell{5}[0pt][l]{Two line\\ Category Name}
& Part 1 & 22.22 & 33.44 & 44.55 \\
& Part 2 & 88.77 & 77.66 & 66.55 \\
& Part 3 & 6.66 & 22.11 & 55.55 \\
& Part 4 & 33.33 & 39.44 & 13.33 \\
& Part 5 & 2.22 & 1.11 & 6.66 \\
\midrule
\multirow{3}{*}{Category Last}
& Part 1 & 44.55 & 66.33 & 44.55 \\
& Part 2 & 11.22 & 44.44 & 33.66 \\
& Part 3 & 1.22 & 11.11 & 5.65 \\
\bottomrule
\end{tabular}
\end{table}

\end{document} 

在此处输入图片描述

答案3

我不会使用\multirow:它不会增加任何清晰度,除了“两行类别名称”,但我相信在您的真实表格中读者会明白这两行指的是同一类别。

\multirow如果您愿意的话,重新插入并不太困难,但在那种情况下\dosplitcell必须有\begin{tabular}[c]而不是\begin{tabular}[t]

标题横跨最后两列的问题在于,标题比两列的总宽度还要宽,所以多余的部分应该放在最后一列。在列之间添加一些空间(确切的数量必须通过视觉来确定)。

\documentclass{article}

\usepackage[inner=1.5in,outer=1in,top=1.0in,bottom=1.25in,a4paper]{geometry}

\usepackage{amsmath,bm,booktabs,siunitx}

\sisetup{
    exponent-product={\times},
    retain-explicit-plus,
    input-open-uncertainty  = ,
    input-close-uncertainty = ,
    table-align-text-pre    = false,
    table-align-text-post = false,
    round-mode=places,
    round-precision=3,
%    table-space-text-pre    = (
}

\NewDocumentCommand{\splitcell}{sO{c}m}{%
  \IfBooleanTF{#1}{\smash{\dosplitcell{#2}{#3}}}{\dosplitcell{#2}{#3}}%
}
\NewDocumentCommand{\dosplitcell}{mm}{%
  \begin{tabular}[t]{@{}#1@{}}#2\end{tabular}%
}

\begin{document}

\begin{table}[t]
\centering

\sisetup{round-precision=2}

\caption{Table Caption}
\begin{tabular}{
  l
  l
  S[table-format=2.2]
  S[table-format=2.2]
  @{\hspace{3em}}     % <---- adjust to suit
  S[table-format=2.2]
}
\toprule
\textbf{Group} & \textbf{Attribute} &
{\bfseries\splitcell*{A long one: \\ three line \\ heading}} &
\multicolumn{2}{c}{\bfseries\splitcell{Another one: \\ two line heading}} \\
\cmidrule(lr){4-5} 
    &   &   & {$\bm\mu$} & {$\bm\sigma$} \\
\midrule
Category One
  & Part 1    &  9.40 &  1.25 & 99.23 \\
  & Part 2    & 30.93 &  3.92 & 22.43 \\
  & Part 3    & 11.33 & 55.22 &  1.11 \\ 
\midrule
Category Two
  & Part 1    & 22.33 & 33.44 & 44.44 \\
  & Part 2    & 22.77 & 55.66 & 33.33 \\
  & Part 3    & 11.33 & 66.66 & 88.66 \\
  & Part 4    & 23.34 & 33.44 & 15.22 \\ 
\midrule
\splitcell*[l]{Two line \\ Category Name} 
  & Part 1    & 22.22 & 33.44 & 44.55 \\
  & Part 2    & 88.77 & 77.66 & 66.55 \\
  & Part 3    &  6.66 & 22.11 & 55.55 \\
  & Part 4    & 33.33 & 39.44 & 13.33 \\
  & Part 5    &  2.22 &  1.11 &  6.66 \\ 
\midrule
Category Last
  & Part 1    & 44.55 & 66.33 & 44.55 \\
  & Part 2    & 11.22 & 44.44 & 33.66 \\
  & Part 3    &  1.22 & 11.11 &  5.65 \\ 
\bottomrule
\end{tabular}

\end{table}

\end{document}

在此处输入图片描述

绝不重新缩放表格。如果您希望表格填充文本宽度,请使用tabular*

\documentclass{article}

\usepackage[inner=1.5in,outer=1in,top=1.0in,bottom=1.25in,a4paper]{geometry}

\usepackage{amsmath,bm,booktabs,siunitx}

\sisetup{
    exponent-product={\times},
    retain-explicit-plus,
    input-open-uncertainty  = ,
    input-close-uncertainty = ,
    table-align-text-pre    = false,
    table-align-text-post = false,
    round-mode=places,
    round-precision=3,
%    table-space-text-pre    = (
}

\NewDocumentCommand{\splitcell}{sO{c}m}{%
  \IfBooleanTF{#1}{\smash{\dosplitcell{#2}{#3}}}{\dosplitcell{#2}{#3}}%
}
\NewDocumentCommand{\dosplitcell}{mm}{%
  \begin{tabular}[t]{@{}#1@{}}#2\end{tabular}%
}

\begin{document}

\begin{table}[t]
\centering

\sisetup{round-precision=2}
\setlength{\tabcolsep}{0pt}

\caption{Table Caption}
\begin{tabular*}{\textwidth}{
  @{\extracolsep{\fill}}
  l
  l
  S[table-format=2.2]
  S[table-format=2.2]
  @{\hspace{3em}}     % <---- adjust to suit
  S[table-format=2.2]
}
\toprule
\textbf{Group} & \textbf{Attribute} &
{\bfseries\splitcell*{A long one: \\ three line \\ heading}} &
\multicolumn{2}{c}{\bfseries\splitcell{Another one: \\ two line heading}} \\
\cmidrule{4-5} 
    &   &   & {$\bm\mu$} & {$\bm\sigma$} \\
\midrule
Category One
  & Part 1    &  9.40 &  1.25 & 99.23 \\
  & Part 2    & 30.93 &  3.92 & 22.43 \\
  & Part 3    & 11.33 & 55.22 &  1.11 \\ 
\midrule
Category Two
  & Part 1    & 22.33 & 33.44 & 44.44 \\
  & Part 2    & 22.77 & 55.66 & 33.33 \\
  & Part 3    & 11.33 & 66.66 & 88.66 \\
  & Part 4    & 23.34 & 33.44 & 15.22 \\ 
\midrule
\splitcell*[l]{Two line \\ Category Name} 
  & Part 1    & 22.22 & 33.44 & 44.55 \\
  & Part 2    & 88.77 & 77.66 & 66.55 \\
  & Part 3    &  6.66 & 22.11 & 55.55 \\
  & Part 4    & 33.33 & 39.44 & 13.33 \\
  & Part 5    &  2.22 &  1.11 &  6.66 \\ 
\midrule
Category Last
  & Part 1    & 44.55 & 66.33 & 44.55 \\
  & Part 2    & 11.22 & 44.44 & 33.66 \\
  & Part 3    &  1.22 & 11.11 &  5.65 \\ 
\bottomrule
\end{tabular*}

\end{table}

\end{document}

在此处输入图片描述

相关内容