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.

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.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

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

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

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

share|improve this answer

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.