# Strange difference in usage of \pgfmathprintnumber

I encountered a strange spacing difference in the following example:

\documentclass{article}

\usepackage{tikz}

\begin{document}

number printer inside of fbox with -5e-5:

\fbox{\pgfmathprintnumber{-5e-5}}

\fbox{$\pgfmathprintnumber{-5e-5}$}
\end{document}


I know that \pgfmathprintnumber eventually uses \pgfutilensuremath which is defined by

\def\pgfutilensuremath#1{%
\ifmmode#1\else$#1$\fi
}


The question is: how can I get consistent behavior? More precisely: how can I change \fbox{\pgfmathprintnumber{-5e-5}} (i.e. change \pgfmathprintnumber) such that it does the same as the other statement (the other way round is not my use case).

Note that this actually a regression in pgfplots: version 1.4 used the second approach whereas version 1.5 uses the first. I would like to keep the first, but with the spacing of the second.

Could it be related to TeX's strategy to look for the next following char after $ to see if it might be a $$? If so, how can I avoid it? - This seems like a bug in pgfmath. The second spacing is incorrect: it printed like {} -5\cdot 10^{-5}, so I guess that pgfmath inserts an empty box somewhere. – Caramdir Oct 29 '11 at 17:03 Use \fbox{$$\pgfmathprintnumber{-5e-5}$$}??? – Chuang Oct 29 '11 at 17:03 @Chuang: Never use $$ .. $$, see Why is $$ preferable to $$?. – Martin Scharrer Oct 29 '11 at 17:05 @Caramdir: Thanks and thanks. Should have tested that first. – Martin Scharrer Oct 29 '11 at 17:09 @Caramdir I would agree... this is probably a bug. And I am quite happy to hear that the second is the wrong choice. But: I traced through the number printer without any clue. I would appreciate any help of volunteers (seems I am blind here). – Christian Feuersänger Oct 29 '11 at 17:16 ## 1 Answer This is a bug in pgfmath. In one place it delimits a group with {} instead of \begingroup ... \endgroup. So in the second version TeX sees the {} inside mathmode causing it to display a binary minus instead of a unary one (try ${}-5$; in the first version you get something like {}$-5\$ and hence unary spacing).

The problematic groups are in \pgfmathfloatrounddisplaystyle@shared@impl@#1#2 and \pgfmathfloatrounddisplaystyle@shared@impl@@#1#2. So to fix it you can add

\makeatletter
\def\pgfmathfloatrounddisplaystyle@shared@impl@#1#2{%
\begingroup\toks0={#1}%
\toks1=\expandafter{\pgfmathfloatrounddisplaystyle@e@mark #2}%
\xdef\pgfmathfloat@glob@TMP{\the\toks0 \the\toks1 }%
\endgroup%
\let\pgfmathresult=\pgfmathfloat@glob@TMP
}%

\def\pgfmathfloatrounddisplaystyle@shared@impl@@#1#2{%
\begingroup\toks0={#1}%
\toks1=\expandafter{\pgfmathfloatrounddisplaystyle@e@mark #2}%
\pgfkeysgetvalue{/pgf/number format/@dec sep mark}\pgfmathprintnumber@fixed@styleDEFAULT@DEC@SEP@MARK
\toks2=\expandafter{\pgfmathprintnumber@fixed@styleDEFAULT@DEC@SEP@MARK}%
\xdef\pgfmathfloat@glob@TMP{\the\toks0 \the\toks1 \the\toks2 }%
\endgroup%
\let\pgfmathresult=\pgfmathfloat@glob@TMP
}
\makeatother