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

1000 sep in fractionals doesn't recognize min exponent for 1000 sep #1379

Open
Mo-Gul opened this issue Jan 4, 2025 · 3 comments
Open

1000 sep in fractionals doesn't recognize min exponent for 1000 sep #1379

Mo-Gul opened this issue Jan 4, 2025 · 3 comments

Comments

@Mo-Gul
Copy link
Contributor

Mo-Gul commented Jan 4, 2025

Brief outline of the proposed feature

I just realized that, when using both options 1000 sep in fractionals and min exponent for 1000 sep the latter is only applied to the digits before the thousand separator.

I am not sure if this is a bug or by intention. But in the latter case it would be nice to make this feature work, either with the same option or by adding another one.

Usage example

\documentclass[border=5pt]{standalone}
\usepackage{pgfmath}
    \pgfkeys{
        /pgf/number format/.cd,
%            set thousands separator={\,},
            min exponent for 1000 sep=4,
            1000 sep in fractionals,
            precision=999,
    }
\begin{document}
    \pgfmathprintnumber{12345}
    \pgfmathprintnumber{1234}
    \pgfmathprintnumber{0.1234}     % <-- here shouldn't be a thousand sep
    \pgfmathprintnumber{0.12345}
\end{document}

Current output

image showing the result of above code

@muzimuzhi
Copy link
Member

muzimuzhi commented Jan 4, 2025

But in the latter case it would be nice to make this feature work, either with the same option or by adding another one.

Looks like we need an other option. Any suggested option name?

PS: I edited your example to load pgfmath instead of tikz.

@muzimuzhi
Copy link
Member

muzimuzhi commented Jan 4, 2025

An casual, experimental attempt. It adds /pgf/number format/shortest fractionals for 1000 sep with initial value 0.

%\documentclass[border=5pt]{standalone}
\documentclass{article}
\usepackage{pgfmath}

\usepackage{xpatch}
\makeatletter
\pgfkeys{
  /pgf/number format/.cd,
  shortest fractionals for 1000 sep/.initial=0,
  in fractionals/.is if=
}

% full \pgfmathprintnumber@fixed@styleDEFAULT@impl@period definition is long
\xpatchcmd\pgfmathprintnumber@fixed@styleDEFAULT@impl@period
  {%
    \ifpgfmathprintnumber@thousand@sep@in@fractional
        \let\pgfmathresultfractional\pgfutil@empty%
        \pgfmathprintnumber@thousand@sep@in@fractional#2MMMM\@@
    \else
        \def\pgfmathresultfractional{#2}%
    \fi
  }
  {%
    \ifpgfmathprintnumber@thousand@sep@in@fractional
        \let\pgfmathresultfractional\pgfutil@empty%
        %%% patch begin
        \ifnum\pgfmathprintnumber@count@digits{#2}%
             >\pgfkeysvalueof{/pgf/number format/shortest fractionals for 1000 sep}\relax
          \let\pgfmathprintnumber@thousand@sep@fractional=%
              \pgfmathprintnumber@fixed@styleDEFAULT@THOUSAND@SEP
        \else
          \let\pgfmathprintnumber@thousand@sep@fractional=\pgfutil@empty
        \fi
        %%% patch end
        \pgfmathprintnumber@thousand@sep@in@fractional#2MMMM\@@
    \else
        \def\pgfmathresultfractional{#2}%
    \fi
  }{}{\PatchFailed}

\ExplSyntaxOn
\def\pgfmathprintnumber@count@digits{\tl_count:n}
\ExplSyntaxOff

\def\pgfmathprintnumber@thousand@sep@in@fractional#1#2#3#4{%
    \if#4M%
        % Ok, no further separator
        \def\pgfmathprint@next{\pgfmathprintnumber@thousand@sep@in@fractional@finish#1#2#3#4}%
    \else
        \begingroup
        \toks0=\expandafter{\pgfmathresultfractional}%
        \toks1={#1#2#3}%
        %%% patch begin
        \toks2=\expandafter{\pgfmathprintnumber@thousand@sep@fractional}%
        %%% before
        % \toks2=\expandafter{\pgfmathprintnumber@fixed@styleDEFAULT@THOUSAND@SEP}%
        %%% patch end
        \xdef\pgfmathfloat@glob@TMP{\the\toks0 \the\toks1 \the\toks2 }%
        \endgroup
        \let\pgfmathresultfractional=\pgfmathfloat@glob@TMP
        \def\pgfmathprint@next{\pgfmathprintnumber@thousand@sep@in@fractional#4}%
    \fi
    \pgfmathprint@next
}
\makeatother

    \pgfkeys{
        /pgf/number format/.cd,
%            set thousands separator={\,},
            min exponent for 1000 sep=4,
            1000 sep in fractionals,
            precision=999,
    }

\begin{document}
\def\tests{%
    \pgfmathprintnumber{12345}
    \pgfmathprintnumber{1234}
    \pgfmathprintnumber{0.1234}     % <-- here shouldn't be a thousand sep
    \pgfmathprintnumber{0.12345}
}

\noindent\verb|shortest fractionals for 1000 sep=0| (initial) \par
\tests

\noindent\verb|shortest fractionals for 1000 sep=4| \par
\pgfkeys{/pgf/number format/shortest fractionals for 1000 sep=4}
\tests
\end{document}
image

@Mo-Gul
Copy link
Contributor Author

Mo-Gul commented Jan 4, 2025

Awesome. Another option would be to use a boolean which links to min exponent for 1000 sep. I can't think of a good reason why to differ from this approach. Then a possible name could be apply min exponent for 1000 sep to fractionals.
(At least for me it is hard to think of a good name for both of the options ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants