I have some numbers output from Matlab and I want to show them as tables in LaTeX. The numbers are in the form 0.14190949656253118000 or 0.01234e-3 and I want to show them in scientific notation. How can I find/write, a macro that is called like \scn{0.14190949656253118000} or \scn{0.01234e-3} and results in $1.41x10^{-1}$ or $1.23x10^{-5}$. Note that the number of digits after the decimal points should be configable.
Asked
Active
Viewed 5.2k times
20
Helium
- 2,493
2 Answers
21
Working from Boris' answer:
\documentclass{article}
\usepackage[scientific-notation=true]{siunitx}
\listfiles
\begin{document}
%\num{0.14190949656253118000}
\num{0.1419094965}
\end{document}

However, for your particular number, siunitx versions lower than 2.4n return a "number too big" error unless you round the number off (as you had originally indicated, but I forgot about). That prompted me to write the following MATLAB function that will print the LaTeX code for your numbers to whatever precision you want:
function s=pp(x,n)
% pretty-print a value in scientific notation
% usage: s=pp(x,n)
% where: x a floating-point value
% n is the number of decimal places desired
% s is the string representation of x with n decimal places, and
% exponent k
exponent=floor(log10(abs(x))); %to accomodate for negative values
mantissa=x/(10^exponent);
s=sprintf('$%*.*f \\times 10^{%d}$',n+3,n,mantissa,exponent);
% returns something like '$1.42 \times 10^{-1}$'
Examples:
>> s1=pp(0.14190949656253118000,2)
s1 =
$1.42 \times 10^{-1}$
>> s2=pp(0.01234e-3,3)
s2 =
$1.234 \times 10^{-5}$
But again, any recent version of siunitx and Boris' answer will work fine for rounded numbers, and siunitx 2.4n will work fine even without rounding.
Mike Renfro
- 20,550
20
The macro \num from, e.g. siunitx does just this. For example
\documentclass{article}
\pagestyle{empty}
\usepackage{siunitx}
\begin{document}
\num[round-precision=2,round-mode=figures]{0.1419094};
\num[round-precision=2,round-mode=figures,
scientific-notation=true]{0.1419094};
\num[round-precision=2,round-mode=figures,
scientific-notation=true]{0.1419094e-6};
\num[round-precision=2,round-mode=figures,
scientific-notation=engineering]{0.1419094e-6}
\end{document}

Note that too many digits in the input may lead to error "Number is too big".
Also, the optional arguments in \num can be set for all numbers using \sisetup.
Boris
- 38,129
-
2It would be better to show a complete example. By default
\num{0.14190949656253118000}produces0.141 909 496 562 531 180 00}– Peter Grill Apr 04 '12 at 00:47 -
-
Thanks Boris, I just defined a macro like this
\newcommand{\sn}[1]{\num[round-precision=2,round-mode=figures,scientific-notation=true]{#1}}since I want to make the numbers consistent throughout the document. – Helium Apr 04 '12 at 02:39 -
9easier to say
\sisetup{round-precision=2,round-mode=figures,scientific-notation=true}, and then just user\num. – Boris Apr 04 '12 at 03:03
siunitxdo you have? You should not get the 'too big' error with the latest release. – Joseph Wright Apr 04 '12 at 07:29