20

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.

2 Answers 2

21

Working from Boris' answer:

\documentclass{article}
\usepackage[scientific-notation=true]{siunitx}
\listfiles
\begin{document}

%\num{0.14190949656253118000}
\num{0.1419094965}

\end{document}

enter image description here

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.

6
  • Which version of siunitx do you have? You should not get the 'too big' error with the latest release.
    – Joseph Wright
    Commented Apr 4, 2012 at 7:29
  • I'll update and check again. Commented Apr 4, 2012 at 11:52
  • Updated from CTAN, got siunitx 2.4l, same problem. Updating answer with new details. Commented Apr 4, 2012 at 12:52
  • Ah, that's not quite what I tried :-) The question indicates that the output should be rounded (which was where there used to be a bug with the number of digits in the mantissa!). So I both rounded and forced scientific notation, which works fine. I will investigate this other issue when I get a moment.
    – Joseph Wright
    Commented Apr 4, 2012 at 12:59
  • Yep. Forgot the rounding, which Boris remembered to include. Commented Apr 4, 2012 at 13:20
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}

enter image description here

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.

4
  • 2
    It would be better to show a complete example. By default \num{0.14190949656253118000} produces 0.141 909 496 562 531 180 00} Commented Apr 4, 2012 at 0:47
  • ok, added some examples
    – Boris
    Commented Apr 4, 2012 at 1:43
  • 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
    Commented Apr 4, 2012 at 2:39
  • 9
    easier to say \sisetup{round-precision=2,round-mode=figures,scientific-notation=true}, and then just user \num.
    – Boris
    Commented Apr 4, 2012 at 3:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .