3

I'm quite lost. I can't figure out what I'm doing wrong below.

\documentclass{article}
\usepackage{amsmath,amssymb}
\makeatletter

\newcommand\aermb[1]{\def\ae@i@r{#1}\ae@rmb}

\def\ae@rmb{%%
  \typeout{=======> \space\space ae@rmb}%%
  \@ifnextchar[%]
  {\@ae@rmb}{\@ae@rmb[\height]}}

\def\@ae@rmb[#1]{%%
  \typeout{=======> \space @ae@rmb}%%
  \def\ae@i@ht{#1}%%
  \@@ae@rmb}

\def\@@ae@rmb{%%
  \typeout{=======> @@ae@rmb}%%
  \@ifnextchar[%]
  {\typeout{\space\space\space optional arg}\@@@ae@rmb}
  {\typeout{no optional arg}\@@@ae@rmb[\depth]}}

\def\@@@ae@rmb[#1]#2{$#2$}%%

%% \def\@@@ae@rmb[#1]#2{%%
%%   \typeout{-->\detokenize\x{\ae@i@r}}%%
%%   \typeout{-->\detokenize\x{\ae@i@ht}}%%
%%   \typeout{==>\detokenize{#1}}}%%
%%   \raisebox{\ae@i@r}[\ae@i@ht][#1]{\mbox{$#2$}}}

\makeatother
\begin{document}

\aermb{\frac{1}{2}}


\end{document}

I get the following error with the above code:

=======>   ae@rmb
=======>  @ae@rmb
=======> @@ae@rmb
no optional arg
Runaway argument?
! Paragraph ended before \@@@ae@rmb was complete.
<to be read again> 
                   \par 
l.30 

? 
A.Ellett
  • 50,533
  • This is always due to \par in the argument of a non \long macro. – egreg Oct 24 '14 at 20:21
  • @egreg Where is the \par coming from? – A.Ellett Oct 24 '14 at 20:22
  • @egreg when I add \long before \def\@@@ae@rmb.... I get a new error about missing $ even though the macro is supposed to insert them. Why is that? – A.Ellett Oct 24 '14 at 20:24
  • From the empty line after {\frac{1}{2}} – egreg Oct 24 '14 at 20:24
  • @egreg That really confuses me. I'm sure I've made plenty of macros and used them at the end of a paragraph without such problems. Why is the paragraph occurring after the macro call effecting the expansion of the macro's argument, which doesn't contain any new paragraph. – A.Ellett Oct 24 '14 at 20:26
  • put x there and you'll see it is x that gets set in math mode not your fraction so it's trying to set \par – David Carlisle Oct 24 '14 at 20:30
  • I see my error. I forgot about the argument I gave to \aermb when I wrote \@@@ae@rmb. – A.Ellett Oct 24 '14 at 20:32
  • Should we vote to close or should I just delete this question? – A.Ellett Oct 24 '14 at 20:32
  • @A.Ellett I find the question interesting enough. See the xparse implementation of your multipronged macro: just a tad easier. ;-) – egreg Oct 24 '14 at 20:47

1 Answers1

4

Let's see what happens.

\aermb{\frac{1}{2}}

becomes

\def\ae@i@r{\frac{1}{2}}\ae@rmb

The macro \ae@rmb tests if the next token is [, skipping spaces. The next token is \par, due to the empty line. Thus the input stream, at this point, is

\ae@rmb\par

and now it becomes

\@ae@rmb[\height]\par

The macro \@ae@rmb does \def\ae@i@ht{\height} and the input stream becomes

\@@ae@rmb\par

Since the next token is not [, this becomes

\@@@ae@rmb[\depth]\par

and now you have the problem, because argument #2 to \@@@ae@rmb is \par; this is illegal because the macro is not long. But making it \long is not the solution, because $\par$ is illegal either.

Probably you should define

\def\@@@ae@rmb[#1]{%
  something with the saved height,
  with #1 which is the desired depth
  and with \ae@i@r
}

An easier implementation with xparse:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{xparse}

\NewDocumentCommand{\aermb}{O{\height}O{\depth}m}{%
  \raisebox{-#1}{$#3$}%
  \raisebox{#2}{$#3$}%
  arg: $#3$
}


\begin{document}

1:\aermb{\dfrac{1}{2}}
2:\aermb[1pt]{\dfrac{1}{2}}
3:\aermb[3pt][1pt]{\dfrac{1}{2}}

\end{document}

Of course you'll have better ideas about what to do with the arguments.

enter image description here

egreg
  • 1,121,712