# Automatic solution for nice roots - but problem with nested ones

Please look at the following MWE. I found an automatic solution for nice roots. The depth of letters ist ignored and subscripts do not influence the depth.

But when I nest the roots, it gets ugly. What am I doing wrong? What has to be done for nice nested roots?

Thanks.

Sorry for using \over, but I usually use Plain (Lua-)TeX. But loading OTF fonts with low level commands would give many extra lines in the code, so I write this MWE with LuaLaTeX and Unicode Math.

% Only for LuaTeX
\documentclass[17pt]{extarticle}
\usepackage[math-style=upright]{unicode-math}
\setmainfont{lmroman10-regular.otf}
\setmathfont{latinmodern-math.otf}

\begin{document}

\makeatletter
\newbox\r@@tbox
\newdimen\r@dic@ldepth

% to get cramped style
\def\mathpalettecramped#1#2{\mathchoice{#1\crampeddisplaystyle{#2}}%
{#1\crampedtextstyle{#2}}{#1\crampedscriptstyle{#2}}%
{#1\crampedscriptscriptstyle{#2}}}

\protected\def\sqrt{\mathpalettecramped\printr@@t}

\def\printr@@t#1#2{\begingroup
\Umathcodenumj=i % example for goobling depth of letters
\setbox\z@\hbox{\scantokens{\catcode\_=\active \let_=\@gobble
$\m@th#1{\vphantom{0}#2}$}} % Subscripts do not influence depth
\global\r@dic@ldepth\dp\z@
\endgroup
\setbox\r@@tbox\hbox{$\m@th#1{\vphantom{0}#2}$}
\dp\r@@tbox\r@dic@ldepth

\section{Good}
This works fine for not nested roots:
$\sqrt A \sqrt j$, $\sqrt{Aj}$, $\sqrt{1_{1_1}\over Aj_{A_A}}$

However, there is a problem with nested roots:
$\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{11}}}}}$

The base of the root signs alternates and the radical rules
are vertically spaced unequal. What am I doing wrong?

\end{document}

•  \global looks suspicious in anything that can be nested – David Carlisle Jul 16 '19 at 22:33

\expandafter\endgroup

• @Sebastiano it's nothing to do with roots, as I commented under the question anything using \global` is likely to fail in a nested context as global definitions do not obey the nested scope. – David Carlisle Jul 16 '19 at 22:44