ronkok / Temml

TeX-to-MathML conversion library in JavaScript
https://temml.org/
MIT License
162 stars 12 forks source link

surd too tall in ` \sqrt{|}` in display mode in Firefox #58

Closed hbghlyj closed 8 months ago

hbghlyj commented 9 months ago

Input: \sqrt{|a|}

Display Mode onDisplay Mode off
![image](https://github.com/ronkok/Temml/assets/53823634/04a97515-490e-4a15-9cd9-395867bc9b91) ![image](https://github.com/ronkok/Temml/assets/53823634/e6c9259a-5b0f-4f02-b599-6e691cdb0efa)

I notice that the surd is too tall in display mode.

Also for /:

Input: \sqrt{1/2}

Display Mode onDisplay Mode off
![image](https://github.com/ronkok/Temml/assets/53823634/413e304b-c4ef-46ba-904d-17986b7d0424) ![image](https://github.com/ronkok/Temml/assets/53823634/a57a7642-dda7-4c2b-9ebe-2dcea64c5077)

Browser: Firefox 123.0

hbghlyj commented 9 months ago

If the depth of | is set to 0 using \smash, the surd will have same size as in \sqrt{a}: image

ronkok commented 8 months ago

The various browsers all have (slightly different) problems with radical sizes. The variations are more than I can correct for. So I will take no action on this issue.

If you are interested in more background, keep reading.

TeX sets exponents and subscripts differently when the (exponent|subscript) is under a radical or in a stacked fraction. They are then "cramped", i.e., squeezed together vertically a little more tightly. It turns out that this affects radicals.

Radicals in math fonts come in various sizes. Typically, there is a standard size, for $\sqrt{x}$, then three (fixed) heights in ascending order, and finally a Size4 radical that can be made any height at run time. The creators of math fonts have made the four fixed sizes in heights that fit the heights of some common math expressions, like $\sqrt{f_c'}$ or $\sqrt{\frac{M_i} b}$.

Microsoft Word also cramps exponents and subscripts. In fact, it cramps them more than TeX does. Computer Modern and its clone Latin Modern have radical sizes set to fit TeX and Cambria Math has radical sizes set to fit Word. So the Cambria Math radical sizes are smaller.

Firefox apparently does not cramp exponents or subscripts at all. I don't know exactly how it picks a radical size, but it appears to be doing so with an algorithm slightly different than TeX. I suspect this is an attempt to deal with the lack of cramping, but that is just a guess.

Chromium does cramp exponents and subscripts. It should be able to pick a different cramp dimension for Latin Modern and Cambria Math. That information is supposed to be available in each math font's MATH table. But I get oddly sized radicals from Chromium when using Cambria Math. It's better with Latin Modern, but still not perfect.

I'm not even sure what Safari does.

Taken together, there is too much variation in browsers and in fonts. I can't correct for all of them, and I'm not going to try.

In my own work, I sometimes make use of \smash, just as you do. I can't think of a better alternative.

hbghlyj commented 8 months ago

Summary: Temml user on Firefox can make use of \smash to get smaller surd or use \textstyle\sqrt{|a|} image

hbghlyj commented 8 months ago

Temml user on Firefox can define a textstyle sqrt \tsqrt, like \tfrac is textstyle frac

\sqrt{|a|}
\def\tsqrt{\textstyle\sqrt}
\tsqrt{|a|}

image and image

hbghlyj commented 8 months ago

Tests in LaTeX:

$\sqrt{x^1_1}$
$\sqrt{)}$
$\sqrt{|}$
$\sqrt{/}$

display mode off: the four expressions are of the same height Untitled display mode on: the first expression is taller Untitled

hbghlyj commented 8 months ago

The radical size jumped between 13.8pt and 13.9pt

\sqrt{\rule1pt13.8pt}\sqrt{\rule1pt13.9pt}

image In Firefox the radical size is set at nsMathMLmrootFrame.cpp

  // height(radical) should be >= height(base) + psi + ruleThickness
  nsBoundingMetrics radicalSize;
  mSqrChar.Stretch(this, drawTarget, fontSizeInflation,
                   NS_STRETCH_DIRECTION_VERTICAL, contSize, radicalSize,
                   NS_STRETCH_LARGER,
                   StyleVisibility()->mDirection == StyleDirection::Rtl);
  // radicalSize have changed at this point, and should match with
  // the bounding metrics of the char
  mSqrChar.GetBoundingMetrics(bmSqr);

and nsMathMLChar.cpp

  // Larger: Critical to the sqrt code to ensure that the radical
  // size is tall enough
  bool isLarger = (aHint & (NS_STRETCH_LARGER | NS_STRETCH_LARGEOP)) && a >= b;
hbghlyj commented 8 months ago

Firefox apparently does not cramp exponents or subscripts at all.

I tested rendering in Firefox v123

\sqrt{x^0} x^0

the position of exponent ^0 in \sqrt{x^0} is lower than in x^0. Maybe Firefox does cramp exponents? image the position of exponent ^0 in \sqrt{x^0} is lower than in x^0. Maybe Firefox does cramp exponents?

I found the following code in nsMathMLmrootFrame.cpp

nsMathMLmrootFrame::TransmitAutomaticData() {
  // 1. The REC says:
  //    The <mroot> element increments scriptlevel by 2, and sets displaystyle
  //    to "false", within index, but leaves both attributes unchanged within
  //    base.
  // 2. The TeXbook (Ch 17. p.141) says \sqrt is compressed
  UpdatePresentationDataFromChildAt(1, 1, NS_MATHML_COMPRESSED,
                                    NS_MATHML_COMPRESSED);
  UpdatePresentationDataFromChildAt(0, 0, NS_MATHML_COMPRESSED,
                                    NS_MATHML_COMPRESSED);

  PropagateFrameFlagFor(mFrames.LastChild(), NS_FRAME_MATHML_SCRIPT_DESCENDANT);

  return NS_OK;
}