# “Pretty-printing” numbers with significant figures

I like to use LaTeX to display data in figures and tables. I've used arrayjobx.sty and fp.sty to extremely good effect to handle data that comes out of, e.g., Excel or other Windoze-world data handling tools. However, something that I've not been able to achieve so far is rounding to significant figures.

I've come up with a plan (a very good plan!) for taking a given macro which stores a number, and converting that number to scientific notation. But before I go to the trouble of implementing it, I wanted to see if it's already been done. Here's what I envision:

\documentclass{minimal}

\usepackage[E]{sigfigs}  %% conjectural package (will require fp.sty, trimspaces.sty, others)
%% options: E will do <mantissa>E+<exponent>
%%          e will do <mantissa>e+<exponent>
%%          times will do <mantissa>$\times{}10^{<exponent>}$

\usepackage{arrayjobx}
\newarray\MyData
\readarray{MyData}{ 3.14159 & 2077652 & 0.000006543 }

\begin{document}
\setsigfigs{3}
\checkMyData(1)
\sigfigs\cachedata % outputs 3.14

\checkMyData(2)
\sigfigs\cachedata % outputs 2.08E+6

\checkMyData(3)
\sigfigs\cachedata % outputs 6.54E-6

\end{document}

-
Have a look at siunitx –  egreg Apr 18 '11 at 21:34
wow, what a prodigious amount of work went into that package! looks nice –  Brandon Kuczenski Apr 18 '11 at 21:58

To flesh-out an siunitx answer, you might do something like

\documentclass{article}

\usepackage{arrayjobx}
\newarray\MyData
\readarray{MyData}{ 3.14159 & 2077652 & 0.000006543 }

\usepackage{siunitx}
\sisetup{output-exponent-marker = E,round-mode = figures, round-precision = 3,
scientific-notation = true}

\begin{document}

\checkMyData(1)
\num{\cachedata}

\checkMyData(2)
\num{\cachedata}

\checkMyData(3)
\num{\cachedata}

\end{document}


This needs siunitx v2.2, as the output-exponent-marker option is new. (I notice that this points up an issue with negative exponents when not using \times 10^{<exponent>}: I'll take a look at that over the next few days and see what I can do.)

-
The code does not currently have an option to force a + sign in the exponent. If that is of interest, it can of course be added (a good name would be useful!). –  Joseph Wright Apr 19 '11 at 7:25
Actually, I note some inconsistent behaviour with the sign-adding code. I'll probably alter that over the next few days. –  Joseph Wright Apr 19 '11 at 7:41
I’d like to take the opportunity to say that siunitx is one of the most amazing packages ever. It “just works”, is incredibly powerful, highly configurable and has an awesome documentation. Well done! –  Konrad Rudolph Apr 19 '11 at 12:14
Hmm, I don't seem to have expl3 installed, and it looks to be some work to install it. I'll accept the answer pending some time to tend to that and test it :) –  Brandon Kuczenski Apr 20 '11 at 3:56
thank you, works very nicely. Now if I can ever bring myself to elevate my laptop beyond ubuntu jaunty, I might be able to try it at home :) –  Brandon Kuczenski Apr 20 '11 at 18:14

siunitx should have you covered---especially if you are laying out tables of numbers. The S column descriptor provided by this package is absolutely awesome as it allow control over such things as decimal point alignment. Many thanks to Joseph Wright for this package---I have used it to typeset many a lab report!

Thanks to Christian Feuersänger, the latest version of PGF also contains a good handful of functions for pretty-printing numbers in decimal and scientific notation along with the PGFplotstable package which applies these tools to tabular material.

-