[Tex/LaTex] “Closed” (square) root symbol

macrosmath-modesymbols

I found a cool trick in the LaTeX wikibook which allows the square root sign to be altered such that it "closes" over its contents.
Here's an example:

closed root

("closed" root on the left, normal one on the right)

The way they do this is as follows:

% New definition of square root:
% it renames \sqrt as \oldsqrt
\let\oldsqrt\sqrt
% it defines the new \sqrt in terms of the old one
\def\sqrt{\mathpalette\DHLhksqrt}
\def\DHLhksqrt#1#2{%
\setbox0=\hbox{$#1\oldsqrt{#2\,}$}\dimen0=\ht0
\advance\dimen0-0.2\ht0
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}}

The cool thing is that (as far as I've tested it) this works regardless of the math font and with pdftex as well as xetex.
However what is not possible is mulitple roots, such as \sqrt[3]{a}. Those produce incorrect output when this "trick" is being used.

So my question is, is somebody able to tweak this code so that it works for multiple roots as well?

Best Answer

Here's an example redefining the internal macro \r@@t of latex.ltx:

\documentclass{article}
\usepackage{letltxmacro}
\makeatletter
\let\oldr@@t\r@@t
\def\r@@t#1#2{%
\setbox0=\hbox{$\oldr@@t#1{#2\,}$}\dimen0=\ht0
\advance\dimen0-0.2\ht0
\setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
{\box0\lower0.4pt\box2}}
\LetLtxMacro{\oldsqrt}{\sqrt}
\renewcommand*{\sqrt}[2][\ ]{\oldsqrt[#1]{#2}}
\makeatother
\begin{document}
\[ \sqrt[3]{\frac{a}{b}} \quad \sqrt{\frac{a}{b}} \]
\end{document}

cubic and normal roots

Edit: \LetLtxMacro{\oldsqrt}{\sqrt} instead of \let\oldsqrt\sqrt because \sqrt takes an optional argument (as advised by egreg)