Is there a way to define a macro that takes one optional argument and behaves transparently? The traditional Latex way (\newcommand{\foo}[1][]{foo}) has the following shortcome: writing $T_\foo$ results in error (missing { inserted), but doing the same with zero-argument macro is possible.

I think a macro with an optional argument should behave the same way as a zero-argument macro when the argument is not provided. Is it possible to achieve this?

  • you get no error from {\foo} if defined as you show. Commented May 9, 2017 at 23:25
  • If you say \newcommand{\foo}[1][]{}, I don't think {\foo} will raise an error. Actually, I even tried it, thought I knew it wouldn't. Please, add an example.
    – egreg
    Commented May 9, 2017 at 23:26
  • @DavidCarlisle: Sorry, I have changed the example.
    – user87690
    Commented May 9, 2017 at 23:46
  • @egreg: Sorry, I have chagned the example.
    – user87690
    Commented May 9, 2017 at 23:47
  • @user87690 That hs nothing to do with the argument but with how does _ work in math mode.
    – Manuel
    Commented May 10, 2017 at 0:05

1 Answer 1


Unlike a macro argument, a _ does not pick up the next token if it is not braced, it expands to find the next non expandable token or brace group

so after






and boxes abc but




which is


so omitting the braces is in any case bad style and leads to unexpected behaviour, revealing the internal implementation of \foo

With the optional argument version, the behaviour reveals the implementation (and this fails)

If you really want this you can add a group in the definition:

enter image description here



$T_\foo$ $T_\foo[d]$


But it is encouraging bad document markup.

  • Thank you. Do you think, that writing $T_\foo$ is a bad markup even for zero-argument macros? So one should always write braces: $T_{\alpha}$?
    – user87690
    Commented May 10, 2017 at 0:18
  • 1
    @user87690 Yes it is bad and it's not related to the argument structure compare with this 0-argument macro T_\longrightarrow and T_{\longrightarrow} Commented May 10, 2017 at 0:21
  • I see. In my situation the macro was just a shortcut for something constant. And when the constant had more tokens, it was enclosed in a group in the definition. But then I wanted to parametrize it, and combining a group in a definition with optional argument is non-trivial (the two step definition with \bgroup). It looks strange to me to write say $T_{0}$ and $T_{\alpha}$ was similar.
    – user87690
    Commented May 10, 2017 at 0:27
  • 2
    @user87690 note the extra group only works if you want the definition to be an ordinary symbol, \longrightarrow has to have a definition \mathrel{...} so can not have an outer group or it would lose the relation spacing, which is why T_\longrightarrow fails as it is T_{\mathrel} Commented May 10, 2017 at 0:46

You must log in to answer this question.

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