Skip to content

Explain how to use multiple fonts in sub/superscript docs - #8095

Merged
laurmaedje merged 5 commits into
typst:mainfrom
YDX-2147483647:docs-super-align
Apr 13, 2026
Merged

laurmaedje merged 5 commits into
typst:mainfrom
YDX-2147483647:docs-super-align

Conversation

@YDX-2147483647

@YDX-2147483647 YDX-2147483647 commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #8090

The baseline and size of sub/superscripts are determined by the font.
If you use multiple fonts with incompatible metrics, then the result will be poor, unless the metrics are overridden.

It looks like a bug, but there's nothing that Typst can do. Therefore, I'm submitting this PR.

About the added example

图片
Outdated screenshots 图片

With set super(…):

图片

Without set super(…):

图片

The example added in this PR is kind of atypical, but I can't come up with better ones.
In the original scenario that triggered this problem, the superscript contained both Chinese and English, so it was natural to use multiple fonts (KaiTi for Chinese and TeX Gyre Termes for English).
However, fonts in typst-assets and typst-dev-assets are quite limited.
It looks like Roboto and Ubuntu are the only two text fonts (excluding math, emoji, and programming fonts) that have metrics incompatible with other fonts.

Fonts in typst-dev-assets and their metrics
fd -t f -x sh -c 'echo ======= {} ======== && ttx -t head -t OS/2 -o - {} | rg "unit|SuperscriptY"' > a.log
======= ./IBMPlexSans-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./IBMPlexSansDevanagari-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./IBMPlexMath-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./IBMPlexSerif-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./CascadiaCode-Regular.ttf ========
    <unitsPerEm value="2048"/>
    <ySuperscriptYSize value="1229"/>
    <ySuperscriptYOffset value="717"/>
======= ./IBMPlexSans-Bold.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./Concrete-Math.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="699"/>
    <ySuperscriptYOffset value="479"/>
======= ./Asana-Math.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="699"/>
    <ySuperscriptYOffset value="360"/>
======= ./Garamond-Math.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="699"/>
    <ySuperscriptYOffset value="479"/>
======= ./IBMPlexSans-Light.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./IBMPlexSans-Medium.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./InriaSerif-BoldItalic.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./IBMPlexSansCondensed-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSansArabic-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./InriaSerif-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./InriaSerif-Bold.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSansThai-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSansSymbols2-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./LibertinusMath-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./InriaSerif-Italic.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSansMath-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSerifCJKjp-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoColorEmoji-Regular-COLR.subset.ttf ========
    <unitsPerEm value="1024"/>
    <ySuperscriptYSize value="614"/>
    <ySuperscriptYOffset value="358"/>
======= ./NotoSerifHebrew-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSerifHebrew-Bold.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSerifCJKtc-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSerifCJKtc-Bold.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSerifCJKsc-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./NotoSerifCJKsc-Bold.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./PTSans-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./Roboto-Regular.ttf ========
    <unitsPerEm value="2048"/>
    <ySuperscriptYSize value="1331"/>
    <ySuperscriptYOffset value="977"/>
======= ./STIXTwoMath-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="500"/>
    <ySuperscriptYOffset value="500"/>
======= ./NotoSerifCJKkr-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./SourceSerif4-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="600"/>
    <ySuperscriptYOffset value="350"/>
======= ./TwitterColorEmoji.ttf ========
    <unitsPerEm value="2048"/>
    <ySuperscriptYSize value="1433"/>
    <ySuperscriptYOffset value="983"/>
======= ./XITSMath-Regular.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="500"/>
    <ySuperscriptYOffset value="500"/>
======= ./texgyrebonum-math.otf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="699"/>
    <ySuperscriptYOffset value="479"/>
======= ./PennstanderMath-Regular.otf ========
    <unitsPerEm value="2000"/>
    <ySuperscriptYSize value="1200"/>
    <ySuperscriptYOffset value="700"/>
======= ./Ubuntu-Regular.ttf ========
    <unitsPerEm value="1000"/>
    <ySuperscriptYSize value="650"/>
    <ySuperscriptYOffset value="477"/>
Fonts in typst-dev-assets and their metrics, organized by AI
unitsPerEm ySuperscriptYSize ySuperscriptYOffset Fonts
1000 600 350 IBMPlexSans-Regular, IBMPlexSansDevanagari-Regular, IBMPlexMath-Regular, IBMPlexSerif-Regular, IBMPlexSans-Bold, IBMPlexSans-Light, IBMPlexSans-Medium, InriaSerif-BoldItalic, IBMPlexSansCondensed-Regular, NotoSansArabic-Regular, InriaSerif-Regular, InriaSerif-Bold, NotoSansThai-Regular, NotoSansSymbols2-Regular, LibertinusMath-Regular, InriaSerif-Italic, NotoSansMath-Regular, NotoSerifCJKjp-Regular, NotoSerifHebrew-Regular, NotoSerifHebrew-Bold, NotoSerifCJKtc-Regular, NotoSerifCJKtc-Bold, NotoSerifCJKsc-Regular, NotoSerifCJKsc-Bold, PTSans-Regular, NotoSerifCJKkr-Regular, SourceSerif4-Regular
1000 699 479 Concrete-Math, Garamond-Math, texgyrebonum-math
1000 699 360 Asana-Math
1000 500 500 STIXTwoMath-Regular, XITSMath-Regular
1000 650 477 Ubuntu-Regular
1024 614 358 NotoColorEmoji-Regular-COLR.subset
2000 1200 700 PennstanderMath-Regular
2048 1229 717 CascadiaCode-Regular
2048 1331 977 Roboto-Regular
2048 1433 983 TwitterColorEmoji

Comment thread crates/typst-library/src/text/shift.rs Resolved
@MDLC01

MDLC01 commented Apr 11, 2026

Copy link
Copy Markdown
Collaborator

I think it would be nice to also show the result without the set rule to help users understand the problem.

@YDX-2147483647

YDX-2147483647 commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor Author

also show the result without the set rule

I update the example, but it might not be what you've expected.
People might rush to the example without reading the explanation. I guess showing 图片 would be more confusing. I add 图片 instead.

@MDLC01

MDLC01 commented Apr 11, 2026

Copy link
Copy Markdown
Collaborator

What about showing the same example, but clearly identifying it as wrong, similar to what is already done in the Accessibility Guide?

@YDX-2147483647

Copy link
Copy Markdown
Contributor Author

How about now?

#let tz(timezone) = text(
  font: "Roboto",
  smallcaps(timezone, all: true),
)

/ Per-font metrics:
  14:00#super[#tz[UTC], not #tz[CET]]

#set super(
  baseline: -0.4em,
  size: 0.6em,
  typographic: false,
)
/ Unified metrics:
  14:00#super[#tz[UTC], not #tz[CET]]
图片

The Accessibility Guide uses ❌ Don't do this / ✅ Do this.
It can't describe what happens here (Do nothing / Do something).
Therefore, I didn't use the exact wording.

@MDLC01

MDLC01 commented Apr 12, 2026

Copy link
Copy Markdown
Collaborator

I think this is great!

@laurmaedje laurmaedje added docs Improvements or additions to documentation's content. waiting-on-review This PR is waiting to be reviewed. labels Apr 13, 2026
@laurmaedje
laurmaedje added this pull request to the merge queue Apr 13, 2026
@laurmaedje

Copy link
Copy Markdown
Member

Thanks, it's indeed nice!

@laurmaedje laurmaedje removed the waiting-on-review This PR is waiting to be reviewed. label Apr 13, 2026
Merged via the queue into typst:main with commit c6f20be Apr 13, 2026
8 checks passed
@YDX-2147483647
YDX-2147483647 deleted the docs-super-align branch April 13, 2026 09:37
gpradofe pushed a commit to gpradofe/typst that referenced this pull request Apr 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation's content.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explain how to use multiple fonts in sub/superscripts

3 participants