# Align decimals in tabular

I am creating a table like this:

\documentclass{article}

\usepackage{siunitx}
\usepackage{multirow}
\usepackage{booktabs}

\begin{document}
\begin{table}[h]
\centering
\caption{Caption here}
\begin{tabular}{@{}
c
l
l
S[table-format=2.1]
S[table-format=2.2]
S[table-format=2]
S[table-format=1]
@{}}
\toprule
{Tree} & \begin{tabular}[c]{@{}c@{}}{Local}\\ {name} \end{tabular} & \begin{tabular}[c]{@{}c@{}}{Scientific} \\ {name} \end{tabular} & {Diameter} & \begin{tabular}[c]{@{}c@{}}{Tree} \\ {height} \end{tabular} & \begin{tabular}[c]{@{}c@{}}{Measured}  \\ {branches}\end{tabular} & \begin{tabular}[c]{@{}c@{}}{}\\ {order} \end{tabular} \\
\midrule
1 & Local name A & \textit{Scientific name A} & 10.1 & 45.54 & 40 & 1 \\
2 & Local name B & \textit{Scientific name B} & 20.2 & 54.45 & 50 & 2 \\
3 & Local name C & \textit{Scientific name C} & 30.3 & 20.02 & 60 & 3 \\
\bottomrule
\end{tabular}
\label{label here}
\end{table}

\end{document}


I put the column names between {} to avoid siunitx having problems reading it like text (siunitx will try to read "ee" from Tree as exponent).

If I use {@{} c l l c c c c @{}} instead of what I propose, it gives me a pdf. If I use the first S[table-format=2.1] also gives me a pdf. Once I put the other one, TexMaker does not compile anymore. It takes like 10x to finish and gives me errors ! Missing control sequence inserted<inserted text>\inaccessible...e} \end{tabular} & {DBH} & \begin{tabular}

Does anyone know why?

Also, the line "name " in "Scientific name" is not aligned left. How can I aligned it left?

thanks!

Here is a simpler code with makecell which compiles. The contents of the \makecell or \thead commands accepts line breaks, and is centred by default, both vertically and horizontally. These commands must be surrounded by a pair of braces in S columns.

I also loaded cellspace to ensure a minimal vertical padding of cells in columns with specifier prefixed with the letter S  (or C if you also load sunitx, as is the case here), and caption to obtain a sensible distance between caption and table

\documentclass{article}

\usepackage{siunitx}
\usepackage{multirow}
\usepackage{booktabs, makecell, cellspace, caption}
\setlength{\cellspacetoplimit}{2.5pt}
\setlength{\cellspacebottomlimit}{2.5pt}

\begin{document}

\begin{table}[h]
\centering
\caption{Caption here}
\begin{tabular}{@{}
c
Cl
l
S[table-format=2.1]
S[table-format=2.2]
S[table-format=2]
S[table-format=1]
@{}}
\toprule
{Tree} & \makecell{Local\\ name} & \makecell{Scientific\\ name} & {Diameter} & {\makecell{Tree \\ height}} & {\makecell{Measured\\branches}} & {\makecell{\\order}} \\
\midrule
1 & Local name A & \textit{Scientific name A} & 10.1 & 45.54 & 40 & 1 \\
2 & Local name B & \textit{Scientific name B} & 20.2 & 54.45 & 50 & 2 \\
3 & Local name C & \textit{Scientific name C} & 30.3 & 20.02 & 60 & 3 \\
\bottomrule
\end{tabular}
\label{label here}
\end{table}

\end{document}


You need to wrap the entire tabular in {}, not just the content of the tabular. However, a macro for the tabular might be helpful. The name in Scientific name is not left aligned because you used a c column in the tabular, you need an l column.

I think I would use \begin{tabular}[b]... for the tabular in the header row, so that the vertical alignment is along the bottom row.

\documentclass{article}

\usepackage{siunitx}
\usepackage{multirow}
\usepackage{booktabs}
\newcommand\quicktab[2][c]{\begin{tabular}[b]{@{}#1@{}} #2 \end{tabular}}
\begin{document}
\begin{table}[h]
\centering
\caption{Caption here}
\begin{tabular}{@{}
c
l
l
S[table-format=2.1]
S[table-format=2.2]
S[table-format=2]
S[table-format=1]
@{}}
\toprule
Tree &
\quicktab{Local \\ name} &
% the optional argument to \quicktab sets the column type for the nested table
% use l for this one
\quicktab[l]{Scientific\\name} &
{Diameter} &
% note the nested tabulars are placed inside {} in the three next rows
{\quicktab{Tree \\ height}} &
{\quicktab{Measured \\ branches}} &
{\quicktab{ \\ order}} \\
\midrule
1 & Local name A & \textit{Scientific name A} & 10.1 & 45.54 & 40 & 1 \\
2 & Local name B & \textit{Scientific name B} & 20.2 & 54.45 & 50 & 2 \\
3 & Local name C & \textit{Scientific name C} & 30.3 & 20.02 & 60 & 3 \\
\bottomrule
\end{tabular}
\label{label here}
\end{table}

\end{document}