5

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. – David Carlisle May 9 '17 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 May 9 '17 at 23:26
  • @DavidCarlisle: Sorry, I have changed the example. – user87690 May 9 '17 at 23:46
  • @egreg: Sorry, I have chagned the example. – user87690 May 9 '17 at 23:47
  • @user87690 That hs nothing to do with the argument but with how does _ work in math mode. – Manuel May 10 '17 at 0:05
10

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

\newcommand\foo{abc}

then

    \fbox\foo

is

\fbox{\foo}

and boxes abc but

$T_\foo$

is

$T_abc$

which is

$T_{a}bc

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

 \documentclass{article}

\def\foo{\bgroup\xfoo}
\newcommand\xfoo[1][]{abc#1\egroup}
\begin{document}



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


\end{document}

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 May 10 '17 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} – David Carlisle May 10 '17 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 May 10 '17 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} – David Carlisle May 10 '17 at 0:46

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.