[Tex/LaTex] siunitx S-column in table: can I combine scientific notation and decimal numbers in one column

formattingsiunitxtables

I have a table with values exported from a statistics software, where the values are formatted like this (see example below):

Some (very small ones) are in scientific notation, some are in "normal" decimal notation.

\begin{tabular}{r}
3,158722 \\
-0,00061 \\
6,28E-05 \\
0,000303 \\
1,67E-04 \\
0,000175 \\
-0,00017 \\
0,000025 \\
-0,00012 \\
-7,7E-05 \\
-0,00042 \\
-0,0005 \\
0,128871 \\
-0,00689 \\
0,004255 \\
-0,00245 \\
0,010475 \\
0,007083 \\
0,000852 \\
0,004583 \\
-0,00042 \\
0,002032 \\
-0,00111 \\
0,000556 \\
12,53887 \\
0,0024 \\
-0,00387 \\
0,002836 \\
-0,00052 \\
-0,00022 \\
5,41E-05 \\
-0,00063 \\
3,17E-04 \\
0,000187 \\
-0,08153 \\
-0,07881 \\
0,316826 \\
0,001066 \\
0,009783 \\
-0,02701 \\
0,006054 \\
0,004167 \\
\end{tabular}%
  • How can I define a number formatting that makes sense?
  • Is it possible with siunitxto combine decimal and scientific notation in one column?
    If yes, how can I define when the scientific notation is used and when not?

Best Answer

While siunitx is good at aligning a series of numbers in a similar format, it is less good for this type of mixed input. My initial thought would be

\documentclass{article}
\usepackage{siunitx}

    \begin{document}
    \begin{tabular}{S[table-format = -1.6e1, table-align-exponent = false]}
    3,158722 \\
    -0,00061 \\
    6,28E-05 \\
    0,000303 \\
    1,67E-04 \\
    0,000175 \\
    -0,00017 \\
    0,000025 \\
    -0,00012 \\
    -7,7E-05 \\
    -0,00042 \\
    -0,0005 \\
    0,128871 \\
    -0,00689 \\
    0,004255 \\
    -0,00245 \\
    0,010475 \\
    0,007083 \\
    0,000852 \\
    0,004583 \\
    -0,00042 \\
    0,002032 \\
    -0,00111 \\
    0,000556 \\
    12,53887 \\
    0,0024 \\
    -0,00387 \\
    0,002836 \\
    -0,00052 \\
    -0,00022 \\
    5,41E-05 \\
    -0,00063 \\
    3,17E-04 \\
    0,000187 \\
    -0,08153 \\
    -0,07881 \\
    0,316826 \\
    0,001066 \\
    0,009783 \\
    -0,02701 \\
    0,006054 \\
    0,004167 \\
    \end{tabular}
    \end{document}

but this will have too much space on the right margin. Perhaps a new option is needed in siunitx to handle this case.

One alternative approach is to use a strategy similar to the dcolumn package, making both , and E active in math mode:

\documentclass{article}
\usepackage{array}
\newbox\tabboxa
\newbox\tabboxb
\newcommand*\tabalignstart{%
  \setbox\tabboxa=\hbox{$-1$}%
    \setbox\tabboxa=\hbox to \wd\tabboxa
      \bgroup
        $
        \lccode`\~=`\,\relax
        \mathcode`\,="8000%
        \lowercase{\def~}%
          {%
              $%
            \egroup
            \setbox\tabboxb=\hbox
              \bgroup
                $
                \lccode`\~=`\E\relax
                \mathcode`\E="8000%
                \lowercase{\def~}####1####2####3%
                  {\times 10^{####1####3}}%
                  .
          }%
        \hfill
}
\newcommand*\tabalignstop{%
    $
    \hfil
  \egroup
  \box\tabboxa
  \box\tabboxb
}

    \begin{document}
    \begin{tabular}{>{\tabalignstart}l<{\tabalignstop}}
    3,158722 \\
    -0,00061 \\
    6,28E-05 \\
    0,000303 \\
    1,67E-04 \\
    0,000175 \\
    -0,00017 \\
    0,000025 \\
    -0,00012 \\
    -7,7E-05 \\
    -0,00042 \\
    -0,0005 \\
    0,128871 \\
    -0,00689 \\
    0,004255 \\
    -0,00245 \\
    0,010475 \\
    0,007083 \\
    0,000852 \\
    0,004583 \\
    -0,00042 \\
    0,002032 \\
    -0,00111 \\
    0,000556 \\
    12,53887 \\
    0,0024 \\
    -0,00387 \\
    0,002836 \\
    -0,00052 \\
    -0,00022 \\
    5,41E-05 \\
    -0,00063 \\
    3,17E-04 \\
    0,000187 \\
    -0,08153 \\
    -0,07881 \\
    0,316826 \\
    0,001066 \\
    0,009783 \\
    -0,02701 \\
    0,006054 \\
    0,004167 \\
    \end{tabular}
    \end{document}

Of course, the problem then is that there are no digit separators.