# \pgfmathprintnumber and pre-defined styles

In text mode I would like to use \pgfmathprintnumber to print numbers in an uniform style. The style used shall be defined in the preamble of the document. See the following MWE:

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
textnumber/.style={
fixed,
use comma,
fixed zerofill,
precision=4,
1000 sep={.},
},
}

\begin{document}
The deviation from the origin is \pgfmathprintnumber[textnumber]{10.008}\,\%.
\end{document}


Unfortunately this gives me the following error:

! Package pgfkeys Error: I do not know the key '/pgf/number format/textnumber'
and I am goinf to ignore it. Perhaps you misspelled it.


I am a little bit confused now and don not know where else to put the definition of the textnumberstyle. Do you have a hint for me?

• Why don't you consider siunitx? – egreg Jan 5 '16 at 16:24
• Is there any significant difference between using siunitxand the way I use pgfmathprintnumber? – Sven Rüberg Jan 6 '16 at 19:28

\pgfplotsset sets the prefix /pgfplots/ for the keys and \pgfmathprintnumbers uses the prefix /pgf/number format/.

So your definition of textnumber needs the explizit prefix /pgf/number format/:

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
/pgf/number format/textnumber/.style={
fixed,
use comma,
fixed zerofill,
precision=4,
1000 sep={.},
},
}

\begin{document}
The deviation from the origin is \pgfmathprintnumber[textnumber]{10.008}\,\%.
\end{document}


or you have to use \pgfmathprintnumber[/pgfplots/textnumber]{...} and prefix all your options for textnumber with /pgf/number format/:

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
textnumber/.style={
/pgf/number format/.cd,% <- changes the prefix for the following options
fixed,
use comma,
fixed zerofill,
precision=4,
1000 sep={.},
},
}

\begin{document}
The deviation from the origin is \pgfmathprintnumber[/pgfplots/textnumber]{10.008}\,\%.
\end{document}


Result:

Update

As @percusse mentioned in a comment you could also use \pgfkeys instead \pgfplotsset. Then you need at least the packages pgfkeys, pgfmaths and pgfcore for your example.

\documentclass{article}
\usepackage{pgfkeys,pgfmath,pgfcore}% or pgf or tikz or pgfplots

\pgfkeys{
/pgf/number format/textnumber/.style={
fixed,
use comma,
fixed zerofill,
precision=4,
1000 sep={.},
},
}

\begin{document}
The deviation from the origin is \pgfmathprintnumber[textnumber]{10.008}\,\%.
\end{document}


or

\documentclass{article}
\usepackage{pgfkeys,pgfmath,pgfcore}

\pgfkeys{
/textnumber/.style={
/pgf/number format/.cd,% <- changes the prefix for the following options
fixed,
use comma,
fixed zerofill,
precision=4,
1000 sep={.},
},
}

\begin{document}
The deviation from the origin is \pgfmathprintnumber[/textnumber]{10.008}\,\%.
\end{document}

• \pgfkeys might be more appropriate if there is no pgfplots key is set. – percusse Jan 5 '16 at 17:42
• Actually, I was looking for some command like this because pgfplotsset only works when the pgfplots package is loaded... (however, I am using the pgfplots package in my document so this wasn't a problem for me.) But just for the record: Using \pgfkeys instead of pgfplotsset in the above example is a more general way to define the style? – Sven Rüberg Jan 6 '16 at 19:33
• Yes, \pgfkeys and \pgfmathprintnumber already work in your example if you load at least the packages pgfkeys, pgfmath and pgfcore (or one of the packages pgf, tikz, pgfplots). I have updated my answer. – esdd Jan 6 '16 at 20:59