[Tex/LaTex] bold exponent in siunitx

boldsiunitx

I have a large table of numbers generated from a script, and I use siunitx to format the output nicely. I would like to highlight the important part of the numbers, which is the exponent, in bold, so that it stands out (the number itself is not as important as the order of magnitude, yet scientific notation makes the exponent less visible, as superscript).

I'm using these settings

\usepackage{siunitx}
\sisetup{
         exponent-product = \cdot,
         round-mode = figures,
         round-precision = 1}

\num{2.12e-14}

enter image description here

but I would prefer the visual equivalent of

$2\cdot 10^{\mathbf{-14}}$

Edit: For a real-world example, see the two large tables in the Appendix of this pdf document

enter image description here

Best Answer

If you can use LuaLaTeX instead of pdfLaTeX, it may be easier to just create a dedicated Lua function and set up an associated TeX "wrapper macro" rather than to try to hack the \num macro of the siunitx package. In the example below, the wrapper macro is called \mynum, and its syntax is set up to be mimic the behavior of the \num macro.

The only assumption that's made about about the numbers to be formatted is that their exponent part is non-empty, i.e., that the numbers contain an e<nn> substring, where <nn> is a positive or negative integer. With this setup, a number such as e1234 is a valid input for \mynum (and for \num too).

enter image description here

\documentclass{article}

\usepackage{siunitx}
\sisetup{exponent-product = \cdot,
         round-mode       = places}

%% Lua-side code
\usepackage{luacode}
\begin{luacode}
function formatnum ( prec, num )
if string.find ( num , "[%d%.]+[eE]" ) then
   mant = string.format( "%."..prec.."f".."\\cdot", string.match ( num, "[%-%+]?[%d%.]+" ) )
else 
   mant = ""
end
expo = string.gsub ( num, ".-[eE]([%-%+]?)(%d+)", "10^{%1\\mathbf{%2}}" )
return ( tex.sprint( "$"..mant..expo.."$" ))
end
\end{luacode}
%% TeX-side code
\newcommand\mynum[2]{\directlua{ formatnum ( \luastring{#1}, \luastring{#2})}}

\begin{document}
\def\NumA{2.12e-14}
\def\NumB{12.34e56}
\def\NumC{e1234}

\num[round-precision=0]{\NumA}

\mynum{0}{\NumA}

\medskip
\num[round-precision=2]{\NumB}

\mynum{2}{\NumB}

\medskip
\num[round-precision=4]{\NumC}

\mynum{4}{\NumC}
\end{document}

Addendum: If you wanted to highlight the exponent in red instead of using bold-facing, you'd need to change \\mathbf{%2} in the Lua code to \\color{red}%2. Of course, either the xcolor or color package needs to be loaded in order to get access to the \color macro.

enter image description here

\mynum{0}{2.12e-14}, \mynum{2}{-12.34e56}, \mynum{0}{e1234}