2

This is following up question from enter link description here

enter image description here

the decimal point make it symmetric in a line.

enter image description here

\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
    p columns are \parbox designed for text with linebreaking in the cells. you do not want that here use single line column such as l for the first column and a decimal alignment column (D from dcolumn package or S from siunitix for numeric columns) – David Carlisle Jan 27 at 22:16
5

Since you load the siunitx package anyway, I'd use the S column type for columns 2 and 4.

enter image description here

\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}
  • thanks. I get very confused with this \begin{tabular*}{0.8\textwidth}{ @{\extracolsep{\fill}} p{2cm} S[table-format=2.6] p{2cm} S[table-format=1.5] }. can you explain in simple english for this? – aan Jan 30 at 21:10
  • 1
    @aan - The tabular* environment takes 2 arguments. The 1st is the overall width, and the 2nd is akin to the single argument of a regular, i.e., non-starred, tabular environment. The @{\extracolsep{\fill}} directive instructs LaTeX to increase the whitespace padding between columns in order to achieve the desired overall width. I trust you know what p{2cm}` does. S[table-format=2.6] tells LaTeX to set aside enough space to typeset numbers with up to 2 integer digits and up to 6 decimal digits; S[table-format=1.5] is for numbers with 1 integer digits and up to 5 decimal digits. – Mico Jan 30 at 21:19
5

You want to use siunitx features.

I'd not widen the table, unless it is already almost as wide as the overall text width.

Bottom is how I'd realize the table. Only the necessary packages have been kept in the code.

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

enter image description here

2

It is not the optimal solution, but works fine for small tables. Try adding a \phantom space, for example:

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

Gives the table with the second column aligned as you want: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.