Skip to content

Text in math mode should use text font #366

Open

Description

@Enivex

I became aware of this while trying to understand #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.

Activity

  1. user202729 commented on Mar 27, 2023

    @user202729
    Contributor

    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...

  2. Enivex commented on Mar 27, 2023

    @Enivex
    CollaboratorAuthor

    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.

  3. added
    math
    on Mar 31, 2023
  4. navh commented on Aug 21, 2023

    @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
  5. NoblFriend commented on Aug 22, 2023

    @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}

  6. changed the title [-]Text in math mode is not using the text font[/-] [+]Text in math mode should use text font[/+] on Nov 14, 2023
  7. added
    feature request
    on Nov 14, 2023
  8. memset0 commented on Dec 25, 2023

    @memset0

    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

  9. kwshi commented on Feb 5, 2024

    @kwshi

    Just to chime in-- it looks like this is very related to #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.

  10. Andrew-S-Rosen commented on May 27, 2024

    @Andrew-S-Rosen

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

  11. Enivex commented on May 27, 2024

    @Enivex
    CollaboratorAuthor

    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.

  12. Andrew-S-Rosen commented on May 27, 2024

    @Andrew-S-Rosen

    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.

  13. pammacdotnet commented on Oct 2, 2024

    @pammacdotnet
    Contributor

    Any progress on the original subject (text in math mode should use text font)?

  14. laurmaedje commented on Oct 2, 2024

    @laurmaedje
    Member

    Any progress on the original subject (text in math mode should use text font)?

    If you want to see this fixed, please react with a thumbs-up instead of leaving a comment asking for progress. Otherwise, people will unecessarily be pinged. If you want to be more in the loop regarding general development, feel free to join the Discord server.

  15. iagobaapellaniz commented on Nov 18, 2024

    @iagobaapellaniz
    Contributor

    AFAIK, there are 3 environments in Typst, code, markup, and math. The same way that there are mechanisms to switch environments from

    1. from markup to code # ...
    2. from code to markup [ ... ]
    3. from markup tu math $ ... $
    4. from code to math [ $ ... $ ] (which I like)

    I don't see a clear path

    1. (5) from math to markup " ... " (is it like this?)

    Hence

    1. (6) from math to code should be " # ... "? (I'm confused, even highlighting will fail on this)

    I highlighted the 4 most important switches. Markup environment should be special, and one should be able to go back a forth from and to markup easily, with a simple construction like 1, 2, 3 and 5. The rest can be chained accesses like 4 and possibly 6.

    IMHO, maybe making this type of distinctions in the environments, the behavior will be more expected and consistent, if clearly documented of course. I really like how well documented is the rest, BTW.

    UPDATE: Maybe the solution would be to jump to code first. Since we already allow #x to access valuables that live in code, extending this behavior for all functions and for code expressions with #( ... ), then markup from math could be something like #( [ ... ] ).

  16. 27 remaining items

  17. tmke8 commented on Dec 29, 2025

    @tmke8

    I can live with the fact that $"..."$ corresponds to $\mathrm{...}$ in LaTeX, and I think having \mathrm{} be the easy-to-use default is probably actually the right thing to do, because I see LaTeX users reaching for \text{} often when \mathrm{} would be more appropriate; e.g., they might write D_\text{test} when D_\mathrm{test} is more correct (D_"test" in typst).

    But something like #mtext[], which automatically switches to the current non-math font, should be available for when you write actual prose in a complex equation.

  18. peedromarinhow commented on Jan 27, 2026

    @peedromarinhow

    I think I found a partial solution. It seems like

    #show math.equation: it => {
      let func = $"text"$.body.func()
      show func: set text(font: font-text)
      it
    }

    does the trick. For instance

    And therefore, if $ f(a) > 0 "  and  " f'(a) > 0, $ we have
    $
      // "G L" here would be shown in font-text
      upright(G L)(n, RR)
      =
      {M in RR^(n times n) | M "is invertible"}.
    $
    $ Delta U_"bath" + Delta U + W = 0 $
    $ U = U_"micro,pot" + U_"micro,kin" $

    gets shown as
    Image
    Here's the full code, adapted from the previous example by @kwshi: https://typst.app/project/rfdOwdkOp4Z5UhXWL4Qgxd

    I got this idea after reading this thread: https://forum.typst.app/t/how-to-use-show-set-to-select-non-text-characters-in-math-equation/4782/10.

    Edit: I haven't thoroughly tested this. Also, I should mention that operators such as lim are also affected.

  19. Andrew15-5 commented on Jan 27, 2026

    @Andrew15-5
    Contributor

    func is text. I recently also used show text in a show rule, guess it has at least a few valid use cases.

  20. peedromarinhow commented on Jan 27, 2026

    @peedromarinhow

    Actually, it seems like just

    #show math.equation: it => {
      show math.text: set text(font: font-text)
      it
    }

    is already enough.

  21. Enivex commented on Jan 27, 2026

    @Enivex
    CollaboratorAuthor

    I can live with the fact that $"..."$ corresponds to $\mathrm{...}$ in LaTeX

    Note that /mathrm actually also uses the text font in latex.

  22. tmke8 commented on Jan 27, 2026

    @tmke8

    Note that /mathrm actually also uses the text font in latex.

    Not in classic pdflatex:

    \documentclass{article}
    \usepackage[T1]{fontenc}
    \usepackage{tgbonum} % use TEX Gyre Bonum
    \usepackage{amsmath}
    
    \begin{document}
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    
    $\mathrm{Lorem\ ipsum\ dolor\ sit\ amet,\ consectetur\ adipiscing\ elit.}$
    
    $\text{Lorem ipsum dolor sit amet, consectetur adipiscing elit.}$
    \end{document}

    output:

    Image

    Though maybe this is true for LuaLaTeX and unicode-math.

    EDIT: yeah, for fontspec and unicode-math, I had to do it like this:

    \usepackage{fontspec}
    \usepackage{unicode-math}
    
    \setmainfont{Times New Roman}
    \setmathfont{New Computer Modern Math}
    \setmathrm{New Computer Modern Math} % required for `\mathrm`

    I find it a bit strange that (Xe/Lua)LaTeX went that way...

  23. Enivex commented on Jan 27, 2026

    @Enivex
    CollaboratorAuthor

    Not in classic pdflatex:

    Okay, i should clarify. \mathrm uses the roman text font specified for math.

  24. winstontsai commented on Feb 16, 2026

    @winstontsai

    @peedromarinhow I think you can avoid affecting operators such as lim by setting the font for math.op back to the explicit math font.

    #show math.op: it => {
      show math.text: set text(font: font-math)
      it
    }
  25. laurmaedje commented on Feb 16, 2026

    @laurmaedje
    Member

    The show math.text isn't really doing anything there. In particular, also math.text == text. You'd just write

    #show math.op: set text(font: font-math)
  26. winstontsai commented on Feb 16, 2026

    @winstontsai

    The show math.text isn't really doing anything there. In particular, also math.text == text. You'd just write

    #show math.op: set text(font: font-math)

    @laurmaedje I'm a bit of a noob here but your version doesn't work for me. If I have the following:

    #let font-size = 11pt
    #let font-text = "TeX Gyre Heros"
    #let font-math = "New Computer Modern Math"
    #set text(font: font-text, size: font-size)
    
    #show math.equation: it => {
      show text: set text(font: font-text, size: font-size)
      it
    }
    // VERSION 1
    #show math.op: it => {
      show text: set text(font: font-math, size: font-size)
      it
    }
    // VERSION 2
    // #show math.op: set text(font: font-math, size: font-size)

    Only VERSION 1 works. If I use VERSION 2, there is no change in the operator font.

  27. peedromarinhow commented on Feb 16, 2026

    @peedromarinhow

    @winstontsai That indeed works, but after trying it I found yet another issue. It turns out that

    #show math.text: set text(font: text-font)

    (which btw is equivalent to

    #show math.equation: it => {
      show text: set text(font: font-text, size: font-size)
      it
    }

    ) also affected the numbers. That can be solved by adding

    #show math.text: it => {
      let tostr(it) = if type(it) == str {it}
        else if type(it) != content {str(it)}
        else if it.has("text") {it.text}
        else if it.has("children") {it.children.map(tostr).join()}
        else if it.has("body") {tostr(it.body)}
        else if it == [ ] {" "}
      if tostr(it).match(regex("[0-9]")) != none {
        text(font: math-font, it)
      }
      else {
        text(font: text-font, it)
      }
    }

    but then, since the above rule deals directly with the text element, it is what ultimately sets the style for all text in equations (which removes the need for

    #show math.text: set text(font: text-font)

    ). So adding

    #show math.op: it => {
      show text: set text(font: font-math, size: font-size)
      it
    }

    after has no effect. I haven't found a way to solve this. I'm also worried that this kind of code (in particular the tostr function above) might start to slow down compilation times for larger documents with many equations.

    Btw, I've updated the example I used before to show the code above in action.

  28. laurmaedje commented on Feb 17, 2026

    @laurmaedje
    Member

    Only VERSION 1 works. If I use VERSION 2, there is no change in the operator font.

    That's because you already have this

    #show math.equation: it => {
      show text: set text(font: font-text, size: font-size)
      it
    }

    and the show text: set text for equations will win over show math.op: set text. The effect of show text: set text(..) and set text(..) is not 100% the same w.r.t precedence, but show text: set text(..) is typically not idiomatic. Here, it can make sense as a workaround, but with #7855 this will become less relevant.

  29. NeilGirdhar commented on Mar 10, 2026

    @NeilGirdhar

    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

    I don't know if this is the right metaphor. I think a lot of people write (and LLMs generate) math code like this:

    DIP-VAE replaces global KL scaling with constraints on the aggregate posterior:
    $ q(z) & = Exp_(x ~ p_"data" (dot.op)) [q_phi (z | x)]. $

    So, it seems like double-quoted text should be in the upright math font?

    For text in math mode, I think this is more legible

    $
      Score_Y := pdv(LossFun(Y, tilde(Y)), tilde(Y)) wide #[evaluated at $tilde(Y) = hat(Y)$.]
    $
    

    than

    $
      Score_Y := pdv(LossFun(Y, tilde(Y)), tilde(Y)) wide "evaluated at " tilde(Y) = hat(Y) "."
    $
    

    Thus, in my opinion, Typst is already doing the right thing?

  30. bluss commented on Mar 10, 2026

    @bluss
    Contributor

    @NeilGirdhar #[] is not as it stands a "text mode" for math. It's parsing typst markup as usual but inserting it as content into an equation. Here's an example of when that goes wrong ($X_1$ is the broken example.)

    $ 
      X_1 & #[I am -1 on that idea.] \
      X_2 & #[I am #"-1" on that idea.] \
      X_3 & "I am -1 on that idea." \
    $
    screenshot of rendered equation -1 broken in X_1
  31. NeilGirdhar commented on Mar 13, 2026

    @NeilGirdhar

    #[] is not as it stands a "text mode" for math. It's parsing typst markup as usual but inserting it as content into an equation. Here's an example of when that goes wrong (
    𝑋
    1
    is the broken example.)

    To me, if you're writing sentences like "I am -1 on that idea", then that is logically content. It's not math. According to any modern style guide, in running text, a minus written inline (like “−1”) should be set with either the true minus sign or an en dash rather than the other options you gave.

      X_4 & #[I am −1 on that idea.] \
    

    $X_4$ renders correctly :

    Image

    The tiny dashes for $X_2, X_3$ are not appropriate to indicate minus in text.

    Here's an example of how I would use quoted text in math mode:

    Image

    Here, it's convenient to make the subscripts clearly not scalar indices, but rather names, which means rendering them as text, but ideally in the math font since they should still match the surrounding math.

    Or else, do you have a reference suggesting otherwise?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestmath

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions