3

When I try to format a number that has a big exponent with TeXForm the following happens:

In[1]:= TeXForm[1.543*10^-10 ]

Out[1]//TeXForm=
    \text{1.543$\grave{ }$*${}^{\wedge}$-10}

But I would like to get something like 1.543 \cdot 10^{-10}. Is there any workaround for this?

Wauzl
  • 31
  • 2

3 Answers3

5

You could use ScientificForm[] for starters:

TeXForm[ScientificForm[1.543*10^-10]]
   1.543\times 10^{-10}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
5

Here is another approach that addresses some issues you may (or may not) encounter: With the built-in formats like ScientificForm, there is an aesthetic problem:

TeXForm[ScientificForm[1.*10^-10]]

1.\times 10^{-10}

Notice that trailing decimal points are printed after machine-precision numbers even when there's no digits to the right.

Also, you wanted a centered dot to represent the multiplication. Both of these issues can be addressed with the following wrapper function:

tF[x_?MachineNumberQ] := 
 TeXForm@NumberForm[x, 
   NumberFormat -> (DisplayForm@
       RowBox[Join[{StringTrim[#1, RegularExpression["\\.$"]]}, 
         If[#3 =!= "", {"\[CenterDot]", 
           SuperscriptBox[#2, #3]}, {}]]] &)]

An example is:

tF[1.34 10^-10]

1.34\cdot 10^{-10}

A slightly different way to implement this would be to declare this as a Format that only kicks in during TeXForm output:

Format[tN[x_?MachineNumberQ], TeXForm] := 
 NumberForm[x, 
  NumberFormat -> (DisplayForm@
      RowBox[Join[{StringTrim[#1, RegularExpression["\\.$"]]}, 
        If[#3 =!= "", {"\[CenterDot]", 
          SuperscriptBox[#2, #3]}, {}]]] &)]

Format[tN[x_]] := x

With this, you could wrap any expression you want with the tN function, and it will display as if tN weren't there. But if you apply TeXForm, the output is changed:

tN[1. 10^-10]

$1. \times 10^{-10}$

TeXForm[%]

1\cdot 10^{-10}

The centered dot and the removed decimal point can be seen only in the last line where TeXForm was applied.

It depends on the application whether you would want to use a wrapper with a Format like tN, or a simple conversion function like tF.

The formatting in these wrapper functions relies on NumberForm, and I basically borrowed the function trimPoint from my answer here.

Jens
  • 97,245
  • 7
  • 213
  • 499
3

The answer by @Jens should be good enough, but unfortunately defining TeXForm formats using Format works only at the top level. For instance:

tN[1.543*^-10] //TeXForm

$1.543\cdot 10^{-10}$

works well, while:

{tN[1.543*^-10]} //TeXForm

$\{\text{1.543$\grave{ }$*${}^{\wedge}$-10}\}$

does not. One can instead define TraditionalForm MakeBoxes rules:

Clear[tN];
tN /: MakeBoxes[tN[x_Real], TraditionalForm] := MakeBoxes[
    NumberForm[
        x,
        NumberMultiplier -> "\[CenterDot]"
    ],
    TraditionalForm
]

Then:

tN[1.543*^-10] //TeXForm

$1.543\cdot 10^{-10}$

and

{tN[1.543*^-10]} //TeXForm

$\left\{1.543\cdot 10^{-10}\right\}$

both work as expected.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355