5
$\begingroup$

I just used Latin Modern Math font to display $\LaTeX$-like formulas in Mathematica. My expected effect is shown as follows:

enter image description here

Using the following code:

Style["f(x)=\!\(\*UnderscriptBox[\(\[Sum]\), \
\(n\)]\)(\!\(\*SuperscriptBox[\(x\), \(n\)]\)/n)", 
 FontFamily -> "Latin Modern Math"]

I get this result:

enter image description here

It doesn't meet my expectation. How can I display formulas in Mathematica like in $\LaTeX$?

$\endgroup$
8
  • 2
    $\begingroup$ Use $FontFamilies to get a list of all font families available. Is "Latin Modern Math" indeed in the list? Perhaps it has a different name. See also MaTeX package. $\endgroup$
    – Domen
    Commented Jul 29, 2023 at 14:18
  • $\begingroup$ @Domen You can download it from gust.org.pl/projects/e-foundry/lm-math/download $\endgroup$
    – lapcal
    Commented Jul 29, 2023 at 14:28
  • $\begingroup$ it doesn't matter if it is on my computer, I am asking whether it shows on your computer. Evalute $FontFamilies and check it, please. $\endgroup$
    – Domen
    Commented Jul 29, 2023 at 14:31
  • 1
    $\begingroup$ @Domen I didn't expect you to ask this low-level question, because installing and checking if fonts are installed is a very basic operation. $\endgroup$
    – lapcal
    Commented Jul 29, 2023 at 14:39
  • 3
    $\begingroup$ MaTeX gives $\LaTeX$ style typesetting in Mathematica. I wish WRI would buy this technology from its developer and incorporate it into their next release. szhorvat.net/pelican/latex-typesetting-in-mathematica.html $\endgroup$ Commented Jul 29, 2023 at 16:12

4 Answers 4

7
$\begingroup$

Practical solution

Just use MaTeX.

Explanation

There is in fact—more or less—nothing really wrong with what you see. If you compare the same text to the one written in some other word processor (e.g. Word), you will see that the letter glyphs (though not the math and greek symbols*), such as f, x, and n, are the same:

enter image description here


Why is this not displayed the same as it is in $\LaTeX$ equations, even though it is supposed to be the correct font? Because Latin Modern Math is a "special" font that contains many (4802) different glyphs, and it is not meant to be used as other typical fonts that you download and use to style your text. You can see all of the glyphs by using, for example, an online font viewer (upload the latinmodern-math.otf file). Look, under the usual Unicode code 0066 which stands for f, there is the up-right character f that you see in the output. But the italic "math-like" one is stored under the Unicode code 01D453:

enter image description here

Therefore, to get the "math-like" characters, you have to type them in using their correct Unicode codes:

Style["\|01d453(\|01d465)=\|01d465/\|01d45b", FontFamily -> "Latin Modern Math"]

enter image description here

Using PrivateFontOptions -> {"OperatorSubstitution" -> False} fixes the display of math symbols:

Style["\|01d453(\|01d465)=\|01d465/\|01d45b", 
 FontFamily -> "Latin Modern Math", 
 PrivateFontOptions -> {"OperatorSubstitution" -> False}]

enter image description here


* If you use a text style cell with Latin Modern Math, even the math characters (but not the Greek ones) are rendered correctly:

enter image description here

$\endgroup$
2
$\begingroup$

I have written a Mathematica function (DispLaTeX) that uses ImportString from LaTeX format, and then some postprocessing (converting the Cells, BoxData, and TextData, etc.) It does not use an external library, so it does not produce as nice an output as MaTeX, nor does it implement all of $\LaTeX$, but it is lightweight and does not require installation.

Style[DispLaTeX["$$f\\!(x)=\\sum_n\\left(x^n/n\\right)$$"], FontSize->16, FontFamily->"Latin Modern Math"]

produces

enter image description here

applying InputForm gives

Style[DisplayForm[RowBox[{StyleBox[RowBox[{}]], StyleBox[FormBox[RowBox[{StyleBox["f","I"], "\[NegativeThinSpace]", RowBox[{"(", StyleBox["x","I"],")"}], "\[LongEqual]", UnderscriptBox["\[Sum]", StyleBox["n","I"], LimitsPositioning->False], RowBox[{"(", RowBox[{SuperscriptBox[StyleBox["x","I"],StyleBox["n","I"]], "/", StyleBox["n","I"]}],")"}]}], TraditionalForm]]}]], FontSize -> 16, FontFamily -> "Latin Modern Math"]

$\endgroup$
1
  • 1
    $\begingroup$ I thought that posting the code for DispLaTeX was not on-topic here, but I can provide it if desired. $\endgroup$
    – robjohn
    Commented Jul 30, 2023 at 5:14
2
$\begingroup$

Domen has already given a nice explanation for the topic in his answer, I just want to add that, the scope of influence of the encoding issue seems to be limited to the 52 English letters, in other words, though it's not easy to achieve exactly the same effect as $\LaTeX$ in Mathematica, it's not too hard to make the font right, we just need to make a replacement. Here'a possible implementation:

range = 16^^1d434 + Range[0, 51];
(* Notice the h is special: *)
codelst = ReplacePart[range, {34} -> 16^^210e];

rhs = FromCharacterCode[#] & /@ codelst;

lhs = Flatten@{ToUpperCase[#], #} &@Alphabet[];

rule = Thread[lhs -> rhs]

enter image description here

display = Function[expr, 
   Style[MakeBoxes[expr, TraditionalForm] /. rule // DisplayForm, 
    FontFamily -> "Latin Modern Math"], HoldAll];

display[f[x] = Sum[x^n/n, n]]

enter image description here

You can then make the fraction 1D via manual edit:

enter image description here

$\endgroup$
1
$\begingroup$

Try this:

TraditionalForm[Style[f[x] == HoldForm[
\!\(\*UnderscriptBox[\(\[Sum]\), \(n\)]\)x^n/n], FontFamily -> #, 
    FontSize -> 24, Bold]] & /@ {"Tahoma", "TimesNewRoman", 
  "Helvetica",
  "Times", "Courrier", "Arial", "Veranda"}

enter image description here

Have fun!

$\endgroup$

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