20

I want to use my raw data in my tables and let the siunitx package do the rounding, so that I can easily adjust the shown precision of my tables depending on my future needs. The problem is that on multiple occasions a -0.00001 or something similar will be rounded to -0.0. Although I understand that in some fields it might be good practice to show from which direction the numbers approach zero, it is quite uncommon in my field and I would like all numbers which are rounded to -0.0 to be displayed as 0.0 without the minus sign.

Is there a specific option to do this? I couldn't find anything in the siunitx documentation.

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs, tabularx}

\begin{document}
  \sisetup{round-mode=places}
  \centering
  \begin{tabular}{
    *2{S[round-precision=1]}
  }\toprule
    {A} & {B}\\\midrule
    -0.0000001 & 2.5823\\
    -2.5823    & 0.0000001\\\bottomrule
  \end{tabular}
\end{document}
  • Isn't the author of siunitx on here? Can we ping them? – Azor Ahai Feb 20 '18 at 20:37
  • 2
    Better than a ping: github.com/josephwright/siunitx/issues/329 – egreg Feb 20 '18 at 23:42
  • I think, in this case, -0.0 is correct. As a standalone number, putting a sign before 0.0 is meaningless. But when 0.0 is result of rounding off -0.00001, it is perfectly fine to have -0.0. Actually, it is required. So the reader knows that it is an approximation of a small negative number. – deshmukh Feb 21 '18 at 6:28
  • @egreg Thanks for making this feature request at the appropriate place. I wasn't sure if this feature is actually missing or if I was just unable to find it in the documentation or internet (wouldn't be the first time...). – Striker Feb 21 '18 at 9:30
  • @deshmukh I think, it really depends on the field and the specific case. In my case the numbers converge from positive to zero. The reason for the very small negative numbers is a little bit of overshooting, but mostly just noise in the data. The minus sign does not give any real additional information, but the inattentive reader could get the impression that this minus sign is by any means meaningful, if the author put it there. I agree though that the minus sign could be a good default, but one should be able to remove it. – Striker Feb 21 '18 at 9:44
12

Indeed siunitx keeps the sign of the original number. As an alternative you can round using pgfplots, which removes the sign for fixed output (it keeps the sign in scientific output).

This can be combined with the collcell package to define a cell operation similar to the S column type from siunitx (see How to execute command on every table column). In the MWE below the an additional check is performed to see if the cell value is numeric (from the package xstring), to allow for table headers. Finally the headers are centered using a one-column multicolumn.

Edit: Note that the multicolumn (being type c) and the numeric check both prevent \pgfmathprintnumber from being called, only one of them would be sufficient for header rows.

MWE:

\documentclass{article}
\usepackage{collcell}
\usepackage{pgfplots}
\usepackage{xstring}

\newcommand{\roundsign}[1]{\IfDecimal{#1}{\pgfmathprintnumber[fixed,fixed zerofill,precision=1]{#1}}{#1}}
\newcolumntype{S}{>{\collectcell\roundsign}{r}<{\endcollectcell}}

\begin{document}
\begin{tabular}{S S}
\multicolumn{1}{c}{A} & \multicolumn{1}{c}{B}\\
\hline
-0.0000001 & 2.5823\\
\hline
-2.5823    & 0.0000001\\
\hline
\end{tabular}

\end{document}

Result:

enter image description here

  • Firstly, thanks for this interesting solution. It works for the MWE, but this combination of collcell, pgfplots and xstring has some limitations compared to the siunitx package and one needs most probably further workarounds for real life examples. For example -15.04598e3 wouldn't be processed to -15.05x10^3. Furthermore this S column is right aligned compared to the centered alignment in siunitx which further aligns all numbers by the decimal dot. Those are just two issues I could directly think of. Maybe my MWE was chosen too simple to actually express that I need some of siunitx benefits. – Striker Feb 22 '18 at 17:15
10

You could use \fpeval to round the numbers and then siunitx to format them:

\documentclass{article}
\usepackage{siunitx,xfp}
\usepackage{booktabs, tabularx}

\begin{document}
  \sisetup{round-mode=places,round-integer-to-decimal}
  \centering
  \begin{tabular}{
    *2{S[round-precision=1]}
  }\toprule
    {A} & {B}\\\midrule
    \fpeval{round(-0.0000001,1)} & \fpeval{round(2.5823,1)}\\
    \fpeval{round(-2.5823,1)}    & \fpeval{round(0.0000001,1)}\\\bottomrule
  \end{tabular}
\end{document}

enter image description here

  • Thanks for this solution. I really like the simplicity of this approach and it seems that one can still use all the benefits of the siunitx package. There are still two issues. If I want to change the precision later, I would have to change this manually for every cell. Of course, this can be prevented by simply defining a new command (\newcommand{\roundcell}[1]{\fpeval{round(#1,1)}}). The second thing is more a convenience issue. It will be quite annoying, but possible, to put \roundcell{} around every value and the readability of the code suffers. – Striker Feb 22 '18 at 17:33
  • You naturally should define a command for the \fpeval{round...}. And agree with you that it would be very neat if one could inject such an (expandable) function with a siunitx key, e.g. use-function=\roundcell. You could make a feature request. – Ulrike Fischer Feb 22 '18 at 17:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.