6

Is there a way to use the sci E notation of pgfs math number printing but getting 0 instead of 0E+0? I know I could just change the style by hand before printing the zero. But I am looking for an automated way which works in pgfplots without manual replacement of labels.

MWE

\documentclass{article}
\usepackage{pgf}
\begin{document}
\pgfkeys{/pgf/number format/.cd,sci,sci E}
Aim:\\
0 \hspace{1em} 1.23E+1\par
Actual:\\
\pgfmathprintnumber{0}\hspace{1em}
\pgfmathprintnumber{12.345}
\end{document}
1
  • are you only going to use \pgfmathprintnumber or planning to do computations ?
    – percusse
    Commented Jun 20, 2017 at 22:03

1 Answer 1

4

You'd have to hack into \pgfmathprintnumber, in particular the sci E notation. But for compatibility it is better to keep the original sci E and introduce a new sci E skip E+0.

\documentclass{article}
\usepackage{pgf}

\makeatletter

\def\pgfmathfloatrounddisplaystyle@Eskipzero#1#2e#3\relax{%
  \ifnum#3<0\relax
    \pgfmathprintnumber@begingroup
    \count0=#3\relax
    \multiply\count0 by-1
    \xdef\pgfmathfloat@glob@TMP{E{-}\the\count0}%
    \pgfmathprintnumber@endgroup%
    \let\pgfmathresult=\pgfmathfloat@glob@TMP
  \else
    \ifnum#3=0\relax
      \def\pgfmathresult{}%
    \else
      \def\pgfmathresult{E{+}#3}%
    \fi
  \fi
  \def\pgfmathfloat@loc@TMPb{\pgfmathfloatrounddisplaystyle@shared@impl#1#2e#3\relax{}{1}}%
  \expandafter\pgfmathfloat@loc@TMPb\expandafter{\pgfmathresult}%
}

\pgfkeys{
  /pgf/number format/sci E skip E+0/.code={%
    \let\pgfmathfloatrounddisplaystyle=\pgfmathfloatrounddisplaystyle@Eskipzero
  }
}

\makeatother

\begin{document}
\pgfkeys{/pgf/number format/.cd,sci,sci E skip E+0}
Aim:\\
0 \hspace{1em} 1.23E+1\par
Actual:\\
\pgfmathprintnumber{0}\hspace{1em}
\pgfmathprintnumber{12.345}
\end{document}

enter image description here


Instead of a new number format, you could make it a conditional.

\documentclass{article}
\pagestyle{empty}
\usepackage{pgf}

\makeatletter

\def\pgfmathfloatrounddisplaystyle@Eskipzero#1#2e#3\relax{%
  \ifnum#3<0\relax
    \pgfmathprintnumber@begingroup
    \count0=#3\relax
    \multiply\count0 by-1
    \xdef\pgfmathfloat@glob@TMP{E{-}\the\count0}%
    \pgfmathprintnumber@endgroup%
    \let\pgfmathresult=\pgfmathfloat@glob@TMP
  \else
    \ifnum#3=0\relax
      \def\pgfmathresult{}%
    \else
      \def\pgfmathresult{E{+}#3}%
    \fi
  \fi
  \def\pgfmathfloat@loc@TMPb{\pgfmathfloatrounddisplaystyle@shared@impl#1#2e#3\relax{}{1}}%
  \expandafter\pgfmathfloat@loc@TMPb\expandafter{\pgfmathresult}%
}

\newif\ifpgfmathprintnumberskipEzero

\let\pgfmathfloatrounddisplaystyle@Ewithzero=\pgfmathfloatrounddisplaystyle@E

\def\pgfmathfloatrounddisplaystyle@E{%
  \ifpgfmathprintnumberskipEzero
    \expandafter\pgfmathfloatrounddisplaystyle@Eskipzero
  \else
    \expandafter\pgfmathfloatrounddisplaystyle@Ewithzero
  \fi
}

\pgfkeys{
  /pgf/number format/skip E+0/.is if=pgfmathprintnumberskipEzero,
  /pgf/number format/skip E+0/.default=true,
  /pgf/number format/skip E+0=false,
}

\makeatother

\begin{document}
\pgfkeys{/pgf/number format/.cd,sci,sci E,skip E+0}
Aim:\\
0 \hspace{1em} 1.23E+1\par
Actual:\\
\pgfmathprintnumber{0}\hspace{1em}
\pgfmathprintnumber{12.345}
\end{document}

The output is similar.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .