UPDATE: Bruno le Floch gave a great suggestion in the comments on one of the answers to How to subtract both very large numbers and numbers smaller than one?. This now makes it possible to use very large numbers in the range of plus/minus [1e-10000,1e10000].
He uses the package expl3
to define a comparison (test which of the numbers is less or greater):
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
An MWE that only uses scientific notations for very small (now defined as 0.01) and very large (100 in this case) numbers is the following.
\documentclass{article}
\usepackage{expl3,siunitx}
\sisetup{scientific-notation=true}
\ExplSyntaxOn
\cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
%Edit these as you wish:
\newcommand*{\ThresholdLow}{0.01}
\newcommand*{\ThresholdHigh}{100}
\let\OldNum\num%
\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}
\newcommand{\Row}[1]{#1 & \OldNum{#1} & \num{#1}}%
\begin{tabular}{l l l}
Num & Old & New\\\hline\\[-0.7em]
\Row{0.01}\\
\Row{0.1}\\
\Row{1}\\
\Row{10}\\
\Row{100}\\
\end{tabular}
\end{document}
A different way this solution can be used is by making a new .sty
-file that you save as for example threshold.sty
, in which you copy-paste the following:
\RequirePackage{expl3,kvoptions,siunitx}
\SetupKeyvalOptions{family=threshold,prefix=threshold@}
\DeclareStringOption[1]{low}[0.01]
\DeclareStringOption[1]{high}[100]
\ProcessKeyvalOptions*
\sisetup{scientific-notation=true}
\ExplSyntaxOn
\cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
\let\OldNum\num%
\renewcommand*{\num}[2][]{%
\fpcmpTF{abs(#2)<=\threshold@low}{%
\OldNum[scientific-notation=true,#1]{#2}%
}{%
\fpcmpTF{abs(#2)>=\threshold@high}{%
\OldNum[scientific-notation=true,#1]{#2}%
}{%
\OldNum[scientific-notation=false,#1]{#2}%
}%
}%
}
and which you then call using for example \usepackage[low=1e-2,high=1e2]{threshold}
.
The advantage of this is that you can use it more easily in other files, and that it doesn't take up so much space in the file you're editing. Also, it is more flexible, as you can just decide not to use any threshold and call the package without any argument (\usepackage{threshold}
), which then essentially does the same as just using \usepackage{siunitx}
. An other option is to use the arguments [low,high]
when using the package, which then uses the default settings for the low and high thresholds (using scientific notation only for numbers outside the range ±[-0.01,100]).
Here I introduce a secondary parser \parseuncertainty
which will separate out the mean value from the variation, and stuff them into two separate tokens. Then, your \SI
command can compare against \SImeanvalue
rather than #2
.
\documentclass{article}
\RequirePackage[
per-mode=reciprocal,
scientific-notation=true,
retain-explicit-plus,
table-space-text-post=\textsuperscript{~a},
table-align-text-post=true,
table-align-exponent,
table-align-uncertainty,
separate-uncertainty = true,
]{siunitx}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \fpcmpTF \fp_compare:nTF
\ExplSyntaxOff
\let\OldSI\SI%
\renewcommand*{\SI}[3][]{%
\parseuncertainty#2()\relax% NEW
\fpcmpTF{abs(\SImeanvalue)<=.001}{% ALTERED
\OldSI[scientific-notation=true,#1]{#2}{#3}%
}{%
\fpcmpTF{abs(\SImeanvalue)>=1000}{% ALTERED
\OldSI[scientific-notation=true,#1]{#2}{#3}%
}{%
\OldSI[scientific-notation=false,#1]{#2}{#3}%
}%
}%
}
\def\parseuncertainty#1(#2)#3\relax{% NEW
\def\SImeanvalue{#1}\def\SIuncertainty{#2}}% NEW
\begin{document}
\SI{83}{\mega\pascal} looks good, \SI{83(22)}{\mega\pascal} does too.\par
And \SI{12345(456)}{\mega\pascal} also.
\end{document}
Best Answer
You have nearly got it! You need to remove the
\num{...}
and just have the number. See example below. For package documentation I like to use CTAN