8

Is it possible to use a package or something to make typing permutations a bit faster? I now have to type $(a,b,c,d)$ in the best case, but I'd like to do something like \Pm{a b c d}. Can I do that?

Jeroen
  • 4,491
  • 1
    You mean it puts the commas in automatically? For me, there is no appreciable speed gain from typing \pm{a b c d} over \pm{a,b,c,d}. – jon Nov 07 '13 at 16:35
  • @jon: Yes, I'd like the commas to be inserted automatically. Perhaps it's not that faster for a permutation with 4 elements, but I'm working with lots more, so in my opinion, it's a bit quicker when I don't have to type all the commas. By the way, it's typing \pm{a b c d}over $(a,b,c,d)$. – Jeroen Nov 07 '13 at 17:19
  • 1
    It should be noted that \pm is already used for ±. – Qrrbrbirlbel Nov 07 '13 at 18:22
  • Naturally, I meant after defining a macro like \newcommand\pm[1]{\ensuremath{(#1)}} (and making sure I wasn't already competing with a definition for \pm).... – jon Nov 07 '13 at 19:41

2 Answers2

7

It doesn't seem much easier to type than $(a,b,c)$ but

\documentclass{article}

\def\pm#1{$(\xpm{}#1 ! )$}
\def\xpm#1#2 {\ifx!#2\else#1#2\expandafter\xpm\expandafter,\fi}

\begin{document}

\pm{a b c}

\end{document}

or without the space delimiter:

\documentclass{article}

\def\pm#1{$(\xpm{}#1 ! )$}
\def\xpm#1#2{\ifx!#2\else#1#2\expandafter\xpm\expandafter,\fi}

\begin{document}

\pm{abc}

\pm{a{bc}de}

\end{document}
David Carlisle
  • 757,742
  • 1
    It would be shorter with a for loop and not type the space! \pm{abcdefg}, that is if the variables are single letters or add commas only if they are not \pm{abcdefg,ze,xb} – yannisl Nov 07 '13 at 19:19
  • 1
    @YiannisLazarides you could just miss out the space after #2 then it would iterate over single letter variables with no space needed. – David Carlisle Nov 07 '13 at 19:27
  • One can delete the $-signs code in order to use \pm in a mathematical environment, like displaymath, align*... – Jeroen Nov 07 '13 at 19:43
  • @DavidCarlisle Was thinking about a for loop to capture the comma for the second case although maybe can be done with conditionals only. – yannisl Nov 07 '13 at 19:45
  • @YiannisLazarides: It would be perfect if \pm{a,bc,de} would generate (a,bc,d,e)! – Jeroen Nov 07 '13 at 19:55
  • 1
    @Jeroen see update using the comma in input would be possible but seems weird to me in a,bc,de why bc one token but not de anyway a{bc}de is easier:-) – David Carlisle Nov 07 '13 at 21:00
  • @DavidCarlisle: You're absolutely right! I didn't think about that. Great piece of code! – Jeroen Nov 07 '13 at 21:24
5

You can use a variant of my answer to Permutation cycle notation

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\Pm}{ O{~} m O{,} }
 {
  (
  \jeroen_cycle:nnn { #1 } { #2 } { #3 }
  )
 }

\seq_new:N \l_jeroen_cycle_seq
\cs_new_protected:Npn \jeroen_cycle:nnn #1 #2 #3
 {
  \seq_set_split:Nnn \l_jeroen_cycle_seq { #1 } { #2 }
  \seq_use:Nn \l_jeroen_cycle_seq { #3 }
 }
\ExplSyntaxOff

\begin{document}
$\Pm{1 2 3}$

$\Pm[,]{1,2,3}[\;]$

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

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

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

The leading optional argument is the input separator (default a space), the trailing one is the output separator (default a comma).

enter image description here

Alternatively, you can set up a key-value interface:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\keys_define:nn { jeroen/perms }
 {
  input-separator .tl_set:N   = \l_jeroen_perms_input_separator_tl,
  input-separator .initial:n  = { ~ },
  output-separator .tl_set:N  = \l_jeroen_perms_output_separator_tl,
  output-separator .initial:n = { , },
 }

\NewDocumentCommand{\Pm}{ O{} m }
 {
  (
  \group_begin:
  \keys_set:nn { jeroen/perms } { #1 }
  \jeroen_cycle:n { #2 }
  \group_end:
  )
 }

\NewDocumentCommand{\permset}{ m }
 {
  \keys_set:nn { jeroen/perms } { #1 }
 }

\seq_new:N \l_jeroen_cycle_seq
\cs_new_protected:Npn \jeroen_cycle:n #1
 {
  \seq_set_split:NVn \l_jeroen_cycle_seq \l_jeroen_perms_input_separator_tl { #1 }
  \seq_use:NV \l_jeroen_cycle_seq \l_jeroen_perms_output_separator_tl
 }

\cs_generate_variant:Nn \seq_set_split:Nnn { NV }
\cs_generate_variant:Nn \seq_use:Nn { NV }
\ExplSyntaxOff

\begin{document}
$\Pm{1 2 3}$

$\Pm[output-separator=\;]{1 2 3}$

$\Pm[output-separator=;]{1 2 3}$

$\Pm[input-separator={,}]{1,2,3}$

\permset{output-separator=;}

$\Pm{1 2 3}$

\permset{input-separator=|}

$\Pm{1|2|3}$
\end{document}

Key-value pairs in the optional argument are local to the current permutation, while those in the \permset command are local to the group they appear in. It would be easy to extend this to also change the outer delimiters.

enter image description here

egreg
  • 1,121,712