Add SymbolElem to improve math character styling and fix $"a"$ vs. $a$#5738
Add SymbolElem to improve math character styling and fix $"a"$ vs. $a$#5738laurmaedje merged 5 commits intotypst:mainfrom
SymbolElem to improve math character styling and fix $"a"$ vs. $a$#5738Conversation
|
Thank you for implementing this! 🎉 NotesI chose the review style of refactoring your PR myself to see what I think should be kept/changed. You can view the end result of it in this branch. (It's all squashed since I'm no jj voodoo master 😅.) Here are the things I think should be changed:
As said, the branch linked above contains the result I ended with, containing all these changes. If you disagree with some of my proposed changes, then I'm curious to hear your thoughts. If not, I don't mind whether one of us pushes the squashed changes to this PR or whether you incorporate them into your cleaner commit history. I assume the individual commits were mostly for review convenience, so maybe it's unnecessary work. Responses
No documentation is necessary. Even though
Not relevant if we transform to
Seems like a nice change in general, but unrelated to the PR. :)
I'm not sure what change you are envisioning, but also probably not tied to this PR.
Having |
|
Thanks! I've incorporated all of your changes 👍
Aha!
I thought there should be a way to do this! Couldn't come up with the right incantation 😁 I rebased onto main and used I did make some changes, but only to comments! You can see them on this commit in my repository (which matches the rebased update exactly). I mostly added more context or fixed typos. If those look good, I'm ready to merge. |
|
Fast-forward merged locally to keep the good commits. :) Thank you!! |
|
As a user affected by #274: very nice, thanks! |
Typst v0.13 slightly reduced the widths of single alphabetic characters in attachments and fractions. In older versions of Typst the width would change between a single character and multiple characters. The current behavior is therefore more consistent! The other cases have a slightly reduced padding between the base unit and the exponent. This is however barely noticeable compared to the first change. I think these changes are related to typst/typst#274 and typst/typst#5738. While the output will be slightly different in older versions of Typst, this is overall not a huge issue.
|
Hi @laurmaedje @wrzian! The description of this PR hints at an upcoming overhaul of how Typst handles math text. I couldn't find another obvious place where ideas are being collected for that overhaul, so I'll include a small note here. If there's a more appropriate place to put this, could you direct me to it? Thanks :) The current math mode behavior of removing spaces between consecutive variables makes typesetting expressions difficult in contexts where juxtaposition indicates some common operation, e.g. function application in lambda calculus or logic, or multiplication in arithmetic. The current workarounds are quite cumbersome, so it would be really nice if the math text overhaul allowed this space-removal between consecutive variables to be turned on or off with a |
Much of the development discussions happen on Discord. There's a dedicated thread for math there. |
|
(I think it's fairly unfortunate that many of the design discussions around Typst are enclosed on Discord. Various people cannot access it easily, and it is not easily searchable by web engines, does not have public archives, etc. This trove of content is going to be lost in a few years when Discord enshittifies in response to the next popular chat app, and the early history of Typst is going to be basically lost.) |
This PR is a compromise to close #274 without doing a full overhaul of how Typst handles math text. That overhaul is still needed, but will happen after a design is ready, which will take some time. This path was agreed on in the call that closed #4638 so that we can get some pragmatic fixes in even if they're temporary.
However this still required many changes, as
SymbolElemsplits all textual content into the two options of itself orTextElem. Getting all the tests to pass was rough, but they now do!I've taken care to separate the changes into discrete chunks, and the test suite should pass on each of the commits.
(random fact: this PR also puts us over 2500 tests in the test suite 🎉)
Commits
1. MathText
Adds the new syntaxkind for math text. I've chosen to use an enum to represent the difference between numbers and characters in the AST.
2. SymbolElem
I added the initial SymbolElem with small hacks so it converts into a TextElem before any other realization (which are reverted in the two layout commits).
I kept SymbolElem separate from the Symbol value to make this PR easier, this required putting it in the outer
symbols.rsfile to avoid duplicate names (Repr). I don't think this is really the right location, but it's the easiest one for now.I'm not sure what we want for the documentation of SymbolElem, I feel like it should be more of a hidden element that isn't exposed to the user for now, and the value type
Symbolshould be the main visible API.3. Using SymbolElem
Here we make use of symbol elem in all the places it should be and I also update the casting rules for
charso that symbols can be used in expected locations, such as matrix delimiters.Notably, the evaluation rules for shorthands and escapes now generate content instead of values.
4. Realization and Markup layout
Here we undo the realization hack and start to layout symbol elems in markup mode.
This also includes a change to textual show rules: if you
showa SymbolElem, it will act like a regex rule, but the synthesized text element that we pass into regex rule recipes has to be updated. If we just pass a TextElem to recipes for all regex rules, then show rules on characters in math will lose their italic styling.Example:
We now pass either a SymbolElem or a TextElem to textual show rule recipes based on these criteria:
I think this is the most intuitive solution, but it does mean that show rules on text can no longer rely on comparing against the
textelement function. Thecontent-fields-complextest exposes this difference and needed to be updated for this.5. Math layout
Here we no longer apply auto-italics to TextElems, but still do for SymbolElem. That should be the only real change, but I also refactored most of the functions in
text.rsin an attempt to improve readability. I'm not sure that I really succeeded, let me know if you have suggestions.One particular edge case was that the
stretch_fragmentlayout function instretch.rsno longer works on character strings, because they now generateFrameFragments instead ofGlyphFragments. I spent a while trying to find a solution. But I couldn't make it work without changing some aspect of the layout.Luckily the workaround is easy: create a SymbolElem by calling
symbol("X"), writing a character escape, or writing the character raw in math. This is the reason for the changes in themath-lr-colorandmath-stretch-shorthandtests.Review
I've done a first pass review myself, but still left a few questions in the code as TODO comments that I would appreciate feedback on. I'll try to be responsive the next few days so we can get this merged.
Let's put #274 to rest!