[Tex/LaTex] Error: paragraph ended before … was complete

debugging

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 

? 

Best Answer

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

Related Question