How can I find out how many arguments (i.e. {} pairs) are used when 'calling' a macro? (my favorite error, that I extend a macro, i.e. it needs one more argument, then I use it to compile a text prepared for a macro having one less argument. I receive a variety of errors, depending of the environment of the macro call in the text.)

In other words: can I find out within the macro, whether it was called with the right number of {} arguments?

  • 1
    Look at the documentation that defines the macro? Or even take a look at the macro definition.
    – Johannes_B
    Jul 16, 2016 at 8:42
  • You can use \usepackage{xparse} \NewDocumentCommand\macro{gggg}{\IfValueTF{#4}{<your definition with #1, #2, #3, and #4>}{<what you want to output in case of error>}. You just have to write the letter g the numer of times of the argument (gggg in case of four arguments), and also \IfValueTF{#4} should have also the last argument. For instance, if you want seven arguments, you can ggggggg and \IfValueTF{#7}. But it's not perfect, and you better do whatever other approach to get to where you want.
    – Manuel
    Jul 16, 2016 at 18:11

1 Answer 1


Depending on how you define "number of macro arguments" the answer may or may not be obtainable from tex and you may need to rely on the package documentation.

If you do



you see

> \fooa=\long macro:

So you could take this #1#2-> as one definition of the command taking two {} arguments

but if you do



Then you may document that as being one optional [] argument and one {} argument but \show shows the primitive definition:

> \foob=macro:
->\@protected@testopt \foob \\foob {}.

\foob technically takes no arguments at all.

Similarly after


Then \fooc operates in almost all contexts just like \fooa and acts as if it takes two arguments but



> \fooc=\long macro:
->\fooa .

confirming that it has no arguments at all as a primitive control sequence definition.

  • I'm more into \meaning :)
    – Manuel
    Jul 16, 2016 at 11:02
  • Many thanks for the detailed reply, maybe I asked the wrong way. I would like to find out within the macro, whether the user provided sufficient number of {} terms. If not, it would be better to provide a well defined error, than to receive a message informing about a wide variety of syntax errors.
    – katang
    Jul 16, 2016 at 17:47
  • @katang you can't: TeX will have picked up the 'required' number of <balanced text> arguments or given an error. You might of course find that what TeX thinks are the arguments are not what was intended!
    – Joseph Wright
    Jul 16, 2016 at 17:56
  • @katang you can't every really do that as the {} are optional if you omit them you just get the next tokens so if you forget a section title \section Once upon a time then you can not (easily) give an error that the {} argument is missing as the input is identical to \section{O}nce upon a time so the section argument is provided, and is O. Jul 16, 2016 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .