josephwright / siunitx

A comprehensive (SI) units package for LaTeX
LaTeX Project Public License v1.3c
350 stars 25 forks source link

Padding not correct / decimal point at wrong place #725

Closed Splines closed 3 months ago

Splines commented 3 months ago

Presumptions

I use the following sisetup:

\sisetup{
    locale=US,
    group-separator={,},
    group-digits=integer,
    per-mode=symbol,
    separate-uncertainty=true
}

Description of problem

I'm wondering why this input:

$$\qty[minimum-decimal-digits=6]{5.407 \pm 0.04}{\m}$$

is printed as

(5.407000 \pm 0.004000) m in the LaTeX document. Note that siunitx altered the uncertainty from 0.04 to 0.004.


The root cause for this is probably the same for the following confusing output:

\NewDocumentCommand{\UncertStat}{}{_{\text{stat}}}
\NewDocumentCommand{\UncertSys}{}{_{\text{sys}}}
\sisetup{input-digits=0123456789\UncertStat\UncertSys\UncertSystematic}

$$\qty{5.407 \pm 0.010\UncertSys \pm 0.04\UncertStat e-7}{\m}$$

will result in:

image

note that the 0 is printed after the \text{stat}. Is there a way to get rid of this trailing 0 altogether?

Efforts taken

Additional context

If you're wondering why we need this \NewDocumentCommand stuff. A friend of mine and me currently build a Python library called ResultWizard as an interface between Python code and the siunitx LaTeX package. We're in the second alpha version right now and stumbled upon the described issue here.

System Information

I'm using TeX Live in WSL (Ubuntu 22.04 jammy). siunitx version: 3.1.11 (2022-12-05).

josephwright commented 3 months ago

Minimised example

\documentclass{article}
\usepackage{siunitx}
\sisetup{minimum-decimal-digits = 4}
\begin{document}
\qty{5.407 \pm 0.04}{\metre}
\end{document}
josephwright commented 3 months ago

The root cause for this is probably the same for the following confusing output:

Off-topic here, but you seem to have missed that siunitx has native support for multiple uncertainties from v3.1.0, released 2022-04-25.

Splines commented 3 months ago

Thanks a lot for your hot fix 🎉

Unfortunately, users of our library might not be easily able to use an updated version of siunitx. E.g. even for the current LTS Ubuntu (Ubuntu 22.04 jammy), the siunitx version that comes shipped with sudo apt install texlive-science is 3.0.4. We do provide an update guide here (at the very bottom), but only up to version 3.1.11 (2022-12-05), as this is the last one before we'd have to update other dependencies as well as you make use of the new LaTeX3 layer and expl3 is breaking for us.

That's why I'm asking if there is a workaround when using version 3.1.11 (2022-12-05) to still get correct output for the following:

\NewDocumentCommand{\UncertStat}{}{_{\text{stat}}}
\NewDocumentCommand{\UncertSys}{}{_{\text{sys}}}
\sisetup{input-digits=0123456789\UncertStat\UncertSys\UncertSystematic}

$$\qty{5.407 \pm 0.010\UncertSys \pm 0.04\UncertStat e-7}{\m}$$
josephwright commented 3 months ago

@Splines The auxiliary hasn't changed name, so

\ExplSyntaxOn
\cs_gset:Npn \__siunitx_number_digits_uncert_aux:nn #1#2
  {
    \exp_not:n {#2}
    \int_compare:nNnT
      { \l__siunitx_number_min_decimal_int - \tl_count:n {#1} } > 0
      {
        \prg_replicate:nn
          { \l__siunitx_number_min_decimal_int - \tl_count:n {#1} } { 0 }
      }
  }
\ExplSyntaxOff

will locally adjust the code.

I'm still not clear why you are not using the built-in multi-uncertainty support in v3.1.

Splines commented 3 months ago

Thanks a lot!

I'm still not clear why you are not using the built-in multi-uncertainty support in v3.1.

I thought that multi-uncertainty support only referred to being able to add multiple \pms to a number. Having looked through the docs again now, I found the uncertainty-descriptors key and that's exactly what I needed in the first place 🙌. The \NewDocumentCommand was just a workaround since we didn't know about the other option ;) We will go ahead and recommend our users to add this to their preamble (or give them a hint they might find this option useful):

\sisetup{uncertainty-descriptor-mode=subscript}

and then generate programatically the following output for quantities:

\qty[uncertainty-descriptors={customlabel1,customlabel2}]{1.2 \pm 0.1 \pm 0.05}{\m}
Splines commented 3 months ago
\qty{5.407 \pm 0.010 \pm 0.04}{\m}

Is there a way to print the 0.04 "as is", i.e. not 0.040? With round-pad=false and round-mode=none and minimum-decimal-digits=0, we still get 0.040 in version 3.1.11 (2022-12-05). Even with you're hotfix (manually applied via your workaround) it's not working as expected. Maybe I have missed a crucial option in the docs?

josephwright commented 3 months ago

\qty{5.407 \pm 0.010 \pm 0.04}{\m}

Is there a way to print the 0.04 "as is", i.e. not 0.040? With round-pad=false and round-mode=none and minimum-decimal-digits=0, we still get 0.040 in version 3.1.11 (2022-12-05). Even with you're hotfix (manually applied via your workaround) it's not working as expected. Maybe I have missed a crucial option in the docs?

No, becaue the main value here has 3 decimal places, so the uncertainty must also be specified to the same precision.

josephwright commented 3 months ago

(Or rather that's the internal model siunitx uses.)

Splines commented 3 months ago

Ah ok and I guess there's also no (easy) way to overwrite that, right?

josephwright commented 3 months ago

@Splines I can imagine an approach but that's definitely a non-trivial feature request for the current release not something that can be shoehorned into a past release.

Splines commented 3 months ago

In the case you're willing to put your time and effort into such a feature (which would be much appreciated), we'd like to then also be able to make our users profit from it. That's why I've opened #726 to get to know how users can upgrade to the most recent version of siunitx without having to rethink their entire LaTeX setup.

josephwright commented 3 months ago

@Splines Please open a separate issue - including supporting links/PDFs/whatever that show that this approach is used by professionally-typeset material (that's my usual requirement for feature requests).