Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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

\bye

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)
(./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.
share|improve this question
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 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 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 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 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 at 11:07

1 Answer 1

up vote 19 down vote accepted

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}


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

\print{Testtext}at 2,3

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

\bye
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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