Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add math.var so that text in math mode uses the text font#7855

Draft
mkorje wants to merge 2 commits intotypst:mainfrom
mkorje:math-var
Draft

Add math.var so that text in math mode uses the text font#7855
mkorje wants to merge 2 commits intotypst:mainfrom
mkorje:math-var

Conversation

@mkorje
Copy link
Collaborator

@mkorje mkorje commented Feb 13, 2026

The main change is the addition of a math.var element, which single letters symbols in math are automatically wrapped in. As a consequence, string literals ("...") and content blocks (#[...]) in math use the body text font (in particular, equation numbers use the body font).

Q&A

(Subject to change)

  1. How do I set the math font?
    #show math.var: set text(font: "math font")
  2. How do I set the text font in math?
    #show math.equation: set text(font: "text font")

    Note that it defaults to the body text font.

  3. How do I use the body text font in math?
    Use a string literal or content block:
    $ "in text font" #[also in text font] $
  4. How do I type a multi-character variable?
    $ var("blah") $
    // or if you use it often
    #let blah = math.var("blah")
    $ blah $
  5. How do I type an upright variable?
    $ upright(A) var(upright("blah")) $
  6. What is the default font for math.op?
    The body text font - this matches LaTeX.
  7. How do I set the font for math.op?
    #show math.op: set text(font: "font")
  8. How do I use the math font for a specific operator?
    Use math.var with math.op:
    #let lim = math.op(math.var("lim"), limits: true)
  9. How do I (a CJK user) set the font with "latin-in-cjk" covers?
    If you only want to use CJK text in strings and content in math: if you've set the body font to a CJK font everything will just work.
    Otherwise: do the same thing as before (as described here).

Issues

To-Do

  • Discuss remaining failing tests
  • Update docs

@MDLC01
Copy link
Collaborator

MDLC01 commented Feb 13, 2026

It's great to see some movement in this area! Thank you for your continued work on math.

How do I type an upright variable?

Does that mean math.var does not automatically make multi-letter variables upright by default?

What is the default font for math.op?
The body text font - this matches LaTeX.

I there a reason why this is the case, other than being LaTeX's default? To me, it seems that operators are "mathy" enough to be typeset in a math font.

Closes #5081 (no longer applicable)

Why is it no longer applicable? Wouldn't #5081 (comment) reproduce the issue?

@Enivex
Copy link
Collaborator

Enivex commented Feb 13, 2026

I there a reason why this is the case, other than being LaTeX's default? To me, it seems that operators are "mathy" enough to be typeset in a math font.

Math fonts aren't designed to typeset runs of letters

The body text font - this matches LaTeX.

I'm not sure this is quite true. It does use a text font, but I believe the text font used for regular text and math operators can technically be different. Sometimes you want to use a font which more closely matches the actual math font.

@winstontsai
Copy link

6. What is the default font for `math.op`?
   
   The body text font - this matches LaTeX.

Not sure about 6. I think the text used for math operators should correspond more closely to \mathrm in LaTeX or upright in Typst, which is math font. Not the same as body text.

@knuesel
Copy link
Contributor

knuesel commented Feb 16, 2026

How do I type an upright variable?
$ upright(A) var(upright("blah")) $

I suppose upright(var("blah")) will also work? (It seems more logical to me and more consistent with #let lim = math.op(math.var(...)))

@MDLC01
Copy link
Collaborator

MDLC01 commented Feb 16, 2026

I there a reason why this is the case, other than being LaTeX's default? To me, it seems that operators are "mathy" enough to be typeset in a math font.

Math fonts aren't designed to typeset runs of letters

But what if I want to display an operator in bold or sans? Text font aren't designed to typeset mathematical letters. Multi-letter vars are typeset using a math font with this PR so using a math font to display runs of letters will still be common.

Is there a technical reason why math fonts can't typeset runs of letters? Or is it just that ligatures, kerning, and such are often only present in text fonts? If it's the latter, I suspect this would be mainly due to the precedent set by LaTeX and we should just ask math font authors to clone metrics and ligatures from the corresponding text font.

@peng1999
Copy link
Contributor

Not sure about 6. I think the text used for math operators should correspond more closely to \mathrm in LaTeX or upright in Typst, which is math font. Not the same as body text.

\mathrm is usually a text font, not math font. Here is a quick survey of common LaTeX defaults:

Document class Math mode normal font \mathrm font main text roman font
article (using pdfLaTeX) Computer Modern Math fonts: letters=Computer Modern Math Italic (cmm),symbols=Computer Modern Symbols (cmsy),largesymbols=Computer Modern Extended (cmex) Computer Modern Roman (cmr) Computer Modern Roman(cmr)
article + unicode-math (XeLaTeX/LuaLaTeX, default recommended settings) Latin Modern Math Latin Modern Roman Latin Modern Roman
IEEEtran.cls Computer Modern Math fonts, same as article Computer Modern Roman (cmr) Times Roman (ptm)

Therefore, using the body text font for math.op seems a reasonable default, and it is also consistent with how unicode-math treats \mathrm.

@knuesel
Copy link
Contributor

knuesel commented Feb 18, 2026

From this table (especially the last row) it seems that care is usually taken to have the \mathrm font consistent with the math font, irrespective of the main text font, as @Enivex suggested might be done.

So if the user changes the main font, but keeps the default math font (New Computer Modern Math), the best might be to keep New Computer Modern for math text such as operators.

@jhvoigts
Copy link
Contributor

Therefore, using the body text font for math.op seems a reasonable default, and it is also consistent with how unicode-math treats \mathrm.

I am unsure about this. Having a different font family for operators and variables seems very out of place to me. Even in the example you provided, \mathrm always uses a font from the same family, while with IEEEtran.cls, the main text font is a completely different font family.
At least in my quick testing with pdflatex, after loading a different text font, \mathrm matches the operator font but not necessarily the main text font. See this minimal example:

\documentclass{article}

\usepackage{amsmath}
\usepackage{palatino}

\begin{document}
\begin{align*}
	\cos(x) \\
	\mathrm{cos}(x) \\
	\text{cos}(x)
\end{align*}
\end{document}
image

Thus, with Typst's default settings, I would expect that operators and variables use NCM, while text inside math uses Libertinus.

Also, do the CM and LM Math fonts even contain the upright character variants? If not, then that explains why the roman fonts are used.

@knuesel
Copy link
Contributor

knuesel commented Feb 18, 2026

So it looks like we have (without manual override of the math.op font):

  • The font for body text
  • The font for math symbols and multi-letter variables
  • The font for math operators and for text in math

while LaTeX has

  • The font for body text and for text in math (with \text or \textrm)
  • The font for math symbols
  • The font for math multi-letter variables and operators (with \mathrm, typically used also by \DeclareMathOperator)

I think I find the LaTeX division more logical: sentences in math should use the body font, and var("speed") should default to upright and probably the same font as operators. This should be a text font that can be set separately from the body font (though like @MDLC01 I wonder if this could eventually be done with a math font...)

@winstontsai
Copy link

What font would/should numbers use? Should they match the operator font?

@jhvoigts
Copy link
Contributor

What font would/should numbers use? Should they match the operator font?

They should match the math/operator font to remain visually consistent. In my LaTeX example above, 1234 uses CM and \text{1234} uses Palatino.
In general, everything mathy should match while actual text should remain in its own font. That includes text that happens to intertwine with the math.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

7 participants