siunitx icon indicating copy to clipboard operation
siunitx copied to clipboard

Different formatting for digits resp. before and after decimal point

Open Soldyesis opened this issue 2 years ago • 5 comments

Hello,

When writing a decimal number, it is a common usage to typeset the digits after the decimal separator differently from the digits of the integral part:

  • E.g., when indicating a price, it is very common, in many countries, to typeset the number of cents in superscript;
  • Also, on French roadsigns (and possibly in other countries), the official norm requires the digits after the decimal comma to be typeset in a smaller font : see e. g. here.

At the time I used the (now unmaintained) package numprint (which siunitx takes some of its inspiration from), there was an internal command \nprt@printafter which could be hacked to get the desired effect. But now that I am using siunitx, I could not find such a feature in the current implementation...

Anyway, it would be nice that the siunitx would include the possibility (without any internal hacking by the user, I mean) to typeset post-decimal-point digits differently from the integer part (e.g. in a smaller font size, or as a superscript, or as text figures, or without thousand grouping, &c., &c.): for it is a common usage as explained above, and a quite convenient one IMHO!

Kind regards,     /Soldyesis

Soldyesis avatar Mar 03 '23 13:03 Soldyesis

This is one of those tricky borderline things. The core aim of siunitx is proper typesetting of quantities, and there the entire point is that the same formatting should apply to everything. It's hard to split formatting of a number and generate a form that is 'correct' for the main target.

josephwright avatar Mar 03 '23 13:03 josephwright

As there is a documented API for siunitx, you could construct your own command. As the API is stable, this is not a 'hack': it's supported as extensible behaviour. A demo:

\documentclass{article}
\usepackage{siunitx}
\ExplSyntaxOn
\NewDocumentCommand \soldyesisnum { O { } m }
  {
    \mode_leave_vertical:
    \group_begin:
      \keys_set:nn { siunitx } { #1 , mode = text }
      \soldyesis_number_format:nN {#2} \l__siunitx_tmp_tl
      \siunitx_print_number:V \l__siunitx_tmp_tl
    \group_end:
  }
\cs_new_protected:Npn \soldyesis_number_format:nN #1#2
  {
      \siunitx_number_parse:nN {#1} \l__siunitx_tmp_tl 
      \siunitx_number_process:NN \l__siunitx_tmp_tl  \l__siunitx_tmp_tl
      \tl_set:Nx \l__siunitx_tmp_tl
        { \siunitx_number_output:NN \l__siunitx_tmp_tl \q_nil }
      \tl_set:Nx #2
        {
          \exp_after:wN \__soldyesis_number_format:w \l__siunitx_tmp_tl
            \q_stop
        }
  }
\cs_new:Npn \__soldyesis_number_format:w
  #1 \q_nil #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_stop
  {
    \__soldyesis_number_format:nw {#1#2#3#4} #5 \q_stop
  }
\cs_new:Npn \__soldyesis_number_format:nw
  #1 #2 \q_nil #3 \q_nil #4 \q_nil #5 \q_nil #6 \q_nil #7 \q_nil #8 \q_stop
  {
    \exp_not:N \textcolor { red } {#1}
    \exp_not:N \tiny {#2#3#4#5#6#7}
  }
\ExplSyntaxOff

\begin{document}
\soldyesisnum{10.5}
\end{document}

josephwright avatar Mar 05 '23 22:03 josephwright

Many thanks for this complementary answer! 😀 You are right, this is probably the best way to go, given the general philosophy of siunitx package 🙂

And your code example will be much helpful to me 🙂, for I am not quite acquainted with the LaTeX3 syntax yet... (Actually I have not managed to test your code yet: as, when I tried to run it, I got a “undefined control sequence \siunitx_number_parse:nN” error... But this seems to be a generic TeX.StackExchange question, rather than an issue with siunitx: so about this point I shall ask on dedicated forums 😇).

Kind regards,

Soldyesis avatar Mar 06 '23 14:03 Soldyesis

@Soldyesis If you have v2 of siunitx then it's more tricky as I didn't used to have a programmers API: that's the obvious reason for \siunitx_number_parse:nN not being defined.

josephwright avatar Mar 06 '23 15:03 josephwright

@josephwright After installing a more recent TeX distribution so that I could enjoy the most recent versions of siunitx (and LaTeX3 syntax), I confirm that your example code works just fine for me 🙂 Thanks again! 😀

Soldyesis avatar Mar 17 '23 05:03 Soldyesis