4

enter image description here

\documentclass[11pt, a4paper]{article}
\usepackage[left=3cm,right=3cm,top=1cm,bottom=2cm]{geometry}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
\begin{table}[h]
    \centering
\begin{tabular}{SS}
\toprule
{$i$}&{$e_i$} \\
\midrule
1&2\\
2&3\\
3&7\\
4&43\\
5&1807
\end{tabular}
\end{table}
\end{document}

Could someone explain me why this table looks so misaligned?

0

2 Answers 2

7

You hadn't specified your number format or specified a default so siunitx defaults kicked in, as you can see in the first table the columns are centred under the heading if you use 2dp, in the second table I give siunitx a hint

enter image description here

\documentclass[11pt, a4paper]{article}
\usepackage[left=3cm,right=3cm,top=1cm,bottom=2cm]{geometry}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
\begin{table}[h]
    \centering
\begin{tabular}{SS}
\toprule
{$i$}&{$e_i$} \\
\midrule
1.01&2.34\\
2.12&3.45\\
3.11&7.22\\
4.22&43.99\\
5.&1807
\end{tabular}

\bigskip

\begin{tabular}{S[table-format=1.0]S[table-format=4.0]}
\toprule
{$i$}&{$e_i$} \\
\midrule
1&2\\
2&3\\
3&7\\
4&43\\
5&1807
\end{tabular}
\end{table}
\end{document}
1
  • Thanks a lot for the very quick and well-written answer!
    – Rhi
    Sep 7, 2021 at 20:45
3

Just for the sake of variety, here's a solution that uses David Carlisle's dcolumn package instead of the siunitx package. That's not meant to somehow cast shade on the siunitx package and its S column type. It's just that the siunitx package does a great many things in addition to providing the S column type. If all you require is to align numbers in the data columns on their explicit or implicit decimal markers, you may be equally well served by employing the specialized dcolumn package.

enter image description here

\documentclass[11pt,a4paper]{article}
\usepackage[left=3cm,right=3cm,top=1cm,bottom=2cm]{geometry}
\usepackage{booktabs}
% new code:
\usepackage{dcolumn}
\newcolumntype{d}[1]{D..{#1}}
\newcommand\mc[1]{\multicolumn{1}{@{}c@{}}{#1}} % handy shortcut macro

\begin{document}
\begin{table}[h]
\centering
$\begin{array}{@{}d{1.0}d{4.0}@{}}
\toprule
i & \mc{e_i} \\
\midrule
1&2\\
2&3\\
3&7\\
4&43\\
5&1807
\end{array}$
\end{table}
\end{document}

You must log in to answer this question.

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