11

I'm getting slightly desperate because the following seemingly simple macro does not do what I want:

\newcommand{\Ma}[1][]{\ensuremath{M_{1}^{#1}}\xspace}
Hello $\Ma{a}$.

I would expect that $\Ma{a}$ would result in a as a superscript, but id does not. What's wrong here?

1
  • 1
    #1 is the optional argument, you want either two argument [2] and #2 or should use [a] as input. xspace is quite useless here and I wouldn't use \ensuremath either. Commented May 26, 2018 at 10:12

1 Answer 1

18

You have \newcommand{\Ma}[1][]{.... The last pair of brackets mean that the macro has one optional argument, which by default is empty. #1 refers to this optional argument. To use an optional argument, you need \Ma[a]. Because no [ is seen, the {a} is not read as an optional argument, and so is just typeset as normal. Use \newcommand{\Ma}[1]{..., or see example below.

Following up on Ulrike's comment, some links about xspace and \ensuremath:

And a complete example, with two possible macro definitions.

enter image description here

\documentclass{article}
\newcommand{\Ma}[1]{M_{1}^{#1}}
\newcommand{\Mb}[2][1]{M_{#1}^{#2}}
\begin{document}
$\Ma{a}, \Mb{b} \Mb[]{c} \Mb[2]{d}$.
\end{document}

You must log in to answer this question.

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