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.
Best Answer
For the simple format given, a short chain of dedicated commands can pick up the number. I'm assuming here that the numbers will never have too many digits for TeX's
\number
primitive, which I'm using to trim off any leading zeros.If you want to retain leading zeros and any leading
+
symbols the code can be slightly simplifiedMore complex parsing is possible without using
siunitx
, and indeed thenumprint
package offers quite a bit of this in a package that has been available for many years.How does the above code work? The outer macro
simply grabs the entire input, and inserts it after the first internal macro
\num@auxi
. The latter is going to look for ae
, so we make sure there will be one to find by includingee
after the input. (I'll come back to why twoe
tokens later.)Macro
\num@auxi
comes nextHere we have a delimited macro. Everything up to the first
e
will be grabbed as#1
, everything between that and the seconde
as#3
and the remainder up to\stop
as#3
. There are two outcomes. If the original input contains ane
then#1
will be the mantissa,#2
will be the exponent and#3
is junk. On the other hand, if there was noe
then#1
is the entire number and#2
is empty. We test for that using\ifx\relax#2\relax
(true if#2
is empty) and branch accordingly.If
e
was not found, there is a bit of set up to do the same again but looking for anE
. Once that search is done, we have two outcomes: a number with no exponent or a number with exponent. The first case is handled byDue to the way I've set up the code, this receives two arguments, the mantissa and the exponent. We know the latter is empty (it's only there to act as a 'throw away') so we just insert
#1
(the mantissa). I've included\number
before it, which means TeX will 'tidy up' the input, removing any leading zeros or+
signs.If there is an exponent then the code
is used: this inserts the mantissa then the exponent with correct formatting. I could have combined the last two macros and included a test on
#2
there, but I felt it was clearer with separate paths.You might wonder in the above why we have
rather than just using one
e
(as the input can only have one). It all comes down to making the tests as short as possible. With a definitionyou might think we'll be OK. However, there's a problem. If I do
and there is no
e
in the input then TeX will complain as the parameters for\num@auxi
will not match properly. So instead we need to doto make sure that there is always an
e
to find. However, if the input also contains ane
then\num@auxi
ends up splitting so that#1
is the mantissa but#2
is the exponent plus that trailinge
. We could allow for that will a clean-up step, but it's easier to use twoe
tokens and use definitionused as
In this case, if there is no
e
in the input we get#1
as the mantissa, while#2
and#3
are empty. On the other hand, if there was ane
in the input then#1
is the mantissa,#2
is the exponent and#3
mops up the straye
(we always discard#3
). So working this way we need only the one macro to do the work.