5

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$?

xzczd
  • 65,995
  • 9
  • 163
  • 468
lapcal
  • 531
  • 2
  • 7
  • 2
    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. – Domen Jul 29 '23 at 14:18
  • @Domen You can download it from https://www.gust.org.pl/projects/e-foundry/lm-math/download – lapcal Jul 29 '23 at 14:28
  • 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. – Domen Jul 29 '23 at 14:31
  • 1
    @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. – lapcal Jul 29 '23 at 14:39
  • 3
    MaTeX gives $\LaTeX$ style typesetting in Mathematica. I wish WRI would buy this technology from its developer and incorporate it into their next release. http://szhorvat.net/pelican/latex-typesetting-in-mathematica.html – David G. Stork Jul 29 '23 at 16:12
  • @DavidG.Stork Latex is free and open source software. WRI can implement this if they want in one day. They just have to bundle the latex engine as part of Mathematica and use it internally. Mathwork has done this more than 30 years ago in Matlab. see this. Latex is build in Matab. No need to install or do anything by the user. But WRI does not want to do this, because they want people to use Mathematica's own way of doing things when it comes to styling. – Nasser Jul 30 '23 at 07:02
  • Strongly related: https://mathematica.stackexchange.com/q/193668/1871 – xzczd Jul 30 '23 at 07:21
  • What platform wind, Linux or Mac?, – MMA13 Jul 30 '23 at 08:33

4 Answers4

7

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

Domen
  • 23,608
  • 1
  • 27
  • 45
2

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"]

robjohn
  • 1,071
  • 8
  • 18
  • 1
    I thought that posting the code for DispLaTeX was not on-topic here, but I can provide it if desired. – robjohn Jul 30 '23 at 05:14
2

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

xzczd
  • 65,995
  • 9
  • 163
  • 468
1

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!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96