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
Depends on the command. The way arguments are parsed is command specific, not TeX/LaTeX imposed.
Some commands expect optional arguments and then parse the square brackets as the start of optional arguments. Other commands expect no optional arguments and then a square bracket is just an argument.
See these examples:
\documentclass{article}
\usepackage{xparse}
\NewDocumentCommand{\mycmd}{o o m}{\IfValueTF{#1}{first #1}{nothing on one},
\IfValueTF{#2}{second #2}{nothing on two},
mandatory #3}
\newcommand\anothercmd[1]{My argument is /#1/}
\begin{document}
\mycmd{Hello}
\mycmd[1]{Hello}
\mycmd[1][2]{Hello}
\anothercmd{test}
\anothercmd[2]{Test}
\end{document}
Best Answer
The
\ProcessList
functionality is experimental and suggests that the macro takes a single argument. One way around this is to define\func
within\boxed
so it grabs the arguments: