[Tex/LaTex] Ignoring arguments passed to a command depending on the number of arguments specified

macros

Consider the following code:

I'm trying to write a \newcommand that will accept either m or n arguments, where m < n. Here m=4, n=6. I want to set things up so that if I call a newcommand, and it is passed 6 arguments, it will ignore the latter two if \fl is defined.

The rationale for this is that I want to merge different commands depending on different options, but I want to call the commands in the same way.

\documentclass{article}

\def\fl{}

\ifdefined\fl
  \newcommand{\xx}[4]
  {#1 #2 #3 #4}
\else
  \newcommand{\xx}[6]{%
    {#1 #2 #3 #4
      \begin{#5}
        \item foo
      \end{#6}
    }
  } \fi

\begin{document}
\xx{a}{b}{c}{d}{enumerate}{enumerate}
\end{document}

To be clear, I want to get

a b c d

if \fl is defined, and

a b c d
\begin{enumerate}
\item foo
\end{enumerate}

if it is not.

Best Answer

Nothing requires you use all arguments in the replacement text:

\documentclass{article}

\def\fl{}

\ifdefined\fl
  \newcommand{\xx}[6]
  {#1 #2 #3 #4}
\else
  \newcommand{\xx}[6]{%                                                                                                                                                    
    {#1 #2 #3 #4                                                                                                                                                           
      \begin{#5}                                                                                                                                                           
        \item foo                                                                                                                                                          
      \end{#6}                                                                                                                                                             
    }                                                                                                                                                                      
  } \fi

\begin{document}                                                                                                                              
\xx{a}{b}{c}{d}{enumerate}{enumerate}
\end{document}

Output when \fl is defined

enter image description here

Output when \fl is not defined

enter image description here

In order to get this, I just put % in front of \def\fl{}


If the status of \fl is changing in the document, a more complex strategy is necessary:

\documentclass{article}[12pt]

\makeatletter
\newcommand{\xx}[4]{%
  #1 #2 #3 #4%
  \ifdefined\fl
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\@gobbletwo}%
  {\xx@aux}%
}
\newcommand\xx@aux[2]{%
  \begin{#1}
    \item foo
  \end{#2}
}

\begin{document}
Here \verb+\fl+ is not defined

\xx{a}{b}{c}{d}{enumerate}{enumerate}

\bigskip
\newcommand{\fl}{}

Here \verb+\fl+ is defined

\xx{a}{b}{c}{d}{enumerate}{enumerate}

\end{document}

enter image description here

Related Question