Skip to content

Single letter strings get parsed as italicized in math mode #274

Closed

Description

@SekoiaTree
#let q1="q"
#let q2="qq"
$ q q1 q2 "qq" $

image

It seems strange to me that "q" is rendered the same as q. Currently, this can technically be worked around with "q ", but that adds a space (not particularly different, but still slightly)

Activity

  1. jakobjpeters commented on Mar 25, 2023

    @jakobjpeters
    Contributor

    I agree that it is unexpected and inconsistent. Since there is already a way to create the italic character (no quotes), it would be better to make characters inside quotes behave consistently.

  2. GaugeAndGravity commented on Mar 27, 2023

    @GaugeAndGravity

    Find the same issue when I was trying #let dd="d" for differential and felt surprised on this behavior...

  3. added
    math
    on Apr 1, 2023
  4. dccsillag commented on Apr 8, 2023

    @dccsillag
    Contributor

    Find the same issue when I was trying #let dd="d" for differential and felt surprised on this behavior...

    BTW, for differential you can just use dif.

  5. mattfbacon commented on Apr 8, 2023

    @mattfbacon
    Contributor

    FYI

    let fragment = if let Some(glyph) = chars
    .next()
    .filter(|_| chars.next().is_none())
    .map(|c| self.style.styled_char(c))
    .and_then(|c| GlyphFragment::try_new(self, c, span))
    {
    // A single letter that is available in the math font.
    if self.style.size == MathSize::Display
    && glyph.class == Some(MathClass::Large)
    {
    let height = scaled!(self, display_operator_min_height);
    glyph.stretch_vertical(self, height, Abs::zero()).into()
    } else {
    glyph.into()
    }

  6. thinety commented on Apr 8, 2023

    @thinety
    Contributor

    I would like to add that I was also surprised by this behavior when trying to write units that use special symbols / Greek letters: $R = 50 med "k" Omega$ displays the k in italic. The alternatives are $R = 50 med upright(k) Omega$ or

    #let kohm = "k" + sym.Omega
    $R = 50 med kohm$
    
  7. binarynoise commented on Jun 14, 2023

    @binarynoise

    If we said, ok, text in quotes is upright, text without quotes is italic (or vice versa) and text with a # prepended is a variable, this would clarify things and also improve the two-letter unit parsed as variable situation where it at least still compiles but just looks wrong instead of refusing to compile.

  8. ModProg commented on Jun 19, 2023

    @ModProg
    Contributor

    If we said, ok, text in quotes is upright, text without quotes is italic (or vice versa) and text with a # prepended is a variable, this would clarify things and also improve the two-letter unit parsed as variable situation where it at least still compiles but just looks wrong instead of refusing to compile.

    I agree with the first half, but having the non # variables in math mode are quite nice if you have a bunch of arg max sum ...

  9. changed the title [-]Single letter strings still get parsed as italicized in math mode[/-] [+]Single letter strings get parsed as italicized in math mode[/+] on Nov 5, 2023
  10. added
    bug
    on Nov 5, 2023
  11. Enter-tainer commented on Apr 4, 2024

    @Enter-tainer
    Contributor

    A more recent pointer

    pub fn layout_text(

  12. Leedehai commented on Apr 4, 2024

    @Leedehai
    Contributor

    It looks like a bug, but curiously the current behavior does not break what the doc (https://typst.app/docs/reference/math/) says:

    In math, single letters are always displayed as is. Multiple letters,
    however, are interpreted as variables and functions. To display multiple letters
    verbatim, you can place them into quotes and to access
    single letter variables, you can use the hash syntax.
    

    If we want to display quoted single letter as normal text too, as suggested in this issue (and I agree!), Typst should promise this instead:

    In math, single letters are always displayed as is. Multiple letters,
    however, are interpreted as variables and functions. To display letters
    verbatim like normal text, you can place them into quotes; to access
    single-letter variables, you can use the hash syntax.
    

    So.. do we want this? Bringing to @laurmaedje 's attention.


    More on impl:

    Inside math i.e. $ ... $, "a" and "aa" are parsed as SyntaxKind::Str and thus Value::Str, and an unquoted v is a Value::Content holding a Text(v), so Typst originally has enough knowledge to render a and v differently. However, in math's eval phase, ExprExt::display_ext() calls Value::display(), which converted both Values to Contents that are downcastable to Packed<TextElem>, effectively erasing this distinction. Therefore, when MathContext::layout_text() runs with Packed<TextElem>, it doesn't know single-letter a and v actually came from different syntax/value kinds.

    One idea is that during the math eval phase, when Value::display() constructs the TextElem from Value::Str, it should specifically request a normal font style. However, it's not trivial to do so because TextElem::style: FontStyle is a #[ghost] field, which "cannot be set on an individual instantiated element, and must be set via the style chain", so this direction looks like a rabbit hole.

    (FYI, #1125 is a larger-scale overhauling FR that is sufficient to address this issue - but not sure if that FR is necessary if we just want to fix this issue)

  13. Leedehai commented on Apr 5, 2024

    @Leedehai
    Contributor

    One idea is that during the math eval phase... this direction looks like a rabbit hole.

    Hmm, not rabbit hole after all? I seem to have made it work by carving out a special case during the math eval phase's eval_display(). This special case utilizes the aforementioned "knowledge to render a and v differently" right before the knowledge is erased.

    impl ExprExt for ast::Expr<'_> {
        fn eval_display(&self, vm: &mut Vm) -> SourceResult<Content> {
            let value = self.eval(vm)?;
            let content = match value {
                Value::Str(_) => {
                    value.display().styled(EquationElem::set_italic(Smart::Custom(false)))
                }
                _ => value.display(),
            };
            Ok(content.spanned(self.span()))
        }
    }

    Result:

    $ 1 "ft" = 0.3048 "m" \
      A = pi r^2 $

    (If this direction is right, it still needs polishing to make the spacing right -- note "0.3048" and "m" are adjacent without a space, unlike "1" and "ft")

  14. laurmaedje commented on Apr 5, 2024

    @laurmaedje
    Member

    @Leedehai Forcing upright at that point would unfortunately break the italic function:

    $ 1 "ft" = 0.3048 "m" \
      A = pi r^2 italic(a + "beta") $

    (Just italic("beta") continues to work because it doesn't use that code path, which would add extra inconsistency.)

    I consider this issue a bug or at least something that's not desirable, but I'm not sure whether we can properly fix it without something like #1125.

  15. Leedehai commented on Apr 8, 2024

    @Leedehai
    Contributor

    Agreed.

    Further, special-casing Value::Str could over-apply the italic-to-upright fix, because it turned out the let bindings with strings and function param names are collected as Value::Strs, too, and thus when they are embedded into $..$ they would be made upright by this fix, which is a mistake.

    Barring an overhaul like #1125, it looks an appropriate limited-in-scope fix should start from the parsing phase. The knowledge of different Value types in the evaluation phase is not sufficient to implement this fix.

  16. mattfbacon commented on Apr 8, 2024

    @mattfbacon
    Contributor

    Yes, I think there could even be added a new element type math.lit for these literals which could even facilitate special styling or overriding the default upright style.

  17. gasche commented on Oct 29, 2024

    @gasche

    I also hit this issue: I would like "Foo" and "F" to generate the same F character in math context, and right now it looks like we have to write upright(F) instead of "F" to get this result.

    My understanding from reading the issue is that this is recognized as a bug, but one that is not so easy to fix. The PR #4638 from @wrzian was submitted just this summer and claims to fix the issue, but it hasn't been reviewed yet. (It is marked in "draft" mode and @wrzian wants to write more documentation.) From a distance, it looks like beginners who are not familiar with the Typst codebase cannot really do much to help currently.

  18. jtchen2k commented on Jan 20, 2025

    @jtchen2k

    I also found that this issue made the output of bold(x) inconsistent with LaTeX's \mathbf{x}. Typst gives a bold, italic x, which is unexpected since we want it to be upright to denote a vector 𝐱. I am currently using a workaround like #let bf(body) = math.bold(math.upright(body)).

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

    bugmath

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions