3
$\begingroup$

MaTeX can convert many data to LaTeX format, such as:

MaTeX[1.26591*^100]

will output

enter image description here

But when the number has a minus of the exponent, MaTeX wouldn't work right. Such as:

MaTeX[1.26591*^-100]

will output

enter image description here

As there are so many numbers with negative exponent, it is not good way to modify them one by one, so how should we do it automatically?

$\endgroup$
2
  • 2
    $\begingroup$ I think the problem is with TeXForm itself and not MaTeX. Compare TeXForm[1.26591*10*^100] output with TeXForm[1.26591*^-100] output and you will see why. screen shot !Mathematica graphics $\endgroup$
    – Nasser
    Commented Jun 3, 2023 at 14:04
  • $\begingroup$ @Nasser Yes! I agreed with you. $\endgroup$
    – Y. zeng
    Commented Jun 4, 2023 at 5:38

2 Answers 2

2
$\begingroup$

Here is a workaround

<< MaTeX`
SetOptions[MaTeX, "Preamble" -> {"\\usepackage{siunitx}"}];
num = 1.26591*^-100;
MaTeX["\\num{" <> ToString@FortranForm[num] <> "}", Magnification -> 2]

Mathematica graphics

If you do not want the group separator space, do

MaTeX["\\num[group-separator =]{" <> ToString@FortranForm[num] <> "}",
  Magnification -> 2]

Mathematica graphics

num = 1.26591*^100
MaTeX["\\num{" <> ToString@FortranForm[num] <> "}", Magnification -> 2]

Mathematica graphics

Another option if you want is to use the FortranForm directly and now you do not have to use the siunitx package.

 MaTeX[FortranForm[1.26591*^-100], Magnification -> 2]

Mathematica graphics

$\endgroup$
2
$\begingroup$

I suggest another method than @Nasser. In practice, there can be several pieces bad-formatted by TeXForm, and we can preprocess the TeX expression by string patterns.

texForm[string_String] :=
    StringReplace[$texRule][string];
texForm[expr_] :=
    StringReplace[$texRule]@ToString@TeXForm[expr];

where $texRule stores the rules like

$texRule = {
    RegularExpression["(\\\\text{)([0-9.]+?)(\\\\$\\\\grave{ }\\\\$\\*\\\\${}\\^{\\\\wedge}\\\\$-)(\\d+?)(})"]->"$2 \\times 10^{-$4} "
};

then using MaTeX we get for example

a+1.26591*^-100 f[x]//texForm//MaTeX

\begin{equation} a+1.26591 \times 10^{-100} f(x) \end{equation}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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