Command with arguments separated by comma II
The argument is first split at commas in a sequence which is mapped taking into account the positions, so we can add &
or \\
as needed.
documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\matr}{m}
{
\begin{pmatrix}
\seq_set_from_clist:Nn \l__watson_matr_seq { #1 }
\seq_indexed_map_function:NN \l__watson_matr_seq \__watson_matr_entry:nn
\end{pmatrix}
}
\seq_new:N \l__watson_matr_seq
\cs_new:Nn \__watson_matr_entry:nn
{
#2 % the entry
\int_case:nn { #1 } { {1}{&} {2}{\\} {3}{&} }
}
\ExplSyntaxOff
\begin{document}
\[
\matr{a,b,c,d}
\]
\end{document}
Can we generalize it? Yes.
\documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\matr}{O{2}m}
{
\int_set:Nn \l__watson_matr_size_int { #1 }
\begin{pmatrix}
\watson_matr:nn { #1 } { #2 }
\end{pmatrix}
}
\seq_new:N \l__watson_matr_seq
\int_new:N \l__watson_matr_size_int
\cs_new_protected:Nn \watson_matr:nn
{
\seq_set_from_clist:Nn \l__watson_matr_seq { #2 }
\seq_indexed_map_function:NN \l__watson_matr_seq \__watson_matr_entry:nn
}
\cs_new_protected:Nn \__watson_matr_entry:nn
{
#2 % the entry
\int_compare:nTF { \int_mod:nn { #1 } { \l__watson_matr_size_int } = 0 }
{ \\ }
{ & }
}
\ExplSyntaxOff
\begin{document}
\[
\matr{a,b,c,d}\ne\matr[3]{1,2,3,4,5,6,7,8,9}
\]
\end{document}
If you're willing to accept a slightly different syntax, with semicolons for ending rows, you can easily produce matrices with different shapes and delimiters.
\documentclass{article}
\usepackage{amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\matr}{O{p}m}
{
\seq_set_split:Nnn \l__watson_matr_full_seq { ; } { #2 }
\begin{#1matrix}
\seq_map_function:NN \l__watson_matr_full_seq \__watson_matr_makerow:n
\end{#1matrix}
}
\seq_new:N \l__watson_matr_full_seq
\seq_new:N \l__watson_matr_row_seq
\cs_new_protected:Nn \__watson_matr_makerow:n
{
\seq_set_split:Nnn \l__watson_matr_row_seq { , } { #1 }
\seq_use:Nn \l__watson_matr_row_seq { & }
\\
}
\ExplSyntaxOff
\begin{document}
\[
\matr{a,b;c,d}\ne\matr[b]{1,2,3;4,5,6;7,8,9;10,11,12}
\]
\end{document}
Here I use tokcycle
to replace the commas with &
separators. WHile I show 2x2 and 3x3, any number of columns are acceptible, as long as the matrices contain 2 or (optionally) 3 rows.
\documentclass{article}
\usepackage{amsmath,tokcycle}
\newcommand\matr[1][2]{\tctestifnum{3=#1}{\matrTHREE}{\matrTWO}}
\newcommand\matrTWO[2]{%
\tokcyclexpress{#1\\#2}%
\begin{pmatrix}\the\cytoks\end{pmatrix}}
\newcommand\matrTHREE[3]{%
\tokcyclexpress{#1\\#2\\#3}%
\begin{pmatrix}\the\cytoks\end{pmatrix}}
\Characterdirective{\tctestifx{,#1}{\addcytoks{&}}{\addcytoks{#1}}}
\begin{document}
\[
\matr{a, b}{c, d}
\]
\[
\matr[3]{a, b,c}{d, e, f}{g, h, i}
\]
\end{document}
OPTIONAL SINGLE ARGUMENT APPROACH
This allows any combination of rows and columns within a single argument with the syntax \matr{ a, b / c, d}
Here, commas are taken a column separators and /
tokens are taken as row separators:
\documentclass{article}
\usepackage{amsmath,tokcycle}
\newcommand\matr[1]{\tokcyclexpress{#1}%
\begin{pmatrix}\the\cytoks\end{pmatrix}}
\Characterdirective{\tctestifx{,#1}{\addcytoks{&}}{%
\tctestifx{/#1}{\addcytoks{\\}}{\addcytoks{#1}}}}
\begin{document}
\[
\matr{a, b / c, d}
\]
\[
\matr{a, b,c / d, e, f/ g, h, i}
\]
\end{document}
I am highly unused to the LaTeX3 syntax too, so I give you non-exp3 solution based only on TeX primitives. You need not any LaTeX package (and you basically don't even need LaTeX):
\documentclass[12pt]{article}
\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\matr#1{\def\matrL{}\matrA#1\end}
\def\matrA#1{\ifx\end#1\pmatrix{\matrL}\else
\ifx,#1\addto\matrL{&}\else
\ifx;#1\addto\matrL{\cr}\else
\addto\matrL{#1}\fi\fi
\expandafter\matrA\fi
}
\begin{document}
$$
\matr{a,b;c,d}\ne\matr{1,2,3;4,5,6;7,8,9;10,11,12}
$$
\end{document}