[Tex/LaTex] Can you get a table to auto format numbers to show thousand separators

booktabstables

I'm preparing financial statements in latex and it would be very convenient to be able to just enter the numbers into the table and it automatic knows where to add the commas in the output (see first table table in MWE for how I'd like to enter numbers and second table for how I'd like example output). I've just manually entered the year ends in the table below but you can ignore for solution as I have a command that will enter them so they won't be typed in numbers on the file I'm working on. Also in the example a negative figure is enclosed in brackets but that seems to be out of alignment with the rest of the figures in the table. Is there any way to improve this.

\documentclass[12pt,a4paper]{article} 
\usepackage[utf8]{inputenc} 
\usepackage{booktabs} 

\begin{document}

\section*{Income \& Expenditure Account} 
\begin{table}[htb] 
\centering 
\begin{tabular}{p{8.25cm}crr}  
& \textbf{Note} & \textbf{2016} & \textbf{2015} \\ \hline\\ 
Income & 1 & 100000 & 102000 \\ \cline{3-4}\\ 
Cost of Sales & 2 & (50000) & (45000) \\ \cline{3-4} 
Gross Profit & & 50000 & 57000 \\
\end{tabular}
\end{table}

\begin{table}[htb] 
\centering 
\begin{tabular}{p{8.25cm}crr}
 & \textbf{Note} & \textbf{2016} & \textbf{2015} \\ \hline\\
Income & 1 & 100,000 & 102,000 \\ \cline{3-4}\\ 
Cost of Sales & 2 & (50,000) & (45,000) \\ \cline{3-4} 
Gross Profit & & 50,000 & 57,000 \\
\end{tabular}
\end{table}

\end{document}

Best Answer

A version using package siunitx:

\documentclass[12pt,a4paper]{article} 
\usepackage[utf8]{inputenc} 
\usepackage{booktabs} 
\usepackage{siunitx}
\sisetup{group-separator={,}}

\begin{document}

\section*{Income \& Expenditure Account} 
\begin{table}[htb] 
\centering 
\begin{tabular}{
  p{7.5cm}
  c
  *2{S[table-format=6.0, table-space-text-pre=(, table-space-text-post=)]}
}  
& \textbf{Note} & {\textbf{2016}} & {\textbf{2015}} \\
\midrule
Income & 1 & 100000 & 102000 \\
\cmidrule{3-4}\\ 
Cost of Sales & 2 & {(}50000{)} & {(}45000{)} \\
\cmidrule{3-4} 
Gross Profit & & 50000 & 57000 \\
\end{tabular}
\end{table}
\end{document}

Result

  • Plain numbers can be specified as requested.
  • The numbers are automatically aligned at the decimal marker.
  • Other entries in the same column can be protected from number parsing by curly braces (see table header). These entries are then centered.
  • The entry with parentheses is more complex. The example adds them as pre and post text.
  • The question is tagged (or was) with booktabs. Therefore the example uses the rules provided by the package.
Related Question