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}