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
\DHLhksqrthas an extra }
Questions:
- How to create a closed
\sqrtsymbol which allows for three optional parameters: the root index, the\leftrootvalue, and the\uprootvalue? - Bonus: How does the
\DHLhksqrtmacro 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}


\leftrootand\uproot. But it messes up the close root symbol (when the root index is too high). See the output for the cases on line 1 with the change to the\setbox0line. – Peter Grill Mar 23 '12 at 17:07\boldmathin that the closing portion is too high. See effect with$\boldmath f(x) = \sqrt{e^{2x}}$. – Peter Grill Apr 06 '12 at 03:53\DHLhksqrtmacro from the beginning (which was not done by me). Personally, I don't believe in 'decorating' font glyphs with rules, because of exactly this kind of problem. New problems might occur when you switch to another math font. – Stephan Lehmke Apr 06 '12 at 05:25\boldmath $\sqrt{3- e^{2x}}$. I added a\kernas in\setbox2=\hbox{\kern-0.375pt\vrule...which helps -- now the problem is barely noticeable at 500% zoom (with Acrobat X), but will most likely have issues with other sizes as well. Seems that the problem is that the exact location of the top right is not properly determined, and that the adjustments need to be a function of the line width. – Peter Grill Apr 06 '12 at 16:07