I want to re-define the \sqrt command (as detailed in Nice-looking p-th roots), and so far I have the following:


\usepackage{fouriernc} % use the New Century Schoolbook font


This $\sqrt[p]{a}$ looks better than $\oldsqrt[p]{a}$,
since the $p$ does not intersect the root symbol.

However, when I try writing \sqrt{a}, I get an error. How can I make the #1 argument optional, so that if it is not passed in, it leaves that area above the root symbol blank (as it would in the default \sqrt command)?

(This is a follow-up to the comments on David's and Harish's answers to above-mentioned question.)

  • 8
    I would suggest using LaTeX's \renewcommand which handles optional arguments for you: \renewcommand\sqrt[1][]{\oldsqrt[\leftroot{-3}\uproot{3}#1]}. – Caramdir Mar 23 '12 at 1:34

If you want commands with at most one optional argument, LaTeX's built in \newcommand and \renewcommand are an easy way to define them. The syntax is

\(re)newcommand⟨\name⟩[⟨number of arguments⟩][⟨default value for the first argument⟩]{⟨code⟩]

If you specify the second optional argument, then the first argument to \name will be optional. So in your case one can simply use


Note that (as in your question), we do not need to specify the second argument, because \oldsqrt will look for it anyway, so that this definition is functionally equivalent to


If you need several optional arguments or an argument other than the first one should be optional, you can either chain \@ifnextchars (as in Yiannis' answer), or use xparse (as in Peter's answer).

  • Thanks! Just to be clear, you still need the line \let\oldsqrt\sqrt before the \renewcommand. – jamaicanworm Mar 23 '12 at 17:11

The primitive \def does not understand optional arguments. That is provided in LaTeX only. What you have defined is a delimited argument:


which means that the input must always be provided as [...], you could have written anything instead of []. To have an optional argument you need to define it in LaTeX using the following method:

  \DeclareRobustCommand\sqrt{\@ifnextchar[{true statement}{false statement}}

The @\ifnextchar checks to see if there is an [ and branches accordingly. You will need to modify your script on this basis.

The commands are normally defined to call two macros, one delimited and another undelimited, as for example:

  \def\@@sqrt[#1]{I have a square bracket #1}  
  \def\@sqrt#1{I don't have a square bracket #1}

Now redefining amsmath macros, is like walking barefoot on a coal fire, so a few more items need to be taken care of. Firstly to let save the old command, we use Oberdiek's package letltxmacro


The rest are shown in the minimal below:

\usepackage{fouriernc} % use the New Century Schoolbook font
   \sqrt[\beta]{k} \qquad
   \sqrt[c]{k} \qquad

enter image description here

  • 1
    @jamaicanworm It will freeze if you redefine it, as you will have infinite regression. Will post a full answer a bit later in the date. – Yiannis Lazarides Mar 23 '12 at 2:10
  • 1
    @jamaicanworm Please see edit done after two caffeine injections:) – Yiannis Lazarides Mar 23 '12 at 3:21
  • 1
    @YiannisLazarides The \ensuremath in the definition of \@@sqrt you're using is wrong and not from a stylistic point of view. Also it's not clear why using \DeclareRobustCommand for \@@sqrt. Note also that redefining \sqrt in this way will prevent further usage of \leftroot and \uproot. – egreg Mar 23 '12 at 9:42
  • 1
    @egreg Thanks. Was sure I missed something and the reason I am normally reluctant to touch anything amsmath. I would appreciate it if you edit a correction in, otherwise will apply a correction in the evening. OP wanted to fix the parameters so I am not over worried about losing \leftroot – Yiannis Lazarides Mar 23 '12 at 10:06
  • 2
    It is what I am looking for. (+100) – kiss my armpit Mar 23 '12 at 16:42

Using a new command: \mysqrt:

This is what I was suggesting in the comments to Nice-looking p-th roots using the xparse package to make the first three parameters optional:

\NewDocumentCommand{\mysqrt}{O{} O{-2} O{2}  m}{\sqrt[\leftroot{#2}\uproot{#3}#1]{#4}}

Then $\mysqrt[p]{a}$ would produce the desired result:

enter image description here

Note that the first optional parameter in the index of the \sqrt. The following two options are the tweaks that get applied to the \leftroot (defaults to -2 in the above code) and the \uproot (defaults to 2).

The same examples from the linked question would be specified as where the left uses the default settings and the right uses the manual tweaks:

\[\mysqrt[\beta]{k}            \quad\mysqrt[\beta][-3][3]{k}          \]
\[\mysqrt[\beta]{\frac{k}{h}}  \quad\mysqrt[\beta][-2][6]{\frac{k}{h}}\]


enter image description here


\NewDocumentCommand{\mysqrt}{O{} O{-2} O{2}  m}{%

This $\mysqrt[p]{a}$ looks better than $\sqrt[p]{a}$,
since the $p$ does not intersect the root symbol.

Redefine the existing \sqrt macro:

If you wish to redfine the \sqrt globally then you have to use \LetLtxMacro from the letltxmacro package since \sqrt has optional parameters already.


The following produces results identical to the second image above:



\RenewDocumentCommand{\sqrt}{O{} O{-2} O{2}  m}{\OldSqrt[\leftroot{#2}\uproot{#3}#1]{#4}}%

\[\sqrt[\beta]{k}            \quad\sqrt[\beta][-3][3]{k}          \]
\[\sqrt[\beta]{\frac{k}{h}}  \quad\sqrt[\beta][-2][6]{\frac{k}{h}}\]
  • Thanks for your help! The problem is that I already have a long document with many uses of \sqrt. I want to re-define the \sqrt command at the top of the document, without having to find-and-replace each one with \mysqrt. – jamaicanworm Mar 23 '12 at 1:35
  • 1
    @jamaicanworm: Ok, have provided a solution for that as well. – Peter Grill Mar 23 '12 at 1:41
  • 1
    Nice piece of work! – percusse Mar 23 '12 at 1:49

This answer is about solving the specific problem, that is, initializing \leftroot and \uproot to different values from the default, leaving intact the possibility of using them for particular cases.

The trick is to notice that amsmath uses the values given as arguments to \leftroot and \uproot to set two count registers, \leftroot@ and \uproot@ which are initialized to zero. This happens in the internal macro called \root. Thus one can do

\patchcmd{\root}{\uproot@\z@}{\uproot@3 }{}{}
\patchcmd{\root}{\leftroot@\z@}{\leftroot@-3 }{}{}

and now those initial values will be applied, but saying


would give the same result as \sqrt[\leftroot{1}\uproot{3}x]{y} without the patching, which might be useful in particular cases. Similarly, \sqrt[\leftroot{0}\uproot{0}x]{y} would give the same result as the original \sqrt[x]{y}.

As Stefan Lehmke points out in his comment, changing the meaning of \sqrt is not really recommended. However, setting a different default for \leftroot and \uproot could be justified with some document fonts. The important thing is to know how the new \sqrt behaves with respect to \leftroot and \uproot.

Making \leftroot and \uproot adding to the default requires a very extensive patching of \root:

\def\default@leftroot{-3} % set the default value of leftroot
\def\default@uproot{3}    % set the default value of uproot
  \def\nextiv@{\ifx\@let@token\@sptoken\DN@. {\FN@\nextv@}\else
   \DN@. {\FN@\nextviii@}\else
  \bgroup\uproot@\default@uproot \leftroot@\default@leftroot\relax \FN@\next@}

(Thanks to Stefan Lehmke for spotting an error in the initial version.)

  • 1
    Here's a slightly less invasive patch: \def\mynextvi@root\leftroot#1{\advance\leftroot@#1\relax\plainroot@}% \def\mynextiii@root\leftroot#1{\advance\leftroot@#1\relax\FN@\nextvii@}% \def\mynextii@root\uproot#1{\advance\uproot@#1\relax\FN@\nextiv@}% \patchcmd{\root}{\uproot@\z@}{\uproot@3 }{}{} \patchcmd{\root}{\leftroot@\z@}{\leftroot@-10\let\nextii@\mynextii@root\let\nextiii@\mynextiii@root\let\nextvi@\mynextvi@root}{}{} But that's a matter of taste I assume. While you're at it, you could consider putting the defaults into global registers... Darn, no newlines in comments... – Stephan Lehmke Mar 23 '12 at 11:19
  • 1
    Parametrizing the defaults was indeed the next step (I thought to it just after closing the edit, but I had to go and catch the train). – egreg Mar 23 '12 at 11:22
  • Thanks! In your patch, why are \makeatletter and \makeatother necessary? – jamaicanworm Mar 23 '12 at 15:35
  • 1
    @jamaicanworm Those are necessary because we are using commands with @ in their names. – egreg Mar 23 '12 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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