# Implement two-digit-rounding (with uncertainty) in LaTeX using siunitx

I would like to implement the following recommendation for rounding values in tables:

• in general two-digit-uncertainty rounding should be used in tables: the uncertainty is always rounded to two significant digits
• the central value should have a precision that matches the uncertainty

The siunitx package provides options for rounding to a fixed number of significant digits but here we need to do this in dependence on the uncertainty. How can this be achieved?

With version 3 of siunitx you can use round-mode = uncertainty, for example

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\sisetup{uncertainty-mode = separate, round-mode = uncertainty}
\begin{tabular}{
l
S[table-format=3.2(2)]
S[table-format=3.3(2)]
}
\toprule
2 tight $\tau$s     & 72.293(269)     &    80.839(51) \\
2 medium $\tau$s    & 212.383(461)    &   231.155(125) \\
\bottomrule
\end{tabular}
\end{document}


or

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\sisetup{uncertainty-mode = separate, round-mode = uncertainty}
\begin{tabular}{
l
S[table-format=6(2)]
S[table-format=6(2)]
}
\toprule
2 tight $\tau$s     & 72293(269)     &    80839(51) \\
2 medium $\tau$s    & 212383(461)    &   231155(125) \\
\bottomrule
\end{tabular}
\end{document}

• huh, I need to read new manual again ... +1 for this info Commented May 25, 2021 at 10:19
• I spotted a bug with separate uncertainties here: the example in the other answer wasn't working correctly but will be in v3.0.6 later today Commented May 25, 2021 at 13:02
• With MiKTeX I stick to version v3.0.2. Maybe I will be lucky an will be able to upgrade (by MiKTeX console) to v3.0.6 at end of vikend :-) Commented May 25, 2021 at 13:20

Based on my very limited knowledge of TeX (and searching the Internet) I came up with the following solution which I will provide here as a start for others to improve on this:

\documentclass{minimal}

\usepackage{siunitx}
\usepackage{stringstrings}

\newcommand*{\numRF}[2]{\num[round-mode=figures,round-precision=#2]{#1}}

\begin{document}

\newcommand{\matchroundtabular}[2]{% only works for integer values
{%
\aftergroup\round
\stringlength[q]{#1}
\newcount\numlen
\numlen=\theresult
\stringlength[q]{#2}
\newcount\errorlen
\errorlen=\theresult
\newcount\round
\round=\numlen
\expandafter
}
\the\round
{\numRF{#1}{\round}} & {\numRF{#2}{2}}%
}

% Problem 1: alignment off
\sisetup{round-mode = figures,round-precision = 2, table-number-alignment = right}%
\begin{tabular}{
l
S[table-format=6.0]@{$\,\pm\,$}S[table-format=3.0]@{~}
S[table-format=6.0]@{$\,\pm\,$}S[table-format=3.0]@{~}
}
2 tight $\tau$s     & \matchroundtabular{72293}{269}     &    80839 &   51 \\
2 medium $\tau$s    & \matchroundtabular{212383}{461}    &   231155 &  125 \\
\end{tabular}

% Problem 2: not working here
\begin{tabular}{c@{$\,\pm\,$}c}
Aaaaa & Bbbbb \\
\matchroundtabular{343454}{455}
\end{tabular}

\end{document}


Output:

So this works but has two problems:

1. The alignment is off. (Compare with the column with 81000+-51, which is correctly aligned but not rounded correctly.)
2. It does not work outside the S column definition. (See second table. My code is bad.)

It currently also only works for integers. (Doing the same for floats is certainly much more complicated -- and I don't need it right now.)