# Variable argument command

I'm typesetting some group theory notes, and a significant amount of it is cycles, i.e. \left(1\,2\,3\right). Is there a way of defining a command which takes a arbitrary number of arguments so I can write \cycle{1}{2}{3}?

-

You can use a comma-separated list to define your cycle:

\documentclass{article}
\newcounter{listcount}\newcounter{totalcount}%
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\newcommand{\cycle}[1]{%
\setcounter{totalcount}{0}% Reset total count
\renewcommand*{\do}[1]{\stepcounter{totalcount}}% Reconfigure count
\docsvlist{#1}% Count number of items
\setcounter{listcount}{0}% Reset current item count
\renewcommand*{\do}[1]{% Reconfigure item \do
\stepcounter{listcount}% Next item
##1\ifnum\value{listcount}<\value{totalcount}\,\fi% Print item
}
\left(\docsvlist{#1}\right)% Process list
}
\begin{document}
$\cycle{1} \quad \cycle{1,2,3} \quad \cycle{1,1,2,3,5,8,13}$
\end{document}


The command \do is redefined twice inside \cycle. The first time is for counting the number of items supplied, while the second time is for printing all the items.

-

A comma separated list notation is really recommended.

\usepackage{xparse}
\ExplSyntaxOn

\NewDocumentCommand{\cycle}{m}{\cycle_main:n {#1}}

\seq_new:N \l_cycle_body_seq
\cs_new:Npn \cycle_main:n #1
{
\seq_set_split:Nnn \l_cycle_body_seq {,} { #1 }
\seq_pop_right:NN \l_cycle_body_seq \l_tmpa_tl
\seq_map_inline:Nn \l_cycle_body_seq { ##1 \, }
\tl_use:N \l_tmpa_tl
}
\ExplSyntaxOff


If really \left and \right are needed, which I don't believe as the argument should always be a list of numbers, then change the definition of \cycle into

\NewDocumentCommand{\cycle}{ s O{} m }
{
\IfBooleanTF{#1}{\left(}{\mathopen{#2(}}
\cycle_main:n { #3 }
\IfBooleanTF{#1}{\right)}{\mathclose{#2)}}
}


that will allow a commonly used "fence" notation:

• \cycle{1,2,3} for normal size parentheses
• \cicle[\big]{1,2,3} for larger parentheses (any size prescribing command can be used
• \cycle*{1,2,3} to enclose into \left( and \right).
-