newcommand with variable number of arguments

1,291

Solution 1

The xparse package allows for some really cool syntax stuff.

\documentclass{article}
\usepackage{xparse}

\NewDocumentCommand\funcF{d()}{%
  \IfValueTF{#1}{f(#1)}{f}}

% This is a version that follows more popular LaTeX syntax conventions.
\NewDocumentCommand\NormalFuncF{o}{%
  \IfValueTF{#1}{f(#1)}{f}}

\begin{document}
\[ \funcF(2) = 4       \]
\[ \funcF              \]
\[ \NormalFuncF[2] = 4 \]
\[ \NormalFuncF        \]
\end{document}

output

Solution 2

Optional arguments should use [] so

\newcommand{\func}[1][]{f\ifx\relax#1\relax\else(#1)\fi}

Used as

\func or \func[x]

Share:
1,291

Related videos on Youtube

Trylks
Author by

Trylks

Updated on May 03, 2020

Comments

  • Trylks
    Trylks over 3 years

    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

    \newcommand{\func}(1){\if{#1}{f(#1)}{f}}
    

    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?

    • Sean Allred
      Sean Allred over 9 years
      Yes, through the use of optional arguments. Check out the xparse package.
    • 1010011010
      1010011010 over 9 years
      How about using a counter?
    • Admin
      Admin over 9 years
      Have a look at the pgfkeys package. It lets you define commands with a key=value API.
    • Sean Allred
      Sean Allred over 9 years
      @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. :)
    • egreg
      egreg over 9 years
      I can't really see the advantage of writing \func{1} instead of \func(1).
    • Trylks
      Trylks over 9 years
      @egreg convention, how can that be done?
    • Admin
      Admin over 9 years
      @SeanAllred You can have a key that specifies the number of arguments, a key/value for first argument, a key/value for second argument, and so on. For example, [arguments=3,argument 1=a, argument 2=b, argument 3=c].
  • David Carlisle
    David Carlisle over 9 years
    The parser allows () delimited arguments but in LaTeX2e () arguments should be used with picture coordinates.
  • Sean Allred
    Sean Allred over 9 years
    Is the \ifx structure how you test for a value with 'normal' TeX? (Also, shouldn't there be a \fi?)
  • Sean Allred
    Sean Allred over 9 years
    @DavidCarlisle Fair—It should be noted that normal syntax rules would follow that {} and [] conventions. I'll edit-in a version that follows this.
  • David Carlisle
    David Carlisle over 9 years
    @SeanAllred a \fi might be useful, thanks:-)
  • David Carlisle
    David Carlisle over 9 years
    @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)
  • Jérôme LAURENS
    Jérôme LAURENS about 2 years
    () delimited arguments fit perfectly with the OP question because of the semantics. They do not conflict with the picture usage. Moreover, they make typing much more natural and refactoring a lot easier because of the xparse ability to properly balance delimiters: \NewDocumentCommand \FuncF { d() } { \IfValueTF { #1 } { f \mathord{ \left( #1 \right) } } { f } } \[ \FuncF( \sin^2(x) ) \]