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.

I have seen several related questions, but none seems to address specifically this.

I would like to define a command that accepts a variable number of arguments. Something like


So that if there is a parameter the output will be f(#1) and if there is no parameter the output will be f, and never f().

Is this possible?

share|improve this question
Yes, through the use of optional arguments. Check out the xparse package. –  Sean Allred May 2 at 13:55
How about using a counter? –  1010011010 May 2 at 13:59
Have a look at the pgfkeys package. It lets you define commands with a key=value API. –  Marc van Dongen May 2 at 14:05
@MarcvanDongen While I don't quite know how a K/V interface would fit here, the l3keys package of the expl3 bundle is also quite nice. :) –  Sean Allred May 2 at 14:10
I can't really see the advantage of writing \func{1} instead of \func(1). –  egreg May 2 at 14:11

2 Answers 2

up vote 4 down vote accepted

The xparse package allows for some really cool syntax stuff.



% This is a version that follows more popular LaTeX syntax conventions.

\[ \funcF(2) = 4       \]
\[ \funcF              \]
\[ \NormalFuncF[2] = 4 \]
\[ \NormalFuncF        \]


share|improve this answer
The parser allows () delimited arguments but in LaTeX2e () arguments should be used with picture coordinates. –  David Carlisle May 2 at 14:05
@DavidCarlisle Fair—It should be noted that normal syntax rules would follow that {} and [] conventions. I'll edit-in a version that follows this. –  Sean Allred May 2 at 14:06

Optional arguments should use [] so


Used as

\func or \func[x]

share|improve this answer
Is the \ifx structure how you test for a value with 'normal' TeX? (Also, shouldn't there be a \fi?) –  Sean Allred May 2 at 14:05
@SeanAllred a \fi might be useful, thanks:-) –  David Carlisle May 2 at 14:09
@SeanAllred if #1 is empty it is \ifx\relax\relax which is true, otherwise it is false (\relax there can be any command that you do not expect in the argument) –  David Carlisle May 2 at 14:11

Your Answer


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.