11

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}

enter image description here

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?

  • 3
    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
  • @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
  • When you are typing \fbox{$\pgfmathprintnumber{-5e-5}$} the fbox is not in mathmode so the conditional goes to the \else part, therefore you inserting an extra $ and you end up with math display mode within the fbox. Chuang hit the nail on the head? – yannisl Oct 29 '11 at 17:21
  • @YiannisLazarides I will look into this. But I think that TeX will only look for ONE token to check if a $ is actually the start of a $$ -- and there are much more tokens between the involved math shifts. Thus, it switches to math mode and the \ifmmode should be true. – Christian Feuersänger Oct 29 '11 at 17:30

1 Answers1

11

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

to your document.

Caramdir
  • 89,023
  • 26
  • 255
  • 291