Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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:

  1. How to create a closed \sqrt symbol which allows for three optional parameters: the root index, the \leftroot value, and the \uproot value?
  2. Bonus: How does the \DHLhksqrt macro works

References:

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}
\usepackage{letltxmacro}

\LetLtxMacro{\OldSqrt}{\sqrt}

% Adapted from http://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}
share|improve this question
add comment

1 Answer

up vote 7 down vote accepted

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}}$}

root example

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

bold root

share|improve this answer
    
Ok, that does indeed allow it to compile and applies the values of \leftroot and \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 \setbox0 line. –  Peter Grill Mar 23 '12 at 17:07
    
Thats what I said in my second edit. I'll make a version which takes this into account (if noone beats me to it...) –  Stephan Lehmke Mar 23 '12 at 17:27
    
@Peter: Have you seen my new edit? –  Stephan Lehmke Mar 26 '12 at 6:13
    
Seems to have a problem with \boldmath in that the closing portion is too high. See effect with $\boldmath f(x) = \sqrt{e^{2x}}$. –  Peter Grill Apr 6 '12 at 3:53
1  
Thanks. But you are right in that it still has problems, for example \boldmath $\sqrt{3- e^{2x}}$. I added a \kern as 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 6 '12 at 16:07
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.