This is comparable to the problem of bundling several key-value arguments (for instance for \includegraphics
) in a macro: At the time of executing (the amsmath version of) \sqrt
, which is now \OldSqrt
, the content of []
is parsed for \leftroot
&c. At that time only \DHLindex
is found, and later \leftroot
is useless.
Changing
\setbox0=\hbox{$#1\OldSqrt[\DHLindex]{#2\,}$}\dimen0=\ht0\relax%
to
\setbox0=\hbox{$#1\expandafter\OldSqrt\expandafter[\DHLindex]{#2\,}$}\dimen0=\ht0\relax%
makes the \leftroot
visible and the first attempt goes through.
Dunno whether it does what you want though.
This should also work for attempt 2. Didn't look at attempt 3.
EDIT: What exactly do you want to know about the \DHLhksqrt
macro? AFAICS it takes two arguments, builds a root expression from them, which it measures and tries (with debatable success) to decorate with an additional vertical rule.
EDIT2: To make this look a bit better, at least \DHLhksqrt
needs to take into account when the root index is moved above the root symbol, otherwise the additional rule is placed too high.
EDIT3: Ok, here's my take at a correction for the displacement problem. The root is formatted twice, so there might be a performance problem.
\def\DHLhksqrt#1#2{%
\setbox0=\hbox{$#1\OldSqrt{#2\,}$}\dimen0=\ht0\relax%
\advance\dimen0-0.2\ht0\relax%
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\hbox{$#1\expandafter\OldSqrt\expandafter[\DHLindex]{#2\,}$}\lower0.4pt\box2}%
}
EDIT4: Unfortunately the root symbol has more whitespace above in bold math mode. Look at
\fboxsep0pt
\fbox{$f(x) = \OldSqrt{e^{2x}}$}
\fbox{\boldmath$ f(x) = \OldSqrt{e^{2x}}$}
![root example](https://i.stack.imgur.com/LSUij.png)
Hence, this has to be considered when placing the "closing rule":
\makeatletter
\newcommand*\bold@name{bold}
\def\DHLhksqrt#1#2{%
\setbox0=\hbox{$#1\OldSqrt{#2\,}$}\dimen0=\ht0\relax%
\advance\dimen0-0.2\ht0\relax%
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{%
\hbox{$#1\expandafter\OldSqrt\expandafter[\DHLindex]{#2\,}$}%
\lower\ifx\math@version\bold@name0.6pt\else0.4pt\fi\box2%
}%
}
\makeatother
![bold root](https://i.stack.imgur.com/BR2r7.png)
The effect depends a lot on the viewer, I just tried your file and it's more noticeable in xpdf than acrobat, but in both it comes and goes to a certain extent at different zoom levels.
It's hard to make the two parts exactly match at all resolutions as the left hand part is made of one or more characters from the font so is subject to the renderer's font hinting and anti-aliasing (note a slight fuzziness in the small horizontal segment in the left of your image) But the horizontal rule is drawn as a rule not using a font character so goes through a different code path in the renderer. This means that even if they are specified to be exactly the same width and alignment, then at certain resolutions they may snap to different pixel boundaries.
Best Answer