我很迷茫。我不知道下面我做错了什么。
\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}
使用上述代码时出现以下错误:
=======> 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
?
答案1
让我们看看发生了什么。
\aermb{\frac{1}{2}}
变成
\def\ae@i@r{\frac{1}{2}}\ae@rmb
该宏\ae@rmb
测试下一个标记是否为[
,跳过空格。由于空行,下一个标记是\par
。因此,此时的输入流为
\ae@rmb\par
现在变成了
\@ae@rmb[\height]\par
宏\@ae@rmb
执行后\def\ae@i@ht{\height}
,输入流变为
\@@ae@rmb\par
由于下一个标记不是[
,因此变为
\@@@ae@rmb[\depth]\par
现在你遇到了问题,因为 的参数是#2
;这是非法的,因为宏不长。但这样做并不是解决方案,因为也是非法的。\@@@ae@rmb
\par
\long
$\par$
也许你应该定义
\def\@@@ae@rmb[#1]{%
something with the saved height,
with #1 which is the desired depth
and with \ae@i@r
}
更简单的实现方式如下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}
当然,您会对如何处理这些论点有更好的想法。