# tabularx vertical alignment on dot

I want to align my tabularx tables on the dot. Let's say I have these two tables:

With the following code:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}

\begin{document}

\begin{table}[h]
\centering
\begin{tabularx}{\textwidth}{XX}
\toprule
{Test Nr.} &                    Timestamp \\
\midrule
1  &  2019-01-15 05:22:10.024094 \\
2  &  2019-01-15 05:35:30.024133 \\
3  &  2019-01-23 14:32:17.868884 \\
\bottomrule
\end{tabularx}
\end{table}

\begin{table}[h]
\begin{tabularx}{\textwidth}{XXXXXXX}
\toprule
{Test Nr.} &  a &  b &  c &  d &  e \\
\midrule
1  &                 123 &        -123 &          123.33 &                12 &                                   123 \\
2  &                 123 &        -123 &          123.98 &                123 &                                   5 \\
3  &                 123 &        -123 &          -0.0123 &               123 &                                   - \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}


Now I want the decimal dot from -0.0123 and 123.98 to be vertically aligned.

I found the following solution:

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

\begingroup
% Allow _ and : in macro names (LaTeX3 style)
\catcode\_=11
\catcode\:=11
% Internal code of S
\gdef\tabularxcolumn#1{%
>{\__siunitx_table_collect_begin:Nn S{} }%
p{#1}%  <- this is different (is c in normal S)
<{\__siunitx_table_print:}%
}
\endgroup


The dots line up as wanted but it messes up everything else, especially the timestamps and it removes the last '-' in column e.

To be honest I really don't understand the \begingroup command.

How can I make this work properly?

• I would not use tabularx for these types of data table just use S columns to get decimal alignment. – David Carlisle Jul 18 '19 at 13:14
• dcolumn also preferable... – MadyYuvi Jul 18 '19 at 13:19

Since there is no apparent need for automatic line breaks in any of the six columns of the second tabularx environment, I can think of only one valid reason for employing a tabularx environment here: to make sure that the six columns are all exactly equally wide.

With this observation out of the way, I suggest you load the sunitx package and use its S column type to align the numbers within each column on their respective explicit or implicit decimal markers. Then, use the C column type -- defined below as a centered version of the X column type -- to format the headers of the five data columns.

That said, I think the table at hand would look even better [!] if one employed a tabular* environment instead of the tabularx environment. For the tabular* environment, change the C column type for the first column to c, while keeping the T column types for the five data columns. The main visual difference is in the widths of the intercolumn whitespaces: With tabular*, the intercolumn whitespaces are equal across all columns.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}

\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
%% \usepackage{array} % is loaded automatically by tabularx

\usepackage{siunitx}
\newcolumntype{T}[1]{S[table-format=#1]}

\newcommand{\mC}[1]{\multicolumn{1}{C}{#1}} % handy utility macro

\begin{document}
\begin{table}[h]
\begin{tabularx}{\textwidth}{%
C T{3.0} T{-3.0} T{3.4} T{3.0} T{3.0} }
\toprule
Test Nr. & \mC{a} & \mC{b} & \mC{c} & \mC{d} & \mC{e} \\
\midrule
1  &  123 &  -123 &  123.33 &  12 &  123 \\
2  &  123 &  -123 &  123.98 & 123 &    5 \\
3  &  123 &  -123 & -0.0123 & 123 & {--} \\
\bottomrule
\end{tabularx}

\vspace{5mm} % create some vertical separation
\setlength\tabcolsep{0pt} % let LaTeX figure out intercol. whitespace amounts
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
c T{3.0} T{-3.0} T{3.4} T{3.0} T{3.0} }
\toprule
Test Nr. & {a} & {b} & {c} & {d} & {e} \\
\midrule
1  &  123 &  -123 &  123.33 &  12 &  123 \\
2  &  123 &  -123 &  123.98 & 123 &    5 \\
3  &  123 &  -123 & -0.0123 & 123 & {--} \\
\bottomrule
\end{tabular*}

\end{table}
\end{document}

• nice trick and probably more popular than my usual complaint of "don't use tabularx for data tables" – David Carlisle Jul 18 '19 at 14:02
• @DavidCarlisle - Many thanks for the compliment! :-) I've gone ahead and added code to show how a tabular* environment compares to the tabularx environment for the table at hand. – Mico Jul 18 '19 at 15:15