I have the following macro defining a mathematical symbol:


Depending on the context, the symbol x should be decorated with a \widetilde or a \widehat or stay undecorated.

A pseudocode could look like:

Hello $\myX{1}{2}$ % x has a hat
Hello $\myX{1}{2}$ % x has a tilde
Hello $\myX{1}{2}$ % x is undecorated

I have already defined the macros for many of those symbols, and I also use them in my text (with no decorators at all), so a solution that doesn't need too much change in the text and in the macros would be great.

What's the best practice to achieve that?

3 Answers 3


If by “context” you mean an environment, it's just a matter of defining correctly the macro:

\newcommand\decorate{\@firstofone} % default

% now we can define macros based on \decorate

If you prefer a declarative style like in your example:


\nodecoratorcontext % start up with no decoration

% now we can define macros based on \decorate

What’s the purpose of \@firstofone? It simply removes the braces, because its definition is basically


so, when this is the value for \decorate, from \myX{a}{b} you first get


and then

  • Thanks, that seems nice. What I don not understand: What's the purpose of \@firstofone? I can not quite find a good explanation.
    – Michael
    May 28, 2018 at 6:21
  • While it works basically, it seems to make trouble when done inside the argument of another macro. Is that possible? I'm currently trying to understand it better, but it's not easy with LaTeX...
    – Michael
    May 28, 2018 at 13:22
  • 1
    @Michael What do you mean? I can't understand what you're trying to achieve. Anyway, context depending macros are evil. Use \tilde when you want a tilde, \hat when you want a hat.
    – egreg
    May 28, 2018 at 13:23

I now use the following solution, comments are welcome:








  It's $\mya$.

  It's $\mya$.

  It's $\mya$.

  • Why \NewEnviron?
    – egreg
    May 27, 2018 at 21:01
  • @egreg: I'm mostly a beginner, so I just searched how to create an own environment, and that's what I found. Would there be a better solution?
    – Michael
    May 27, 2018 at 21:17
  • Yes: \newenvironment is the key; \NewEnviron is a rather advanced tool, which can bite you in several ways. But see my other approach.
    – egreg
    May 27, 2018 at 21:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .