有没有办法使用 pgfs 数学数字打印的 sci E 符号,但得到 0 而不是 0E+0?我知道我可以在打印零之前手动更改样式。但我正在寻找一个自动化在 pgfplots 中有效的方法,无需手动替换标签。
平均能量损失
\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
您必须破解\pgfmathprintnumber
,特别是sci E
符号。但为了兼容性,最好保留原始符号sci E
并引入新的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}
您可以将其设为条件数字格式,而不是采用新的数字格式。
\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}
输出类似。