Is it possible to use the siunitx
package to typeset numbers with the "E" notation so that the output might look like
1E3
instead of
1 x 10^3
formattingsiunitx
Is it possible to use the siunitx
package to typeset numbers with the "E" notation so that the output might look like
1E3
instead of
1 x 10^3
Fist solution (doing it manually)
With the normal preferences, siunitx prints the numbers in the way, you type them in. 0.000001
is not printed in normal decimal mode, 1.234e5
is printed in the scientific mode. here a minimal working example:
\documentclass[12pt]{article}
\usepackage{siunitx}
\sisetup{
tight-spacing = true,
input-open-uncertainty = ,
input-close-uncertainty = ,
round-mode = places,
round-precision = 3,
table-space-text-pre = (,
table-space-text-post = )\Star,
table-align-text-pre = false
}
\protected\def\Star{$\text{*}$}
\begin{document}
\begin{tabular}{l*{4}{S}}
2006 & 4e-4 & -0.0282\Star & 3e-4 & -0.0015\\
& (0.0361) & (0.0229) & (0.1285) & (0.0539)\\
2011 &-2e-4 &-0.0315 & 0.0083 & 0.0037\\
\end{tabular}
\end{document}
And here the result:
Update: second solution (doing it automatically)
Here is a version which checks automatically the numbers and format them correctly. First it checks, if the number is in the range between 0.001 and 100, or -0.001 and -100. Then \pgfmathprintnumberto
is used to create a macro containing the number in a verbatim-like scientific form (e.g.1.234e5
), which is interpreted with siunitx.
Update2: Mirco had the right solution for the parenthesis spacing problem, and I edit my answer including his hint.
\documentclass[12pt]{article}
\usepackage{etoolbox}
\usepackage{tikz}
\usepgflibrary{fpu}
\usepackage{siunitx}
\sisetup{
tight-spacing = true,
input-open-uncertainty = ,
input-close-uncertainty = ,
round-mode = places,
round-precision = 3,
table-space-text-post = )\Star,
input-symbols = (
}
\protected\def\Star{$\text{*}$}
\def\mynum{}
\newcommand{\testnum}[1]{\begingroup%
\pgfmathparse{or(and(#1<100,#1>=0.001), and(#1>-100,#1<=-0.001))}%
\globaldefs=1\relax\ifnumequal{\pgfmathresult}{1}%
{\pgfkeys{/pgf/number format/.cd, fixed, precision=3, verbatim}}% printing 12300
{\pgfkeys{/pgf/number format/.cd, sci, sci e, precision=3, verbatim}}% printing 1.23e4
\pgfmathprintnumberto{#1}{\mynum}\endgroup}
\begin{document}
\begin{tabular}{l*{4}{S}}
2006 & {\testnum{0.0004}}\mynum & {\testnum{-0.0282}}\mynum\Star &
{\testnum{0.0003}}\mynum & {\testnum{-0.0015}}\mynum\\
& {\testnum{0.0361}}(\mynum) & {\testnum{0.0229}}(\mynum) &
{\testnum{0.1285}}(\mynum) & {\testnum{0.0539}}(\mynum)\\
2011 & {\testnum{-0.0002}}\mynum & {\testnum{-0.0315}}\mynum &
{\testnum{0.0083}}\mynum & {\testnum{0.0037}}\mynum\\
\end{tabular}
\end{document}
Here the result:
Ok, I think I got most of it - except, now there is an "exponentiation roundtrip" which causes rounding errors (see pic below); if anyone knows how to avoid it that, please post an answer.
It is those 500.12e-3 and 499.97 xtick values that show the rounding errors; the problem here is that the \tick
variable here is logarithm'd (actually, ln'd), so I have to exponentiate it -- and that requires use of fpu
, and then coming back to fixed point; and by the time we've gotten back to the original xtick values domain, those values are already "corrupted": the \typeout
shows:
tick -0.69316, tmp 0.50012
tick 0.0, tmp 1.0000000000
tick 1.60942, tmp 4.99974000000000
tick 2.30258, tmp 10.000000000
tick 3.912, tmp 49.9974000000000
tick 4.60516, tmp 100.00000000
tick 6.21458, tmp 499.974000000000
tick 6.90775, tmp 1000.0000000
tick 8.51717, tmp 4999.74000000000
tick 9.21033, tmp 10000.000000
tick 10.81975, tmp 49997.4000000000
tick 11.51291, tmp 100000.00000
Otherwise, it is siunitx
's \num
that does the engineering notation formatting - through the use of the xticklabel
key of the plot.
Here is the MWE:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5.1}
\usepackage{tikz}
\usepackage{siunitx}
\usepackage[pdftex,active,tightpage]{preview}
\begin{document}
\pgfkeys{
/pgf/number format/sci generic={mantissa sep={\!\cdot\!},exponent={10^{#1}}},
}
% use \newcommand (not \def), so the one (and only)
% argument can be specified in {} (and not in []) brackets
\newcommand{\mynum}[1]{
% =\pgfmathparse{e^\tick}\pgfmathresult : ! Dimension too large. ; - use exp(x)
\pgfkeys{/pgf/fpu}% else dimension too large!
\pgfmathparse{exp(#1)}%
% \edef\tmp{\pgfmathresult}%
% \pgfmathprintnumberto{\pgfmathresult}{\tmp}% nope, gives 1Y5.0012e-1]
\pgfmathfloattofixed{\pgfmathresult}% also outputs in \pgfmathresult
\edef\tmp{\pgfmathresult}%
\pgfkeys{/pgf/fpu=false}%
\typeout{tick #1, tmp \tmp}%
\num[%
scientific-notation=engineering,%
round-mode=places,%
round-precision=2,%
zero-decimal-to-integer,%
group-digits=false,%
exponent-product=\!\cdot\!,%
]{\tmp} %
}
\begin{preview}
\begin{tikzpicture}
\begin{axis}[
axis x line=bottom,
axis y line=left,
scaled ticks=false,
xmode=log,
/pgfplots/xtick={0,0.5,1,5,10,50,100,500,1000,5000,10000,50000,100000},
x tick label style={rotate=-50, anchor=west, align=center, font=\small},
xmin=0, xmax=200000,
ymin=0,
visualization depends on={rawx \as \myx}, % , \tick \as \myxt
nodes near coords={($\pgfmathprintnumber\myx$, \textcolor{black}{\num[scientific-notation = engineering,round-mode=places,round-precision=2,zero-decimal-to-integer]{\myx}}\color{blue})},
% cannot use options to \num here, we're already in options,
% so extra [], like below, will cause "! Missing \endcsname inserted."
% xticklabel=\num[scientific-notation=engineering,round-mode=places,round-precision=2,zero-decimal-to-integer]{123},
% so, use a \newcommand ( \mynum ) here:
xticklabel=\mynum{\tick}, % \myx undefined here, \tick is logarithm'd!
]
\addplot[color=blue,mark=*] coordinates
{ (0.5,1) (1,2) (5,3) (10,4) (50,5) (100,6) (500,7)
(1000,8) (5000,9) (10000,10) (50000,11) (100000,12)};
\end{axis}
\end{tikzpicture}
\end{preview}
\end{document}
Best Answer
Yes, it's possible, see p. 29 in the manual: