我希望在包含两列数字的四列表格中,数字按小数点对齐

我希望在包含两列数字的四列表格中,数字按小数点对齐

这是来自在此处输入链接描述

在此处输入图片描述

小数点使其在一条线上对称。

在此处输入图片描述

\documentclass[12pt,oneside]{book}

\usepackage[]{geometry}
\usepackage{amsmath}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx,
            threeparttable, tabulary}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\usepackage[svgnames, table]{xcolor}
\usepackage{siunitx} %for table spacing to second row
\usepackage{graphicx}

\usepackage[font=small,
            labelfont={bf,sf}, textfont={sf}, 
            justification=centering]{caption}

\begin{document}


\begin{table}[h!]
\setlength\tabcolsep{0pt}
\centering
%% select suitable overall width (e.g., 0.8\textwidth) and column width (e.g., 1.5cm)
\begin{tabular*}{0.8\textwidth}{ @{\extracolsep{\fill}} *{4}{p{1.9cm}} }
\toprule
\multicolumn{2}{l}{\textbf{Dataset 1}} & \multicolumn{2}{l}{\textbf{Dataset 2}}\\
\cmidrule{1-2} \cmidrule{3-4}
\textbf{Sample} & \textbf{Value} & \textbf{Sample} & \textbf{Value} \\
\cmidrule{1-2} \cmidrule{3-4}

1 & 10.1007 \\
2 & 9.620371 & 827.918 & 9.23345 \\
3 & 8.882499 \\

\bottomrule
\end{tabular*}
\end{table}

\end{document}

答案1

由于无论如何您都会加载siunitx包,因此我将使用S第 2 列和第 4 列的列类型。

在此处输入图片描述

\documentclass[12pt,oneside]{book}

\usepackage{geometry}
\usepackage{amsmath}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx,
            threeparttable, tabulary}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\usepackage[svgnames, table]{xcolor}
\usepackage{siunitx}
\usepackage{graphicx}
\usepackage[font=small, labelfont={bf,sf}, textfont={sf}, 
            justification=centering]{caption}
\begin{document}

\begin{table}[h!]
\sisetup{table-number-alignment=left,group-digits=false}
\setlength\tabcolsep{0pt}
\centering
\begin{tabular*}{0.8\textwidth}{ @{\extracolsep{\fill}} 
  p{2cm} S[table-format=2.6] p{2cm} S[table-format=1.5] }
\toprule
\multicolumn{2}{l}{\textbf{Dataset 1}} & 
\multicolumn{2}{l}{\textbf{Dataset 2}} \\
\cmidrule{1-2} \cmidrule{3-4}
\textbf{Sample} & {\textbf{Value}} & 
\textbf{Sample} & {\textbf{Value}} \\
\cmidrule{1-2} \cmidrule{3-4}
1 & 10.1007 \\
2 & 9.620371 & 827.918 & 9.23345 \\
3 & 8.882499 \\
\bottomrule
\end{tabular*}
\end{table}

\end{document}

答案2

您想使用siunitx功能。

我不会加宽表格,除非它已经和整体文本宽度一样宽。

下面就是我实现表格的方式。代码中只保留了必要的包。

\documentclass[12pt,oneside]{book}

\usepackage{booktabs}
\usepackage{siunitx} %for table spacing to second row

\usepackage[
  font=small,
  labelfont={bf,sf},
  textfont={sf}, 
  justification=centering
]{caption}

\begin{document}


\begin{table}[!htp]
\setlength\tabcolsep{0pt}
\sisetup{group-digits=false}
\centering

\caption{The caption to the table}

%% select suitable overall width (e.g., 0.8\textwidth) and column width (e.g., 1.5cm)
\begin{tabular*}{0.8\textwidth}{
  @{\extracolsep{\fill}}
  c
  S[table-format=2.6]
  S[table-format=3.3]
  S[table-format=1.5]
}
\toprule
\multicolumn{2}{l}{\textbf{Dataset 1}} & \multicolumn{2}{l}{\textbf{Dataset 2}}\\
\cmidrule{1-2} \cmidrule{3-4}
\textbf{Sample} & \textbf{Value} & \textbf{Sample} & \textbf{Value} \\
\cmidrule{1-2} \cmidrule{3-4}

1 & 10.1007 \\
2 & 9.620371 & 827.918 & 9.23345 \\
3 & 8.882499 \\

\bottomrule
\end{tabular*}
\end{table}

\begin{table}[!htp]
\sisetup{group-digits=false}
\centering

\caption{The caption to the table}

%% select suitable overall width (e.g., 0.8\textwidth) and column width (e.g., 1.5cm)
\begin{tabular}{
  @{}
  c
  S[table-format=2.6]
  S[table-format=3.3]
  S[table-format=1.5]
  @{}
}
\toprule
\multicolumn{2}{@{}c}{\textbf{Dataset 1}} & \multicolumn{2}{c@{}}{\textbf{Dataset 2}}\\
\cmidrule(r){1-2} \cmidrule(l){3-4}
\textbf{Sample} & \textbf{Value} & \textbf{Sample} & \textbf{Value} \\
\midrule

1 & 10.1007 \\
2 & 9.620371 & 827.918 & 9.23345 \\
3 & 8.882499 \\

\bottomrule
\end{tabular}
\end{table}

\end{document}

在此处输入图片描述

答案3

这不是最佳解决方案,但对于小型表格来说效果很好。尝试添加一个\phantom空格,例如:

\documentclass[12pt,oneside]{book}

\usepackage[]{geometry}
\usepackage{amsmath}
\usepackage{ragged2e}
\usepackage{booktabs, makecell, multirow, tabularx,
            threeparttable, tabulary}
\renewcommand\theadfont{\small\bfseries} % for bold in table using \small
\renewcommand\theadgape{}
\usepackage[svgnames, table]{xcolor}
\usepackage{siunitx} %for table spacing to second row
\usepackage{graphicx}

\usepackage[font=small,
            labelfont={bf,sf}, textfont={sf}, 
            justification=centering]{caption}

\begin{document}


\begin{table}[h!]
\setlength\tabcolsep{0pt}
\centering
%% select suitable overall width (e.g., 0.8\textwidth) and column width (e.g., 1.5cm)
\begin{tabular*}{0.8\textwidth}{ @{\extracolsep{\fill}} *{4}{p{1.9cm}} }
\toprule
\multicolumn{2}{l}{\textbf{Dataset 1}} & \multicolumn{2}{l}{\textbf{Dataset 2}}\\
\cmidrule{1-2} \cmidrule{3-4}
\textbf{Sample} & \textbf{Value} & \textbf{Sample} & \textbf{Value} \\
\cmidrule{1-2} \cmidrule{3-4}

1 & 10.1007 \\
2 & \phantom{1}9.620371 & 827.918 & 9.23345 \\
3 & \phantom{1}8.882499 \\

\bottomrule
\end{tabular*}
\end{table}

\end{document}

使表格的第二列按您希望的方式对齐: 在此处输入图片描述

相关内容