[Tex/LaTex] “Pretty-printing” numbers with significant figures

calculationsformattingtext manipulation

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}

Best Answer

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.)

Related Question