math shorthand macro: avoid extra space from empty superscript

This seems to be an optional feature so should be an optional argument:

 \newcommand\zo[1][]{\{0,1\}\ifx\relax#1\relax\else^{#1}\fi}

    \zo   \zo[x]

Using e.g. xparse you'll get more readable code:

\usepackage{xparse}
\DeclareDocumentCommand\zo{g}{%
  \ensuremath{%
    \{0,1\}%
    \IfNoValueTF{#1}{}{^{#1}}%
  }%
}

Using {o} as an argument specifier instead of {g} would make the command use an optional ([]-delimited) argument instead, which may be more appropriate.


Solution A

Use

\def\a{\{0,1\}}

and add ^{<stuff>} if needed.

Solution B

Notes

  • \csname <something else>\endcsname expands to \relax if \<something else> is not defined.
  • No other macro must be named \qrrgobbletwo<something> where <something> could be an argument for \a.
  • All other “tricks” and helpers used in the other answers ( \newcommand with optional argument/\@ifnextchar) can still be applied. (But then I would use an conditional anyway!)

Reference

  • Does the \relax side-effect of \csname...\endcsname still have a use?

Code

\def\qrrgobbletwo#1#2{}
\def\a#1{%
    \{0,1\}\csname qrrgobbletwo\detokenize{#1}\endcsname^{#1}%
}
\def\b#1{\{0,1\}\if\relax\detokenize{#1}\relax\else^{#1}\fi}
\leavevmode\rlap{$\a{}\a{}\a{}\a{}$}$\b{}\b{}\b{}\b{}$

\leavevmode\rlap{$\a{a}\a{a}\a{a}\a{a}$}$\b{a}\b{a}\b{a}\b{a}$
\bye

Output

enter image description here