# Permutation Operator

I'd like to be able to write permutations in the correct way: Typesetting permutations with latex but I would like a command to do it automatically.

I have simply done it manually: (1\ 2\ 3) etc. - no thank you. I can make a command for each one:

\newcommand{\threeperm}[3]{({#1}\ {#2}\ {#3})}
\newcommand{\fourperm}[4]{({#1}\ {#2}\ {#3}\ {#4})}


But (a) that doesn't seem like an efficient way around it considering I could easily be writing a permutation of up to 15 elements or so, and (b) I'd also like to be able to use it in subscript and have smaller spaces between the elements: and don't want to have to duplicate all of my commands like this:

\newcommand{\smallthreeperm}[3]{({#1}\, {#2}\, {#3}\, {#4})}


and so on.

Is there a way to create a macro to perform this task?

Thank you in advance for any help provided.

In order to process multiple items in the same way, I'd suggest processing the content as a list (separated by commas, say):

\documentclass{article}

\usepackage{etoolbox}

\newcommand{\permutation}[1]{
\gdef\itemdelim{\gdef\itemdelim{\mathchoice{\ }{\ }{\,}{\,}}}
(\dopermutation{#1})}
% https://tex.stackexchange.com/a/89187/5764
\newcommand{\dopermutation}[1]{
\renewcommand*{\do}[1]{\itemdelim##1}
\docsvlist{#1}}

\begin{document}

\begin{tabular}{l}
$(1\ 3\ 2\ 4)$ \\
$\permutation{1,3,2,4}$
\end{tabular}

\begin{tabular}{l}
$S_{(1\,3\,2\,4)}$ \\
$S_{\permutation{1,3,2,4}}$
\end{tabular}

\end{document}


You can update the spacing depending on the math mode you're in.

References:

Here the separators are spaces; if you prefer a syntax like \perm{1,2,3,4} (but output just blank space), change { ~ } in the code below with { , }.

\documentclass{article}
\usepackage{xparse}

\newcommand{\permsep}{%
\mathchoice
{\mskip\thickmuskip}% display
{\mskip\medmuskip}% text
{\mskip1.5mu}% first level sub/superscript
{\mskip1.5mu}% second level or deeper
}

\ExplSyntaxOn
\NewDocumentCommand{\perm}{m}
{
\seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
( \seq_use:Nn \l_tmpa_seq { \permsep } )
}
\ExplSyntaxOff

\begin{document}

$\perm{1 2 3 4}\in S_{\perm{1 2 3 4}}$
$\perm{1 2 3 4}$

\end{document}


With \mathchoice you can decide what spacing you want for each math style.

\documentclass{scrartcl}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \perm { r() } { \percy_perm:n {#1} }
\cs_new_protected:Npn \percy_perm:n #1
{
(
\seq_set_split:Nnn \l_tmpa_seq { ~ } { #1 }
\mathchoice
{ \seq_use:Nn \l_tmpa_seq { \  } }
{ \seq_use:Nn \l_tmpa_seq { \  } }
{ \seq_use:Nn \l_tmpa_seq { \, } }
{ \seq_use:Nn \l_tmpa_seq { \, } }
)
}
\ExplSyntaxOff

\begin{document}
$\perm(1 2 3 4)$ and $S_{\perm(1 2 3 4)}$
\end{document}


I suggest using the natural syntax of \perm(1 2 3 4) to accomplish the task. Here, I use listofitems package to parse the list, and scalerel to achieve a smaller gap for the smaller math sizes.

\documentclass{article}
\usepackage{listofitems,scalerel}
\setsepchar{ }
\def\perm(#1){(%
\ThisStyle{%
\foreachitem\pitem\in\permlist{\pitem{}\kern3\LMpt\kern-1pt}%
\kern-2\LMpt%
})%
}
\begin{document}
$\perm(1 2 3 4)_{{\perm(1 2 3 4)}_{\perm(1 2 3 4)}}$
\end{document}


Here is solution based on TeX primitives:

\def\permsep{\mathchoice {\mskip5mu} {\mskip4mu plus2mu}
{\mskip1.5mu} {\mskip1mu}}
\def\perm#1{\permA #1 {} }
\def\permA#1 {\bgroup(#1\permB}
\def\permB#1 {\ifx\relax#1\relax)\egroup \else \permsep#1\expandafter\permB\fi}

$\perm{1 2 3 4},\ S_\perm{1 2 3 4}$

\end