Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
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
show 2 more comments

1 Answer

up vote 10 down vote accepted

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.

share|improve this answer
    
Excellent. That fixed it. I did not know there could be such problems in using these braces as group delimiters! Thanks for your help here. –  Christian Feuersänger Oct 29 '11 at 17:27
    
@Christian: Neither did I until a few minutes ago. –  Caramdir Oct 29 '11 at 17:29
    
Reported the bug: sourceforge.net/tracker/… –  Caramdir Oct 29 '11 at 17:38
    
Ah - the bug report was actually unnessary: I am responsible for the number printer and I will commit the bugfix within the next minutes. –  Christian Feuersänger Oct 29 '11 at 18:09
3  
OK, is fixed now in PGF CVS. I found some more occurrences of the same problem in that file. –  Christian Feuersänger Oct 29 '11 at 18:24
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.