Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different formatting for digits resp. before and after decimal point #647

Open
Soldyesis opened this issue Mar 3, 2023 · 5 comments
Open
Labels
enhancement New feature or request

Comments

@Soldyesis
Copy link

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

@josephwright josephwright added the enhancement New feature or request label Mar 3, 2023
@josephwright
Copy link
Owner

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
Copy link
Owner

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}

@Soldyesis
Copy link
Author

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,

@josephwright
Copy link
Owner

@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.

@Soldyesis
Copy link
Author

@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! 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants