typst icon indicating copy to clipboard operation
typst copied to clipboard

Text in math mode should use text font

Open Enivex opened this issue 1 year ago • 12 comments

I became aware of this while trying to understand https://github.com/typst/typst/issues/355

If I change the text font to "Comic Neue" (Only to get a distinctly different font)

Hello world, $"Hello world"$, $#text(font: "Comic Neue")[Hello world]$

will render as

image

This is not what I expect. In LaTeX, the input

Hello world, $\text{Hello world}$

will indeed render as

image,

correctly using the text font when displaying text in math mode.


Why is this the correct behavior? Because it is common to write something like

And therefore, if $ f(a) > 0 quad "and" quad f'(a) > 0, $ we have

where the text is part of the, well, text. When this is typeset using the math font it looks very awkward.

image

Obviously this is an extreme example where the text and math fonts are wildly different, but it's still a problem.

Enivex avatar Mar 27 '23 09:03 Enivex

Let's see... if I understood correctly, currently "q" and q is parsed to exactly the same thing in math mode. The option whether or not to use italic font is only determined by the length of the string. #274

You can change math font, but it applies to both math and "text" inside math. #313

Maybe Typst could have an option to set different font for "text" and "math" part inside math mode.

I feel the behavior is intentional? See the section in the tutorial right after

If you want to have a variable that consists of multiple letters, you can enclose it in quotes:

https://typst.app/docs/tutorial/writing-in-typst/


On the other hand currently you can do something like textm("something") to put it in your own font...

user202729 avatar Mar 27 '23 11:03 user202729

It may be intentional, but a multiple-letter variable is very uncommon, and should be the exception rather than the rule. Math fonts are often not even designed for it to be used for text.

It could maybe be argued that it makes sense for "hello world" to be typeset using the math font, but the explicit text("hello world") definitely shouldn't.

Regardless of what the solution is, it should be easy to use the text font in a math environment without having to manually specify the font.

Edit: Another example can be found in the reference https://typst.app/docs/reference/math/ . There, "area" and "radius" should arguably be typeset in the math font, but "is natural" should be typeset in the text font.

Enivex avatar Mar 27 '23 12:03 Enivex

I think this is related

If I create a typst document that only contains: N $N$ $"N"$ $"N."$ the result is a wonky N on the last N.

Screenshot 2023-08-21 at 16 07 19

navh avatar Aug 21 '23 20:08 navh

I think this is related

If I create a typst document that only contains: N $N$ $"N"$ $"N."$ the result is a wonky N on the last N.

Screenshot 2023-08-21 at 16 07 19

if necessary, it can now be "fixed" using the zero-width space: \u{200b}

NoblFriend avatar Aug 22 '23 21:08 NoblFriend

I think this is related If I create a typst document that only contains: N $N$ $"N"$ $"N."$ the result is a wonky N on the last N. Screenshot 2023-08-21 at 16 07 19

if necessary, it can now be "fixed" using the zero-width space: \u{200b}

you can also use upright function if necessary

memset0 avatar Dec 25 '23 12:12 memset0

Just to chime in-- it looks like this is very related to https://github.com/typst/typst/issues/1125.

Just wanted to add that another way to do text inside math is using the #[] content-block syntax:

X = {1, 2, 3} union {#[multiples of $5$]}

This is an example that most closely mirrors how I would use \text{} in LaTeX-- namely, to insert actual plain English text in math mode, and to be able to nest math expressions within text. Here is LaTeX equivalent:

X = \{1, 2, 3\} \cup \{ \text{multiples of \(3\)} \}

In this setting, I very much expect that the text content is actually typeset using text font. Upright math font makes sense for multi-letter math terms and operators, e.g. lim, sin, exp, etc., but in my opinion it absolutely doesn't make sense for rich text content to default to math font.

kwshi avatar Feb 05 '24 04:02 kwshi

Any ideas why "A " converts the A to text font but "B " does not?

Andrew-S-Rosen avatar May 27 '24 19:05 Andrew-S-Rosen

Any ideas why "A " converts the A to text font but "B " does not?

I think you need to be more specific here. Neither are typeset using the text font by default.

Enivex avatar May 27 '24 20:05 Enivex

You're right. I wasn't clear. I was trying to do $ ["A"] $ and noticed that the A was italicized. So, I tried the "hack" of doing $ ["A "] $ and it rendered in a non-italicized style. However, replacing A with B was yielding different behavior (it remained italicized). However, I now realize it was likely a conflict due to the chem-par package.

Andrew-S-Rosen avatar May 27 '24 20:05 Andrew-S-Rosen

I agree with the proposal, although I have some concerns regarding implementation. I certainly would not want math operators to look different than the other math objects, so if the desired math font offers a corresponding text version, there should probably be a way to tell Typst to use it for operators — for example, Libertine for general text, New CM Math for math, and New CM for operators. (It is probably not a good idea to use the upright style of the math font because, as has already been said, math fonts usually are not built to support long strings of characters, e.g. they don't support even standard ligatures.)

(Note that I'm assuming a distinction of operators/variables and math-embedded text as outlined in #1125.)

ZornAC avatar Jun 19 '24 14:06 ZornAC