I'm looking for a command \takeargs{n} (in either TeX or LaTeX) such that

f \takeargs{n} {x_1} ... {x_n} = f\ x_1\ ...\ x_n

, that is (if I understood that part)

\takeargs{0} =
\takeargs{n} \sometoken = \ \sometoken \takeargs{n-1}

Background: In my document, I define some functions (in Haskell-Syntax, which uses spaces for function application). On my constant striving towards don't-ever-repeat-yourself, I'd like to have newcommands for them defined automatically as I go, so I can write

\declarefunction{LaunchRockets}{2}{Coords}{Warhead}{IO ()}
% ^ Prints the function name and type and creates \CallLaunchRockets
% ...
Let $p$ be the target coordinates. Then for a huge boom we could e.g.
do a $\CallLaunchRockets p \Nuclear$ ...
% ^ Expands to $\mycodefont{LaunchRockets}\ p\ \Nuclear$
  • If you want to specify n arguments why not just put the space in manually? Unless the elements (or tokens) are generic of sorts. – Werner Feb 26 '12 at 17:26
  • So you want the TeX equivalent of the C command int array[3] = {0,1,2}; except taking functions instead of int, and with a command to print out the whole array?' – Canageek Feb 26 '12 at 17:34

What about this (plain-TeX) code? (Note: I did it in 5 minutes without thorough testing, there might be some bug, but it seems to work. Also, I used exactly your idea of a tail-recursive structure.)


  \advance\takeargscount by -1
  \takeargsseparator #1\takeargs{\the\takeargscount}%
\def\takeargsseparator{ }


  • Awesome! Thanks a lot! Have I understood your code correctly in that \expandafter prevents the \fi from coming too early, so \dotakeargs can eat the next token? – srs Feb 26 '12 at 17:36
  • Well, I'm not exactly sure (my copy of The TeXbook is at work;)), but expanding \fi means that \dotakeargs sees the next token instead of \fi. So I think you are exactly right. – mbork Feb 26 '12 at 18:15
  • Also, I guess that eTeX's \numexpr might help making the code shorter, but I'm not sure (and don't have time to try it now, sorry). Anyway, I'm glad you like this;). – mbork Feb 26 '12 at 18:16
  • And one more thing: instead of \expandaftering after \fi, you could use Knuth's \let\next=... trick. – mbork Feb 26 '12 at 18:17
  • @rainmaker: {a}, {b}, {c} are 3 items. Why then do you need to put 3 to lead them? – Ahmed Musa Feb 26 '12 at 18:20

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.