Math & ScienceCustom Cube Root

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
didius
Posts: 4
Joined: Mon Jul 12, 2010 8:36 pm

Custom Cube Root

Post by didius »

Hi

Some time ago i found the following code on wikipedia:

Code: Select all

% 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}}
It makes my square roots look nicer, and more clear for my students.
However i can't use this code to print cube roots.

Code: Select all

\sqrt[3]{27}
isn't working with that 'hack'.
I can solve it using:

Code: Select all

\oldsqrt[3]{27}
But then it isn't closing the content..
I wonder if it's possible to alter the code (or write a new custom sqrt) to have cube roots who close after their content...

Does anyone know of such code?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

justdeath
Posts: 69
Joined: Mon Sep 05, 2011 10:27 am

Custom Cube Root

Post by justdeath »

You have found that from here:
http://en.wikibooks.org/wiki/LaTeX/Mathematics#Roots

And it clearly says:
Unfortunately this code won't work if you want to use multiple roots: if you try to write \sqrt{a} as \sqrt{a} after you used the code above, you'll just get a wrong output. In other words, you can redefine the square root this way only if you are not going to use multiple roots in the whole document.


I don't know any workaround, and will such a small vertical line make a difference?

Nikolay
kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Custom Cube Root

Post by kaiserkarl13 »

After some hacking and a look or two at the definitions in $TEXMF/tex/latex/base/latex.ltx, I think I've got the following working:

Code: Select all

\documentclass{article}

\let\oldsqrt\sqrt
\makeatletter
\renewcommand*{\sqrt}[2][{}]{\oldsqrt[#1]{#2}}
\def\r@@t#1#2{%
  \setbox\z@\hbox{$\m@th#1\sqrtsign{#2\,}$}%
  \dimen@\ht\z@ \advance\dimen@-\dp\z@
  \setbox2=\hbox{\vrule height\ht\z@ depth -\dimen\z@}%
  \mkern5mu\raise.6\dimen@\copy\rootbox\mkern-10mu
  {\box\z@\lower0.4pt\box2}}
\makeatother

\begin{document}

Here's the old one:  $\oldsqrt{2+3}$.
Here's the new one:  $\sqrt{2+3}$.

Here's the old one with a cube: $\oldsqrt[3]{2+3}$.
Here's the new one with a cube: $\sqrt[3]{2+3}$.
\end{document}
It's a hack, and you'll notice that it doesn't correctly preserve "\oldsqrt" for the non-square root case (it still puts the drop bar on). However, it does do what you want it to do, you just can't revert to the "old" way by using \oldsqrt with a cube root involved. Not that you seem to want to do so.

I have no idea how robust the resulting command is.
didius
Posts: 4
Joined: Mon Jul 12, 2010 8:36 pm

Custom Cube Root

Post by didius »

Thanks kaiserkarl13

That's looking great, thanks a lot!

About the robustness:

Code: Select all

\sqrt[3]{\dfrac{3x+1}{2}}
is over reacting a bit, the drop bar looks like overkill ;)

I don't know how the hacking works, but could it be possible to give the drop bar a standard height? So it doesn't grow with the size of the root?
kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Custom Cube Root

Post by kaiserkarl13 »

Code: Select all

\def\r@@t#1#2{%
  \setbox0=\hbox{$\m@th#1\sqrtsign{#2\,}$}\dimen0=\ht0
  \advance\dimen0-0.2\ht0
  \setbox2=\hbox{\vrule height\ht0 depth -\dimen0}%
  \mkern5mu\raise.6\dimen0\copy\rootbox\mkern-10mu%
  {\box0\lower0.4pt\box2}}
\makeatother
Try the above. It's almost literally the same thing as what was in the original definition you gave, except the fourth line. It's not the same height, but it's proportional to the size of the radicand.
Post Reply