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'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}?

share|improve this question

2 Answers 2

up vote 16 down vote accepted

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

enter image description here

\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.

share|improve this answer

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).
share|improve this answer

Your Answer

 
discard

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.