13

How do you nicely denote a permutation via cycle notation? Ie, the cycle

\[1 \mapsto 2 \mapsto 3 \mapsto 1\]

can be done by something like:

\[ (1\quad2\quad3) \]

using \quad, but that seems like a bit too much spacing.

Can I do something better?

edit: Wasn't aware of \; -- that works well.

alecbz
  • 305

3 Answers3

8

Here's a command that uses \; by default, but you can select another separator as you wish; for instance, I frequently use no separator when the set is “small” (up to 9, you see it in the second example):

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\cycle}{ O{\;} m }
 {
  (
  \alec_cycle:nn { #1 } { #2 }
  )
 }

\seq_new:N \l_alec_cycle_seq
\cs_new_protected:Npn \alec_cycle:nn #1 #2
 {
  \seq_set_split:Nnn \l_alec_cycle_seq { , } { #2 }
  \seq_use:Nn \l_alec_cycle_seq { #1 }
 }
\ExplSyntaxOff

\begin{document}
$\cycle{1,2,3}$

$\cycle[]{1,2,3}$

$\cycle[\quad]{1,2,3}$

$\cycle[,]{1,2,3}$

$\cycle[;]{1,2,3}$
\end{document}

enter image description here

Don't use $$ in LaTeX, see Why is \[ ... \] preferable to $$ ... $$?

egreg
  • 1,121,712
2

Trivial with the listofitems parser.

\documentclass{article}
\usepackage{listofitems}
\newcommand\cycle[2][\,]{%
  \readlist\thecycle{#2}%
  (\foreachitem\i\in\thecycle{\ifnum\icnt=1\else#1\fi\i})%
}
\begin{document}
$\cycle{1,2,3}$

$\cycle[]{1,2,3}$

$\cycle[\quad]{1,2,3}$

$\cycle[,]{1,2,3}$

$\cycle[;]{1,2,3}$
\end{document}

enter image description here

1

Old post, but this has been bothering me so I threw something together that I don't completely understand. In particular, I don't get why two curly braces ("{{") are needed to open and close the definition -- I guess something to do with the nested definition. I was inspired by egreg's answer, but I don't understand the expl3 (?) stuff and anyway it seems way too complicated. Fortunately egreg himself has an alternative in this answer. FYI, I shortened the function name and I prefer spaces instead of commas when entering the source ( \cyc{1 2 3} instead of \cyc{1,2,3} ).

\documentclass[a4paper]{article}
\usepackage{xparse}
\begin{document}

\newif\iffirstitem
\firstitemtrue

% \cyc[optional joiner]{space separated list}
% write list in permutation cycle notation with joiner between
% elements, defaulting to \;
\NewDocumentCommand \cyc { O{\;} >{\SplitList{ }} m }
    {{%
        \NewDocumentCommand{\processfoo}{ m }
            {
                \iffirstitem
                    \firstitemfalse
                \else
                    #1
                \fi
                ##1
            }
        (\ProcessList{#2}{\processfoo})
        \firstitemtrue
    }}

$\cyc[]{1 2 3}$

$\cyc[\,]{1 2 3}$

$\cyc{1 2 3}$

$\cyc[\quad]{1 2 3}$

$\cyc[,]{1 2 3}$

$\cyc[;]{1 2 3}$

\end{document}
joeA
  • 141
  • 6