[Tex/LaTex] Selectively use scientific notation in siunitx “S column”

siunitxtables

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:

\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:

Tabluar, manual notation switching

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:

Tabular, automatic notation switching