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}}$}
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
Best Answer
Fragile commands like
\renewcommand
have to be\protect
ed when used in moving arguments like the one of\caption
(see What is the difference between Fragile and Robust commands? for more info).Moreover, you have to add a trailing
%
whenever you don't want to insert extra space (see What is the use of percent signs (%) at the end of lines?).So you should change your definition to