8

As I understand it, \mathpalette takes two arguments; the first must be a macro A that itself takes two arguments. A will be passed as #1 commands to reproduce the math enviroment at the location where \mathpalette was invoked; and A will be passed as #2 the second argument passed to the invocation of \mathpalette. For instance:

\newcommand\A[2]{\hbox{$\m@th#1(#2)$}}
...
$$\mathpalette\A2$$

Will display a (2) in the same math environment as 2 inhabited on the last line. (Here, that is a displaystyle environment.)

So far, so good. My problem is that I want to work with a macro like A that needs additional parameters. My strategy for solving this was to keep A as a 2-argument macro (making \mathpalette happy) but to have A invoke a further macro, but only partially supplying the arguments it wants. The rest of the arguments should be read from the position in the stream following the invocation of \mathpalette. For example:

\newcommand\A[2]{\B#1#2}
\newcommand\B[3]{\hbox{$\m@th#1(#2,#3)$}}
...
$$\mathpalette\A23$$

\mathpalette will invoke A with the current math environment settings in #1 and with 2 in #2. A should then pass those arguments on to B, and B should read 3 as its third argument, yielding the output (2,3).

But this doesn't compile. It gives the error:

! Argument of \B has an extra }.

I can get it to compile but sticking \expandafter or \relax at the end of the definition of \A, but I don't fully understand what I'm doing, and none of my monkeying around is getting this to deliver the desired output of (2,3).

dubiousjim
  • 1,035
  • 1
  • 8
  • 14

2 Answers2

8

The problem is that, \mathchoice (which is used to define \mathpalette) takes four subformula arguments that will be typeset immediately.

In your example,

\newcommand\A[2]{\B#1#2}
\newcommand\B[3]{\hbox{$\m@th#1(#2,#3)$}}
$$\mathpalette\A23$$

We know that \mathpalette\A23 expands to \mathchoice{\A\displaystyle{2}}{\A\textstyle{2}}{\A\scriptstyle{2}}{\A\scriptscriptstyle{2}}3 but \A\displaystyle{2} cannot be typeset because you cannot expand it.

To fix the problem, you should use \mathchoice directly:

\documentclass{article}
\makeatletter
\newcommand\A[2]{%
  \mathchoice
    {\B\displaystyle{#1}{#2}}
    {\B\textstyle{#1}{#2}}
    {\B\scriptstyle{#1}{#2}}
    {\B\scriptscriptstyle{#1}{#2}}}
\newcommand\B[3]{\hbox{$\m@th#1(#2,#3)$}}

\begin{document}

$ \A23 $

\end{document}

Or, you can define your own variant of \mathpalette:

\documentclass{article}
\makeatletter
\newcommand\mathpalettetwo[3]{%
  \mathchoice
    {#1\displaystyle{#2}{#3}}
    {#1\textstyle{#2}{#3}}
    {#1\scriptstyle{#2}{#3}}
    {#1\scriptscriptstyle{#2}{#3}}}
\newcommand\B[3]{\hbox{$\m@th#1(#2,#3)$}}

\begin{document}

$ \mathpalettetwo\B23 $

\end{document}
Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
Leo Liu
  • 77,365
8

Use another level:

\documentclass{article}

\makeatletter
\newcommand{\jim}[2]{\mathpalette\jiminner{{#1}{#2}}}
\newcommand{\jiminner}[2]{\mbox{$\m@th#1\jimsolve#2$}}
\newcommand{\jimsolve}[2]{(#1,#2)}
\makeatother

\begin{document}

$\jim{1}{2}\scriptstyle\jim{3}{4}\scriptscriptstyle\jim{5}{6}$

\end{document}

The two arguments to \jim are packed into a single argument to \mathpalette; then \jiminner passes control to \jimsolve.

enter image description here

egreg
  • 1,121,712