In the context of writing something like homework solutions for a math class, I often face an awkward situation. Each problem will have some idiomatic functions that are cumbersome and error prone to rewrite. For example,

f(x)=\sup_{\xi\in\mathcal{A}}\sum_{k=1}^\infty \phi_k(x,\xi)

One solution, to avoid rewriting the right hand side is

\newcommand{\f}[1]{\sup_{\xi\in\mathcal{A}}\sum_{k=1}^\infty \phi_k(#1,\xi)}

This allows me to write out the right hand side in its entirety with less chance of typos, when I don't need to manipulate the subparts of the expression, making for cleaner code. However in some cases, the function f may take on different definitions in different problems, so I would often need to redefine it.

Is there a way to make a definition local to some part of the code so that the redefinition will have less chance of affecting the other parts of my code in case I forget to redefine the variable? This is especially important if formatting the function requires some manual spacing adjustments which make the code cluttered.


Group the redefinition to make it local (either using {...}, or \begingroup...\endgroup, or \bgroup...\egroup, or inside the math group itself $...$). Of course, you could also incorporate optional arguments to specify some of the changes to your function in order to make the changes more general (so you don't have to localize it through an entire redefinition):

enter image description here

\newcommand{\func}[2][\xi]{\sup_{#1\in\mathcal{A}}\sum_{k=1}^\infty \phi_k(#2,#1)}
$\func{x}$ \par% Default definition
$\renewcommand{\func}[1]{f(#1)} \func{x}$ \par% Entire redefinition (localized)
$\func[\psi]{x}$% Localized "redefinition"

This might all depend on the way you can generalize the functions you use.


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.