12

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}
  • I don't know the details enough, but it's an idea: Before passing the number to siunitx compare it to your boundaries. If they are inside, just let them pass. If not, calculate the desired mantissa and the missing powers of 10. Pass the desired mantissa to siunitx and save the powers of 10 to a command \missingpowers. Invoke that command inside the tabular preamble after each row: \begin{tabular}{l*{4}{S@{\missingpowers\renewcommand{\missingpowers}{}}}}. – Toscho Oct 22 '13 at 18:17
  • That sounds like a reasonable approach - I have no idea how to implement it, do you? – PaulB Oct 22 '13 at 18:40
  • According to Joseph Wright at tex.stackexchange.com/questions/35911/…, "Simply don't use \num to indicate numbers within an S column." – Steven B. Segletes Apr 22 '14 at 2:17
3
+200

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

  • 1
    I fail to see how this helps though. The OP wants the conversion to be automatic afaik. – Trefex Apr 25 '14 at 6:52
  • @Trefex: Then I missunderstood the question. – Michael P Apr 25 '14 at 8:36
  • You can overcome the spacing problems with the parentheses by specifying the option input-symbols = (). However, even with that issue taken care of, your answer does not seem to address the main concern of the OP, which was automatic switching between scientific and "normal" notation. – Mico Apr 26 '14 at 6:14
  • @Mirco:Thangs for your hint with input-symbols = ()!I will include this. And my second solution does an automatic switching between scientific and "normal" notation, as you can see in the picture. – Michael P Apr 28 '14 at 8:54
  • @MichaelP - Very nice solution. May I suggest you remove the "detect-mode" option, as removing it will (re)enable the use of typographically correct minus signs instead of dashes? – Mico Apr 28 '14 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.