\def 两个具有相同名称但不同参数语法的宏

\def 两个具有相同名称但不同参数语法的宏

是否有可能存在名称相同但“语法”不同的宏?

例如,我们有原始的\font它可以看作有 3 个参数(a. 不包含大小信息;b. 用 提供字体大小at;c. 用 提供缩放信息scaled)。

有没有办法为自己的宏提供类似的功能?

我已经尝试过以下(愚蠢的)实验:

\def\print#1at#2,#3{The text ``#1'' shall be printed at $#2 \abovewithdelims()0pt #3$}
\def\print#1by#2,#3{The text ``#1'' shall be printed by #2 and #3 Doe}

\print{Testtext}at 2,3

\print{Testtext}by{Jon},{Jane}

\bye

尝试编译它(使用tex -interaction=nonstopmode def.tex)会产生以下错误:

This is TeX, Version 3.14159265 (TeX Live 2014) (preloaded format=tex)
(./def.tex
Runaway argument?
{Testtext}at 2,3 
! Paragraph ended before \print was complete.
<to be read again> 
                   \par 
l.5 

[1] )
(see the transcript file for additional information)
Output written on def.dvi (1 page, 280 bytes).
Transcript written on def.log.

答案1

单个宏可能只有一个定义,但宏的组合可以解析输入流:

\def\aprint#1at#2,#3{The text ``#1'' shall be printed at $#2 \abovewithdelims()0pt #3$}
\def\bprint#1by#2,#3{The text ``#1'' shall be printed by #2 and #3 Doe}


\def\print#1#2{\csname#2print\endcsname{#1}#2}

\print{Testtext}at 2,3

\print{Testtext}by{Jon},{Jane}

\bye

相关内容