# Adding content to nonexpandable commands without a huge hassle

I'm wondering if there is a package or method to add content around delicate commands without changing the command's name. For example, let's say I want to change \sqrt[#1]{#2} command to yield \displaystyle{\sqrt[#1]{#2}} without having to change anything in the body of my document. I want the optional arguments and standard arguments to all work as they already did... I just want to make it so every square root is in display style. Is there a nice way to add this into the already existing macro? What if the macro is non-expandable, or it may be redefined later in the document (but I want my change to persist); can this be done as well?

Edit: I apparently picked a weird example, so how about if I wanted to edit \log to render \displaystyle\log instead? The actual commands aren't really important, it's the appending and/or prepending content to an existing macro without changing the syntax of that macro in the body of the document that I am wondering about.

• You surely don't want to do \displaystyle{\sqrt}: \displaystyle is a declaration that remains valid for the current scope. – egreg Jul 24 '18 at 20:13
• I just pulled some commands off the top of my head to illustrate my point, although using \displaystyle{command} seems to work (as in; makes the command render in display mode) in my tex document... is there some reason why this is bad? – Jason Jul 24 '18 at 20:16
• \sqrt is a rather odd example actually as by far the largest part of the code is making it work consistently in \display, text,script,scriptscriptstyle so you are asking to disable almost all its functionality. – David Carlisle Jul 24 '18 at 20:17
• \displaystyle{\sqrt} does not work at all, so good or bad doesn't really apply. the \displaystyle would ignore the {} and make the rest of the expression displaystyle and the {} would stop \sqrt taking the argument expression to go under the radical. – David Carlisle Jul 24 '18 at 20:18
• @Jason \displaystyle is similar to \bfseries a switch like macro, it doesn't take an argument but changes the style of the current scope (so group). E.g. $\frac{5}{7}\displaystyle{\frac{5}{7}}\frac{5}{7}$ will change two \fracs to be display style not only one. – Skillmon Jul 24 '18 at 20:20

Just for academic interest:

\documentclass{article}
\usepackage{amsmath}

\usepackage{letltxmacro} % see https://tex.stackexchange.com/questions/88001/

\LetLtxMacro{\latexsqrt}{\sqrt}

\renewcommand{\sqrt}[2][]{%
{%
\displaystyle
\if\relax\detokenize{#1}\relax
\latexsqrt{#2}%
\else
\latexsqrt[#1]{#2}
\fi
}%
}

\begin{document}

$a\sqrt[3]{\frac{x}{y}}+\frac{x}{y}$

$a\latexsqrt[3]{\frac{x}{y}}+\frac{x}{y}$ % for comparison

$a\displaystyle\latexsqrt[3]{\frac{x}{y}}+\frac{x}{y}$

\end{document}


Note that \displaystyle is a declaration whose effect extend to the current scope, which is the reason for the additional braces in the redefinition. You can see it in the last example.

However, it's clear from the output that you don't want to do it. And, generally, you don't want to add \displaystyle to anything.

• Ah ha, it looks like the package letltxmacro is what I needed. After some Googling and reading your answer here I think that pretty much sums it up. Thanks egreg! – Jason Jul 24 '18 at 20:34