Graphics, Figures & TablesNumber Alignment in Data Table

Information and discussion about graphics, figures & tables in LaTeX documents.
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Number Alignment in Data Table

Post by LavaTyper »

Alright but then boldface numbers aren't aligned properly. I searched the siunitx manual and tried all sorts of combinations of textbf and mathbf and detect-weight and I can't get it to work.

How do I get this to work?

Code: Select all

\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}

\begin{document}

\begin{table}[h]
\centering
\addtolength{\tabcolsep}{0.1cm}
\begin{tabular}{c S[
		table-number-alignment = center,
		table-figures-integer = 1,
		add-integer-zero = false,
		table-figures-decimal = 3
	]
	S[
		table-number-alignment = center,
		table-figures-integer = 1,
		add-integer-zero = false,
		table-figures-decimal = 3
	]}
\textbf{DATA} & {\textbf{MMRS2}} & {\textbf{MMRS3}} \\
\hline
A & $\mathbf{.111}$ & .090 \\
B & $\mathbf{.222}$ & .100 \\
C & $\mathbf{-.333}$ & -.110 \\
D & $\mathbf{-.444}$ & .120* \\
E & $\mathbf{.555}$ & -.130* \\
F & .006 & $\mathbf{.666}$ \\
G & .007* & $\mathbf{.777}$ \\
H & -.008 & $\mathbf{.888}$ \\
\hline
\end{tabular}
\end{table}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

hugovdberg
Posts: 133
Joined: Sat Feb 25, 2012 6:12 pm

Number Alignment in Data Table

Post by hugovdberg »

First of all, by explicitly going into math mode siunitx is no longer able to correctly align your numbers. There is however a workaround to make selected cells bold, with help from the etoolbox package. This solution is also mentioned at tex.se
I also recommend to make use of the \sisetup command to set default settings for siunitx, this can be done either as global settings in the preamble, at any point in your text, and also within the table environment. If you want to use different settings for your tables than for the rest of your text, but the same across all tables you could even further simplify your life by defining a new environment to wrap your table in. This is especially useful since you mention having lots of tables, and I presume you want them to have the same look. I also added a few extra options to reserve space for a sign in front of the number (table-sign-mantissa) and the asterisk behind it (table-space-text-post), since the current code gave some warnings about overfull hboxes.
Finally, I know you provided a minimal working example, so perhaps you already use it in your real document, but the booktabs package provides a few commands that replace \hline with rules of different width and fine tuned spacing.

Code: Select all

\documentclass[12pt]{article}
\usepackage{array}
\usepackage{siunitx}

% enable siunitx to properly align bold numbers
\usepackage{etoolbox}
\robustify\bfseries

% Produce nicer tables
\usepackage{booktabs}

% Declare new environment with default settings for siunitx
% It needs a single (possibly empty) argument for placement options
\newenvironment{sitable}[1]{
    \begin{table}[#1]
        \sisetup{
            detect-all,
            table-number-alignment = center,
            add-integer-zero = false,
            table-figures-integer = 1,
            table-figures-decimal = 3,
            table-sign-mantissa,
            table-space-text-post = *,
	}
        \centering
        \addtolength{\tabcolsep}{0.1cm}
}{
    \end{table}
}

\begin{document}

\begin{sitable}{h}
    \begin{tabular}{cSS}
        \toprule
        \textbf{DATA} & {\textbf{MMRS2}} & {\textbf{MMRS3}} \\
        \midrule
        A & .111 & .090 \\
        B & \bfseries .222 & .100 \\
        C & -.333 & -.110 \\
        D & \bfseries -.444 & .120* \\
        E & \bfseries .555 & -.130* \\
        F & .006 & \bfseries .666 \\
        G & .007* & .777 \\
        H & -.008 & \bfseries .888 \\
        \bottomrule
    \end{tabular}
\end{sitable}

\end{document}
Ubuntu 13.10 + Tex Live 2013 + Texmaker / Windows 7 Pro + MikTex 2.9 + TexnicCenter / Android 4.3 + TexPortal + DroidEdit
LavaTyper
Posts: 69
Joined: Sat Feb 11, 2012 2:38 am

Number Alignment in Data Table

Post by LavaTyper »

This seemed to help a lot. Many of my tables are consistently formatted from table and column to other tables (and yes I use booktabs but \hline looks the same), but not usually.
More on this later...
Post Reply