Skip to content

Problems with \sqrt[...] #2021

@RuixiZhang42

Description

@RuixiZhang42

I was encouraged by Karl Berry to make a report here as well.

The LaTeX syntax \sqrt[...] is essentially a wrap of the plain TeX syntax \root...\of, so it inherits the good and the bad of plain TeX. The following code and description are adapted from my original report.

\documentclass{article}
\setlength{\parindent}{0pt}
\begin{document}
$a+\sqrt[np]2$ has large unwanted space.

$a+\sqrt[]2$ almost has collision versus $a+\sqrt2$.

$\sqrt[3]2$ in the first line%
\cleaders\hbox{.}\hskip\glueexpr\hsize-99.88329pt\relax
$a+\sqrt[3]2+\sqrt[3]4$ in the second line.
\[x=\sqrt[np]{-\frac{q}{2}+\sqrt[]{\frac{q^2}{4}+\frac{p^3}{27}}}%
=\sqrt[np]{2+\sqrt[]{-121}}\]
\end{document}

reveals several problems:

  1. collision between the p and the surd;
  2. too much space in front of the wide radical index;
  3. not enough space in front of the narrow radical index (in the extreme, the empty radical index does not match the behavior of \sqrt);
  4. and even worse, \mkern5mu will disappear at line-breaks, so the surd will protrude the left edge of the text block. It’s impossible to align \sqrt[3]2 to the left.

The faulty code is:

latex2e/base/ltmath.dtx

Lines 241 to 245 in 7b35c82

\def\r@@t#1#2{%
\setbox\z@\hbox{$\m@th#1\sqrtsign{#2}$}%
\dimen@\ht\z@ \advance\dimen@-\dp\z@
\mkern5mu\raise.6\dimen@\copy\rootbox
\mkern-10mu\box\z@}

This was discovered during my on-going investigation into OpenType MATH. OpenType MATH inherited this faulty design by

  • correcting the index raise amount from .6(\ht0-\dp0) to (\ht0+\dp0-padding)*radicalDegreeBottomRaisePercent/100-\dp0+\dp\rootbox;
  • mapping \mkern5mu to radicalKernBeforeDegree;
  • mapping \mkern-10mu to radicalKernAfterDegree.

In turn, the issue slips into MathML Requirements, which affects all math rendering on the web. :(

In my follow-up, I propose abandoning BeforeKern altogether, and making AfterKern proportional to the width of surd instead of constant. This works (surprisingly well):

\documentclass{article}
\setlength{\parindent}{0pt}
\begin{document}
$a+\sqrt[np]2$ has large unwanted space.

$a+\sqrt[]2$ almost has collision versus $a+\sqrt2$.

$\sqrt[3]2$ in the first line%
\cleaders\hbox{.}\hskip\glueexpr\hsize-99.88329pt\relax
$a+\sqrt[3]2+\sqrt[3]4$ in the second line.
\[x=\sqrt[np]{-\frac{q}{2}+\sqrt[]{\frac{q^2}{4}+\frac{p^3}{27}}}%
=\sqrt[np]{2+\sqrt[]{-121}}\]

\makeatletter
\def\r@@t#1#2{\setbox\z@\hbox{$\m@th#1{#2}$}\dimen@ii\wd\z@
  \setbox\z@\hbox{$\m@th#1\sqrtsign{#2}$}\advance\dimen@ii-\wd\z@
  \dimen@ii.66666\dimen@ii % 10mu is about 2/3 of the width of surd
  \dimen@.6\ht\z@
  \advance\dimen@-.4\dp\z@ \advance\dimen@\dp\rootbox
  \advance\dimen@-.6\fontdimen8
   \ifx#1\scriptscriptstyle\scriptscriptfont\else
    \ifx#1\scriptstyle\scriptfont\else\textfont\fi\fi\thr@@
  \raise\dimen@
   \ifdim-\dimen@ii<\wd\rootbox\copy\rootbox
    \else\hbox to-\dimen@ii{\hfil\copy\rootbox}\fi
  \kern\dimen@ii\box\z@}
\makeatother

$a+\sqrt[np]2$ has no large unwanted space.

$a+\sqrt[]2$ has no collision; identical to $a+\sqrt2$.

$\sqrt[3]2$ in the first line%
\cleaders\hbox{.}\hskip\glueexpr\hsize-99.88329pt\relax
$a+\sqrt[3]2+\sqrt[3]4$ in the second line.
\[x=\sqrt[np]{-\frac{q}{2}+\sqrt[]{\frac{q^2}{4}+\frac{p^3}{27}}}%
=\sqrt[np]{2+\sqrt[]{-121}}\]
\end{document}

Edits:

  • \sqrtsign instead of \sqrt;
  • \dimen@ii.66666\dimen@ii (-364084sp) instead of \dimen@ii.66667\dimen@ii (-364092sp), 8sp closer to -10mu (-364080sp).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions