Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 8 down vote accepted

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.

share|improve this answer
    
Which version of siunitx do you have? You should not get the 'too big' error with the latest release. –  Joseph Wright Apr 4 '12 at 7:29
    
I'll update and check again. –  Mike Renfro Apr 4 '12 at 11:52
    
Updated from CTAN, got siunitx 2.4l, same problem. Updating answer with new details. –  Mike Renfro Apr 4 '12 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 Apr 4 '12 at 12:59
    
Yep. Forgot the rounding, which Boris remembered to include. –  Mike Renfro Apr 4 '12 at 13:20
show 1 more comment

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.

share|improve this answer
2  
It would be better to show a complete example. By default \num{0.14190949656253118000} produces 0.141 909 496 562 531 180 00} –  Peter Grill Apr 4 '12 at 0:47
    
ok, added some examples –  Boris Apr 4 '12 at 1:43
    
Thanks Boris, I just defined a macro like this \newcommand{\sn}[1]{\num[round-precision=2,round-mode=figures,scientific-notati‌​on=true]{#1}} since I want to make the numbers consistent throughout the document. –  Mohsen Apr 4 '12 at 2:39
5  
easier to say \sisetup{round-precision=2,round-mode=figures,scientific-notation=true}, and then just user \num. –  Boris Apr 4 '12 at 3:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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