[Tex/LaTex] Consistant formatting with siunitx and pgf: Thousands Separator

pgfplotssiunitxtikz-pgf

I like the default behavior of the siunitx package in that the thousands separator is not applied to four digit numbers, but is applied when the number of digits exceeds four. In documents that contain pgfplots, \pgfmathprintnumber is used for the formatting of the numbers, but it does not exhibit this behavior:

enter image description here

Is there a setting for \pgfmathprintnumber to format numbers similar to the \num macro from the siunitx package so either method can be freely used with for formatting numbers

Code:

\documentclass{article}

\usepackage{siunitx}
\usepackage{pgf}

\sisetup{
    group-digits=true,
    group-separator={\,},
}

\pgfkeys{/pgf/number format/.cd, set thousands separator={\,}}%

\begin{document}
\begin{tabular}{rll}
siunitx: &\num{9000}                &\num{19000} \\
    pgf: &\pgfmathprintnumber{9000} &\pgfmathprintnumber{19000} \\
\end{tabular}
\end{document}

Best Answer

siunitx-independent PGF solution

The same behaviour of siunitx’ option group-four-digits set to false can be achieved in PGF with the /pgf/number format/min exponent for 1000 sep style.

From the PGF manual, section 66.1 “Changing display styles”, p. 547:

/pgf/number format/min exponent for 1000 sep={<number>} (no default, initially 0)

Defines the smallest exponent in scientic notation which is required to draw thousand separators. The exponent is the number of digits minus one, so <number> = 4 will use thousand separators starting with 1e4 = 10000.

A value of 0 disables this feature (negative values are ignored).

Code

\documentclass{article}

\usepackage{siunitx}
\usepackage{pgf}

\sisetup{
    group-digits=true,
    group-separator={\,},
}

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

\begin{document}
\begin{tabular}{rll}
siunitx: &\num{9000}                &\num{19000} \\
    pgf: &\pgfmathprintnumber{9000} &\pgfmathprintnumber{19000} \\
\end{tabular}
\end{document}

Output

Output of min exponent for 1000 sep = 4

siunitx and PGF

If you want to use siunitx and PGF’s number printing system consistently  together you should check out siunitx “little” \SendSettingsToPgf macro.

From the siunitx manual, section 7.9 “Transferring settings to PGF”, p. 63:

The numerical engine in the pgf package has settings similar to those in siunitx. To enable working with both packages easily, the macro \SendSettingsToPgf is available. It will set some commonly-used numerical formatting options in pgf to the current values used by siunitx to make using the two packages together more convenient for end users. This function can be used at any point after loading both the pgf and siunitx packages.

As group-four-digits is set to false per default there are no further adjustment needed in your example.

Code

\documentclass{article}

\usepackage{siunitx}
\usepackage{pgf}

\sisetup{
    group-digits=true,
    group-separator={\,},
%   group-four-digits=false,% default setting
}
\SendSettingsToPgf

\begin{document}
\begin{tabular}{rll}
siunitx: &\num{9000}                &\num{19000} \\
    pgf: &\pgfmathprintnumber{9000} &\pgfmathprintnumber{19000} \\
\end{tabular}
\end{document}

Output

Output of \SendSettingsToPgf

Related Question