My question is similar to Scientific Notation Only For Large Numbers and Adjust the exponent to switch between notations using siunitx
I have a table which, for the most part, has numbers between .001 and 100. I'd like to use scientific notation for numbers outside that range. I use an "S column", which doesn't seem compatible with the solutions linked to above. If instead I use a regular column with the redefined \num command from above, I run into trouble aligning since I have post text (a \Star for statistical significance).
Below is an MWE that includes the code from above links.
How can I selectively use scientific notation and align my numbers correctly?
Thanks!
\documentclass[12pt]{article}
\usepackage{expl3,siunitx}
\sisetup{
scientific-notation=true,
detect-mode,
tight-spacing = true,
input-open-uncertainty = ,
input-close-uncertainty = ,
round-mode = places,
round-precision = 3,
table-space-text-pre = ( [,
table-space-text-post = ) ] \Star
}
\protected\def\Star{$\text{*}$}
\ExplSyntaxOn
\cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
\newcommand*{\ThresholdLow}{0.001}
\newcommand*{\ThresholdHigh}{100}
\renewcommand*{\num}[2][]{%
\fpcmpTF{abs(#2)<=\ThresholdLow}{%
\OldNum[scientific-notation=true,#1]{#2}%
}{%
\fpcmpTF{abs(#2)>=\ThresholdHigh}{%
\OldNum[scientific-notation=true,#1]{#2}%
}{%
\OldNum[scientific-notation=false,#1]{#2}%
}%
}%
}%
\begin{document}
\begin{tabular}{l*{4}{S}}
2006&\num{0.0004} &-0.0282\Star&0.0003 &-0.0015\\
&(0.0361) &(0.0229)&(0.1285) &(0.0539)\\
2011&-0.0002 &-0.0315&0.0083 &0.0037\\
\end{tabular}
\end{document}
Best Answer
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: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.Here the result: