How to use (old) Computer Modern for `math.cal`? And why context matters?

My friend wants to use the Computer Modern Math font for math.cal.
We manage to define our own cal as the following, but we encounter issues on the positioning of sub/superscripts. And it’s quite strange that the result changes if we prepend a context.

// Using cmsy10.ttf:
#let cal(s) = text(font: "cmsy10", math.upright(s))

// Using context + cmsy10.ttf:
#let cal(s) = context text(font: "cmsy10", math.upright(s))

$cal(P)_n$ $cal("P")_n$
Standard :white_check_mark: Good :sweat_smile: Bad kerning, but good height
cmsy10.ttf :x: Bad kerning and height :sweat_smile: Bad kerning, but good height
context + cmsy10.ttf :sweat_smile: Bad kerning, but good height :sweat_smile: Bad kerning, but good height
Full code
#let cal(s) = text(font: "cmsy10", math.upright(s))
#let ccal(s) = context cal(s)

#set text(fallback: false)
#set raw(lang: "typc")
#table(
  columns: 3,
  stroke: none,
  align: (end, center, center),

  table.header([], `$cal(P)_n$`, `$cal("P")_n$`),
  table.hline(),
  table.vline(x: 1),

  [Standard], $std.math.cal(P)_n$, $std.math.cal("P")_n$,
  [`cmsy10.ttf`], $cal(P)_n$, $cal("P")_n$,
  [`context` + `cmsy10.ttf`], $ccal(P)_n$, $ccal("P")_n$,
)

Could anyone offer any advice or explanation? Thanks in advance.

Futher info about the font

We mean the the old Computer Modern Math, the default font in LaTeX (without loading new packages like newcomputermodern and unicode-math).

The original Computer Modern Math predates OpenType, and only exists as antique font formats in typical TeX distributions. The matplotlib project has converted the font to OTF.
That is the font file used above, and can be downloaded from lib/matplotlib/mpl-data/fonts/ttf/cmsy10.ttf.

Note that cmsy10.ttf maps cal(P) to P (ASCII P), rather than 𝒫 (Unicode math symbol, U+1D4AB). Therefore, we added upright to our customized cal.


Relates to Is there still difference between `$upright(…)$` and `$"…"$` in v0.14.0?

1 Like

Another friend 请输入密码 remapped the character map and added MathKernInfo & MathItalicsCorrectionInfo. The fixed font resolves all problems.

#show math.equation: set text(font: (
  // Fixed font
  (name: "Computer Modern Symbol", covers: regex("[𝒜ℬ𝒞𝒟ℰℱ𝒢ℋℐ𝒥𝒦ℒℳ𝒩-𝒬ℛ𝒮-𝒵]")),
  "New Computer Modern Math",
))

Full code
#set page(height: auto, width: 240pt, margin: 1em)

#set align(end)

Typst default $cal(K M Z), cal(P)_n, cal(T)^p$

#[
  #let cal(s) = text(font: "cmsy10", math.upright(s))
  Original font  $cal(K M Z), cal(P)_n, cal(T)^p$
]

#[
  #let cal(s) = context text(font: "cmsy10", math.upright(s))
  Original font with context $cal(K M Z), cal(P)_n, cal(T)^p$
]

#[
  #show math.equation: set text(font: (
    (name: "Computer Modern Symbol", covers: regex("[𝒜ℬ𝒞𝒟ℰℱ𝒢ℋℐ𝒥𝒦ℒℳ𝒩-𝒬ℛ𝒮-𝒵]")),
    "New Computer Modern Math",
  ))
  Fixed font $cal(K M Z), cal(P)_n, cal(T)^p$
]

Download CMSY10-fix_cmap_kerning.otf

Recorded in 怎么把 cal 字体变成 LaTeX 里 mathcal 默认的那种? | Typst 中文社区导航.

3 Likes

@Y.D.X If you don’t mind me asking, I am revisiting the kerning issue for Ralph Smith Formal Script (RSFS), from

After asking around, I learned that TeX has a different method for assigning spacing that doesn’t rely on built-in kerning info. This is how RSFS is spaced, since its .afm file didn’t contain any such info (well, it contained one kerning pair).

Since you pulled from matplotlib, I don’t know if it came with any kerning info. If not, how did your friend get the spacing right? Was it just comparing based on sight with a LaTeX document?

Let me ask 请输入密码…

Before he sees it, let me first state what I currently know:

  1. Matplotlib’s version does not have kerning information.
  2. The kerning info in CMSY10-fix_cmap_kerning.otf might have been copied from New Computer Modern Math, or from LaTeX (in a way I don’t know). I remember that he once released both versions and let us compare.

请输入密码 replied:

I might have copied the MathKernInfo from NCMM (New Computern Modern Math) and then made manual adjustments until the visual effect looked fine. The MathItalicsCorrectionInfo was also adjusted manually.

Since Typst extracts many math constants from the main math font, the version I adjusted works only when the main math font is NCMM. This does not guarantee visual compatibility when other fonts are used as main math fonts.

I have not tried to use NCMM + cmsy for LaTeX. I don’t know if it is supported. If it is supported, I don’t know if its kerning is normal. If it is normal, then you can try to see if you can perfectly replicate the effect of LaTeX. I did not pursue this aspect.

(Not an exact quotation. I’ve reformatted.)

2 Likes

Thank you, but also yeash. I didn’t realize this would be such a mess. Guess I’ve got another rabbit hole to go down. Thanks again.

1 Like