Kolaru / MathTeXEngine.jl

A latex math mode engine in pure Julia.
MIT License
97 stars 19 forks source link

[FEATURE]? Is there a way to use different fonts in LaTeXStrings? #53

Open TS-CUBED opened 2 years ago

TS-CUBED commented 2 years ago

LaTeX itself allows to choose fonts for typeset maths. Is there a way to do that in LaTeXStrings / MathTexEngine.jl as well?

Kolaru commented 2 years ago

Currently there is none.

Locating the correct symbols in the font files is done mostly manually, so for a new font we would have to create a mapping of commands to symbols in the font.

So far I have not been able to make sense of what the latex packages are doing to provide new fonts. The ability to adapt fonts from latex packages would be of immense help for that.

What could be possible without too much trouble is changing the font for everything but the maths.

TS-CUBED commented 2 years ago

Thanks. At the moment that latter solution is what I do (Computer Modern for all fonts in Makie). And for the Physics journals that I publish in at the moment, that's ok. But more and more use sans-serif fonts.

Would using the LaTeX maths fonts not mean that all the symbols would be in the same location/encoding within the font?

It is possible in Matplotlib, btw., but that uses a LaTeX engine in the background.

Kolaru commented 2 years ago

Thanks. At the moment that latter solution is what I do (Computer Modern for all fonts in Makie). And for the Physics journals that I publish in at the moment, that's ok. But more and more use sans-serif fonts.

Should the mathematical symbols use sans-serif font in this case too? What I meant there is that in a LaTeX string mixing math and normal text, the text could use the font you need, while still falling back to Computer Modern for the math symbols.

Would using the LaTeX maths fonts not mean that all the symbols would be in the same location/encoding within the font?

Unfortunately, no. Computer Modern (which is also LaTeX default) distributes its symbols over several font files and I don't know if the ordering of it is otherwise standard for LaTeX fonts. It may or may not, the problem being that I find extremly difficult to understand what is going on, and I have not been able to find clear explanation about it (I did not search that much to be fair).

On the other hand, newer system like LuaLaTex uses unicode fonts that should have a standard encoding and ordering, but the system they use to define alternative symbol (e.g. the two different symbols for integral, for inline math vs separate equation) is, as far as I understood, not supported by FreeType which is the backend we use to retrieve symbol informations (and that Makie and maybe Cairo use to render them).

TL DR: Fonts are complicated, and I don't fully understand all the magic LaTeX is doing to make them work.

It is possible in Matplotlib, btw., but that uses a LaTeX engine in the background.

There has been discussion and some test about using a LaTeX engine for rendering the fonts see e.g. https://github.com/JuliaPlots/MakieTeX.jl

I have not followed the exact developpement of the idea, and I am not sure where it actually stands.

marcpabst commented 2 years ago

On the other hand, newer system like LuaLaTex uses unicode fonts that should have a standard encoding and ordering, but the system they use to define alternative symbol (e.g. the two different symbols for integral, for inline math vs separate equation) is, as far as I understood, not supported by FreeType which is the backend we use to retrieve symbol informations (and that Makie and maybe Cairo use to render them).

Hey @Kolaru, Do you have any references for that / any resources on that topic? I'd like to do some digging, but need a place to start :).

Kolaru commented 2 years ago

@marcpabst The problem of grabbing alternative glyph has been solved \o/ (mostly, I still built a manual table to get what I need, but it works)

One place to start to see what kind of unicode font is supported by latex(or by the unicode-math package to be precise, maybe als compatible with LuaLaTeX or XeTeX) is here : https://tex.stackexchange.com/questions/425098/which-opentype-math-fonts-are-available/425099#425099

On master we are currently using the New Computer Modern font listed there.

There seems to be a bit more info about opentype fonts and where to find the math info here : https://tex.stackexchange.com/questions/360406/using-math-fonts-in-lualatex

marcpabst commented 2 years ago

Nice! Is there someone currently working on supporting OpenType math fonts? I'm specifically interested in Fira Math, but I think the mechanics would be the same for all OpenType (math) fonts. If not, I would start looking into it :)

Kolaru commented 2 years ago

The backend used by MathTeXEngine and Makie to handle font is FreeTypeAbstraction.jl. As the name indicates it provides support for FreeType fonts, but there is some level of interop between the two.

As far as I know no one is trying to get a full support for OpenType in julia.

marcpabst commented 2 years ago

I was under the impression that FreeType (I don't think there is a font format of that name btw) had support for OpenType fonts... But I'll play around with FreeTypeAbstraction.jl for a bit, thanks!

Moelf commented 1 year ago

would it be easier to at least support ASCII look up? if finding all symbols is non trivial

Kolaru commented 1 year ago

What do you mean by ASCII look up? Defaut char in case of failure?

This issue is rather frustrating, because the work has been done already for a bunch of fonts (mapping command to glyph and correctly placing them), but I just can't figure out where the info is stored.

If I ever feel really bored, I plan to look at what MathJax is doing, apparently they have something going on (https://docs.mathjax.org/en/latest/output/fonts.html).

Moelf commented 1 year ago

I just mean that I only need mapping for the ascii sets in those other fonts

Kolaru commented 1 year ago

Oh that should already kind-of work, only the placement could be off.

I'll look into that. I suspect the main problem is that the interface to change the font is probably clunky and confusing.

Moelf commented 1 year ago

basically, when publisher requires some font, it's usually about the ASCII, for example they want the "unit" of some value to be in Helvetica. I mean Helvetica doesn't have math stuff anyway, right? (\sqrt{})

jtschneider commented 6 months ago

Sorry for my uninformed and unsolicited comment! I am by no means an expert on fonts. I have tried to skim this discussion to offer a possible step forward if one actually wanted to implement OptenType fonts here:

So from what I gather, the letter that LaTeX renders when using \alpha in a math environment is equivalent to the Unicode definition of mathematical italic small alpha. Note the italic which anyone should recognise who ever used the \mathrm{} command inside the LaTeX math environment.

The unicode-math package has a huge TeX table here that defines the new unicode-math latex command, in the case of \alpha we have \mitalpha (supposing mit stands for mathematical italic) which gets mapped to U+1D6FC as well as the unicode description (mathematical italic small alpha).

The unicode coordinate may then be lookup in the OFT files for a math font, e.g. the brilliant work by the TeX Gyre group and others:

In case of \mathrm{\alpha} one should end up with what unicode calls "Greek Small Letter Alpha" U+03B1, which unicode-math defines as: \mupalpha (I suppose mu stands for mathematical upright) So with this very brief lookup, one may be able to fill out the table for simple characters. However, I am no expert in unicode, and anyone with more experience may chip in from this point:

What I have no clue on are the environment characters such as \sqrt which have not just a character to begin with but also a continuous overbar, but a hint may offer this line. The unicode-math package furthermore offers a different field for each character, which in this case is \mathopen for the root symbol, suggesting that the root here 'starts', but no idea what this actually means. One may (should) dig deeper in reverse direction through the unicode-math package files....

Let me know if there was any useful information in my comment, I hope this may offer an angle of attack to continue the hunt for an automatised lookup table.

Kolaru commented 6 months ago

Sorry for my uninformed and unsolicited comment!

No need to apologize, the point of making the code open source is to get unsolicited comments :)

Finding the unicode-math table is already great!

I did some work on that a while back, but other projects and issues are currently keeping me occupied. I hope to be able to delve into this again soonish.