Is it possible to have macros with the same name, but different “syntax”?

E.g. we have the primitive \font which could be seen as having 3 arguments(a. without size information; b. giving the fontsize with at; c. giving scaling information with scaled).

Is there a way to provide similar functionality for own macros?

I already tried the following (stupid) experiment:

\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



Trying to compile it (using tex -interaction=nonstopmode def.tex) yields following error:

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

[1] )
(see the transcript file for additional information)
Output written on def.dvi (1 page, 280 bytes).
Transcript written on def.log.
  • 1
    No: each \def has exactly one argument definition. If we can assume that #1 is well-formed then this is, though, quite doable using \futurelet (cf. LaTeX's optional argument syntax): can we assume that #1 is always a <balanced text>? – Joseph Wright Jul 6 '14 at 10:01
  • I've tagged here as tex-core rather than plain-tex as the question is applicable to any TeX format. – Joseph Wright Jul 6 '14 at 10:15
  • Sorry, I was not aware of ht tex-core-tag, I had chosen it if I was… Also what exactly do mean by “balanced text”? – NobbZ Jul 6 '14 at 10:52
  • No problem on the tagging: these things are not always easy. On <balanced text>: if you are going to ask low-level TeX questions then it's likely some things will be 'taken as read' :-) A <balanced text> is either a single token or multiple tokens enclosed in a pair of (usually) braces (matching/balanced pairs of catcode-1/catcode-2 tokens required). Examples: a, \foo, {}, {Hello {world} } but not {}}, etc. – Joseph Wright Jul 6 '14 at 10:57
  • OK, so my first intuition on this was right :) And of course param #1 is meant to be a single token or balanced group. Now it is time to dig into @david-carlisle's answer and understand whats going on, then I can accept it. – NobbZ Jul 6 '14 at 11:07

An individual macro may only have one definition, but a combination of macros can parse the input stream:

\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}


\print{Testtext}at 2,3



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.