As mentioned in this answer to Nice-looking p-th roots the amsmath
package provides \leftroot{}
and \uproot{}
which allow you to move the position of the root to the left, and up in small increments. Using this I was able to redefine the \sqrt
as follows with the help of the xparse
and letltxmacro
packages:
\LetLtxMacro{\OldSqrt}{\sqrt}
\RenewDocumentCommand{\sqrt}{O{} O{-2} O{2} m}{\OldSqrt[\leftroot{#2}\uproot{#3}#1]{#4}}%
Now, the redefined \sqrt
accepts two additional optional parameters to apply if tweaking is necessary as illustrated in this answer to Optional arguments in \def (\sqrt
).
A related question defined how to produce a "Closed" (square) root symbol. So, I attempted to extend egreg's solution as above to allow for two additional parameters to specify the numbers to be used for the \leftroot{}
and \uproot{}
.
When I started I thought this would be straight forward, but this was not the case. The failed attempts are below. The first two yield:
Package amsmath Error: Invalid use of
\leftroot
.
and the third (which shows that I don't quite understand the \DHLhksqrt
macros works) results in:
Argument of
\DHLhksqrt
has an extra }
Questions:
- How to create a closed
\sqrt
symbol which allows for three optional parameters: the root index, the\leftroot
value, and the\uproot
value? - Bonus: How does the
\DHLhksqrt
macro works
References:
Code:
\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{letltxmacro}
\LetLtxMacro{\OldSqrt}{\sqrt}
% Adapted from https://tex.stackexchange.com/questions/29834/closed-square-root-symbol
\newcommand{\ClosedSqrt}[1][\hphantom{3}]{\def\DHLindex{#1}\mathpalette\DHLhksqrt}%
\def\DHLhksqrt#1#2{%
\setbox0=\hbox{$#1\OldSqrt[\DHLindex]{#2\,}$}\dimen0=\ht0\relax%
\advance\dimen0-0.2\ht0\relax%
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}%
}
%% Attempt 1: Package amsmath Error: Invalid use of `\leftroot`.
\RenewDocumentCommand{\sqrt}{O{\hphantom{3}} O{-2} O{2} m}{\ClosedSqrt[\leftroot{#2}\uproot{#3}#1]{#4}}%
%%% Attempt 2: Package amsmath Error: Invalid use of `\leftroot`.
%\RenewDocumentCommand{\sqrt}{O{\hphantom{3}} O{-2} O{2} m}{\def\DHLindex{\leftroot{#2}\uproot{#3}#1}\mathpalette\DHLhksqrt}%
%\def\DHLhksqrt#1#2{%
% \setbox0=\hbox{$#1\OldSqrt[\DHLindex]{#2\,}$}\dimen0=\ht0\relax%
% \advance\dimen0-0.2\ht0\relax%
% \setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
% {\box0\lower0.4pt\box2}%
%}%
%%% Attempt 3: Argument of `\DHLhksqrt` has an extra }
%\RenewDocumentCommand{\sqrt}{O{\hphantom{3}} O{-2} O{2} m}{\def\DHLindex{#1}\mathpalette\DHLhksqrt}%
%\def\DHLhksqrt#1#2#3#4{%
% \setbox0=\hbox{$#1\OldSqrt[\leftroot{#2}\uproot{#3}\DHLindex]{#4\,}$}\dimen0=\ht0\relax%
% \advance\dimen0-0.2\ht0\relax%
% \setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
% {\box0\lower0.4pt\box2}%
%}%
\begin{document}\noindent
\[2 \sqrt{k} \]
\[\sqrt[\beta]{k} \quad\sqrt[\beta][-3][3]{k} \]
\[\sqrt[\beta]{\frac{k}{h}} \quad\sqrt[\beta][-2][6]{\frac{k}{h}}\]
\end{document}
Best Answer
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
to
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.
EDIT4: Unfortunately the root symbol has more whitespace above in bold math mode. Look at
Hence, this has to be considered when placing the "closing rule":