You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Single letter strings get parsed as italicized in math mode #274
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)
Reacted by Jakob Krell, SeniorMars, Yingjie Wang, ᡥᠠᡳᡤᡳᠶᠠ ᡥᠠᠯᠠ·ᠨᡝᡴᠣ 猫, Tesla Zhang, Matthew Peterson, Ludwig Austermann, Jorma17, binarynoise, Daniel Csillag and 46 more
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.
Reacted by ᡥᠠᡳᡤᡳᠶᠠ ᡥᠠᠯᠠ·ᠨᡝᡴᠣ 猫, Tesla Zhang, Jorma17, srich387, Rubem Vasconcelos Pacelli, Heda Chen, 梦飞翔, Andrew S. Rosen, Кочев Владислав, SridharRamesh and 3 more
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
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.
Reacted by Daniel Csillag, mbaz, Rubem Vasconcelos Pacelli and Andrew S. Rosen
Reacted by Rubem Vasconcelos Pacelli and Andrew S. Rosen
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 ...
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
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)
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.
implExprExtfor ast::Expr<'_>{fneval_display(&self,vm:&mutVm) -> 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")
Reacted by Wenzhuo Liu, Garmelon, ᡥᠠᡳᡤᡳᠶᠠ ᡥᠠᠯᠠ·ᠨᡝᡴᠣ 猫, devgioele and Sierra Tang
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.
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.
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.
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)).
Activity
jakobjpeters commentedon Mar 25, 2023
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.
GaugeAndGravity commentedon Mar 27, 2023
Find the same issue when I was trying
#let dd="d"for differential and felt surprised on this behavior...dccsillag commentedon Apr 8, 2023
BTW, for differential you can just use
dif.mattfbacon commentedon Apr 8, 2023
FYI
typst/library/src/math/ctx.rs
Lines 132 to 146 in 94e052b
thinety commentedon Apr 8, 2023
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 thekin italic. The alternatives are$R = 50 med upright(k) Omega$orbinarynoise commentedon Jun 14, 2023
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.ModProg commentedon Jun 19, 2023
I agree with the first half, but having the non
#variables in math mode are quite nice if you have a bunch ofarg max sum ...[-]Single letter strings still get parsed as italicized in math mode[/-][+]Single letter strings get parsed as italicized in math mode[/+]Enter-tainer commentedon Apr 4, 2024
A more recent pointer
typst/crates/typst/src/math/ctx.rs
Line 193 in ffc9570
Leedehai commentedon Apr 4, 2024
It looks like a bug, but curiously the current behavior does not break what the doc (https://typst.app/docs/reference/math/) says:
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:
So.. do we want this? Bringing to @laurmaedje 's attention.
More on impl:
Inside math i.e.
$ ... $,"a"and"aa"are parsed asSyntaxKind::Strand thusValue::Str, and an unquotedvis aValue::Contentholding aText(v), so Typst originally has enough knowledge to renderaandvdifferently. However, in math's eval phase,ExprExt::display_ext()callsValue::display(), which converted bothValues toContents that are downcastable toPacked<TextElem>, effectively erasing this distinction. Therefore, whenMathContext::layout_text()runs withPacked<TextElem>, it doesn't know single-letteraandvactually came from different syntax/value kinds.One idea is that during the math eval phase, when
Value::display()constructs theTextElemfromValue::Str, it should specifically request a normal font style. However, it's not trivial to do so becauseTextElem::style: FontStyleis 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)
Leedehai commentedon Apr 5, 2024
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 renderaandvdifferently" right before the knowledge is erased.Result:
(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")
laurmaedje commentedon Apr 5, 2024
@Leedehai Forcing upright at that point would unfortunately break the
italicfunction:(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.
Leedehai commentedon Apr 8, 2024
Agreed.
Further, special-casing
Value::Strcould over-apply the italic-to-upright fix, because it turned out the let bindings with strings and function param names are collected asValue::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.
mattfbacon commentedon Apr 8, 2024
Yes, I think there could even be added a new element type
math.litfor these literals which could even facilitate special styling or overriding the default upright style.gasche commentedon Oct 29, 2024
I also hit this issue: I would like
"Foo"and"F"to generate the sameFcharacter in math context, and right now it looks like we have to writeupright(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.
jtchen2k commentedon Jan 20, 2025
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)).Update format test references to Typst v0.13