3

I want to make the content center, i have out of idea what else I can change.

enter image description here enter image description here

\documentclass[12pt,oneside]{book}

 \usepackage{showframe}
 \renewcommand\ShowFrameLinethickness{0.15pt}
 \renewcommand*\ShowFrameColor{\color{red}}

\usepackage{makecell,siunitx}
\usepackage{booktabs}

\begin{document} 

\begin{table}[htp!]
\centering
\begin{tabular}{
  l
  S[table-format=2.2,table-space-text-post=\%]
  S[table-format=3.2,,table-space-text-post=\%]
}
\toprule
    & \multicolumn{2}{c}{\thead{\makebox[0pt]{\textbf{Descriptive Analysis 1}}}}\\ 
     \cmidrule{2-3}
& \textbf{MM}
& \textbf{CM} \\
\midrule
N       & {10}  &  {10}      \\
    Mean    & 91.45\%  & 8.55\% \\
    Median  & 94.74\%  & 5.26\% \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

4 Answers 4

5

You should fix the number of digits for the S column and two pairs of {} are required around the two textbf{...}. You may consider also removing the braces around {10} but I'll leave it for you to decide.

\documentclass{article}
 \usepackage{showframe}
 \renewcommand\ShowFrameLinethickness{0.15pt}
 \renewcommand*\ShowFrameColor{\color{red}}

\usepackage{makecell,siunitx}
\usepackage{booktabs}

\begin{document} 

\begin{table}[htp!]
\setlength{\tabcolsep}{1em}
\centering
\begin{tabular}{
  l
  S[table-format=2.2,table-space-text-post=\%]
  S[table-format=2.2,table-space-text-post=\%]
}
\toprule
    & \multicolumn{2}{c}{\thead{\makebox[0pt]{\textbf{Descriptive Analysis 1}}}}\\ 
     \cmidrule{2-3}
& {\textbf{MM}}
& {\textbf{CM}} \\
\midrule
    N      & {10}       & {10}     \\
    Mean   & 91.45\%  & 8.55\% \\
    Median & 94.74\%  & 5.26\% \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

enter image description here

3
  • Thanks. Could you explain more what is the meaning of \setlength{\tabcolsep}{1em}? thanks.
    – aan
    Commented Sep 1, 2019 at 18:42
  • @aan: \tabcolsep is half of teh horizontal white space between teh contents of adjacent cells. The command \setlength{\tabcolsep}{1em} sets this space to be as wide as 1em. (See also What are the various units (ex, em, in, pt, bp, dd, pc) expressed in mm?)
    – leandriis
    Commented Sep 1, 2019 at 20:04
  • @aan tabcolsepis half the space between two columns, i.e from the right margin of one cell to the Ieft margin in same cell in the next column, it is two tabcolsep. 1em is approximatly the same as the width of the letter m, or 12 pt here. The standard is 6pt and this example double it. The recommendation in some textbook I have, is 0.5em. It is better to set it relative to the fontsize using em than a fixed number of points.
    – Sveinung
    Commented Sep 1, 2019 at 20:18
4

You need to override the S column to a c in all places where you don't have a percentage (and the numbers in the first column should be 4.2):

\begin{tabular}{
  l
  S[table-format=4.2,table-space-text-post=\%]
  S[table-format=3.2,,table-space-text-post=\%]
}
\toprule
    & \multicolumn{2}{c}{\thead{\makebox[0pt]{\textbf{Descriptive Analysis 1}}}}\\
     \cmidrule{2-3}
& \multicolumn{1}{c}{\textbf{MM}}  & \multicolumn{1}{c}{\textbf{CM}} \\
\midrule
N       & \multicolumn{1}{c}{10}  &  \multicolumn{1}{c}{10}      \\
    Mean    & 91.45\%  & 8.55\% \\
    Median  & 94.74\%  & 5.26\% \\
\bottomrule
\end{tabular}

enter image description here

2
  • "and the numbers in the first column should be 4.2" Could you please explain why? As far as I can see the numbers have only 2 digits before and 2 after the decimal separator. Why use 4.2 which corresponds to four digits before and two after the decimal separator?
    – leandriis
    Commented Sep 1, 2019 at 20:06
  • @leandriis Yep, I was wrong. Thinking too much in C format descriptors...
    – Rmano
    Commented Sep 1, 2019 at 21:01
4

Here is how I interpret your table. Since you have aligned the table at decimal point, the number without decimal is set to the left. You may overcome this by enclosing the two nunbers 10 in curly brackets ({10}) or using \multicolumn{1}{c}{10}, but this is typographically probably not correct.

enter image description here

\documentclass[12pt,oneside]{book}

 \usepackage{showframe}
 \renewcommand\ShowFrameLinethickness{0.15pt}
 \renewcommand*\ShowFrameColor{\color{red}}

\usepackage{makecell,siunitx}
\usepackage{booktabs}

\begin{document} 

\begin{table}
\centering
\begin{tabular}{@{}
  l
  S[table-format=2.2,table-space-text-post=\%]
  S[table-format=2.2,table-space-text-post=\%]  % As @Zarko says, reduce this numer to 1.1 if number 10 is centred, else 2.2
@{}}
\toprule
    & \multicolumn{2}{@{}c@{}}{\thead{\textbf{Descriptive Analysis 1}}}\\ 
     \cmidrule{2-3}
& \multicolumn{1}{c}{\textbf{MM}}
& \multicolumn{1}{c}{\textbf{CM}} \\
\midrule
%N      & {10}  &  {10}  \\ % Enclose in brackets to centre the content
N       & 10  &  10      \\
Mean    & 91.45\%  & 8.55\% \\
Median  & 94.74\%  & 5.26\% \\
\bottomrule
\end{tabular}
\end{table}

\end{document}
3

One more variation ...

Your problem arise from the fact that text in multicolumn cell is wider than natural width of the spanned columns. Problem is solved, if you make those column has equal wider width, for example with determine their widths enough large as it is done in the following MWE:

\documentclass[12pt,oneside]{book}
\usepackage{booktabs, makecell}
\renewcommand\theadfont{\small\bfseries}
\renewcommand\theadgape{}
\usepackage{siunitx}

\begin{document}
    \begin{table}
\centering
\sisetup{table-format=2.2,
         table-space-text-post={\%},
         table-column-width=5em}     % <-----
\begin{tabular}{@{} l S S @{}}
\toprule
        & \multicolumn{2}{@{}c@{}}{\thead{Descriptive Analysis 1}}  \\
    \cmidrule{2-3}
        & {\thead{MM}}  & {\thead{CM}}  \\
\midrule
N       & {10}          & {10}          \\
Mean    & 91.45\%       & 8.55\%        \\
Median  & 94.74\%       & 5.26\%        \\
\bottomrule
\end{tabular}
    \end{table}
\end{document}

enter image description here

4
  • I have zero overfull hboxes in my example. And the numbers 10 centre if I enclose them in curly brackets.
    – Sveinung
    Commented Sep 1, 2019 at 20:20
  • @Sveinung, sorry, I didn't test your solution :-(. I will do this asap. However, it has bigger reservation for integers in the last column and consequently work as you said. For this kind of tables number of sample are (usual) centered (since they are different type of data), so doing this is not typographical problematic.
    – Zarko
    Commented Sep 1, 2019 at 20:34
  • @Sveinung, I just test your answer. It also work without any warning , if you for the last column declare S[table-format=1.2,table-space-text-post=\%]. In this case you obtain better result, of course if you enclose number of samples in curly braces :-)
    – Zarko
    Commented Sep 1, 2019 at 20:40
  • Good observation, your are absolutely right, and I should have spotted it myself. Thank you. This assume the the figure 10 is centred. If not, it should be 2.2.
    – Sveinung
    Commented Sep 1, 2019 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .