MWE:
\documentclass{article}
\usepackage{mathtools}
\begin{document}
\ExplSyntaxOn
\tl_new:N \g__mymodule_env_name_tl
\tl_new:N \g__mymodule_env_args_tl
% Defaults
\tl_set:Nn \g__mymodule_env_name_tl { matrix }
\keys_define:nn { mymodule/math } {
,env .tl_set:N = \g__mymodule_env_name_tl
,env-args .tl_set:N = \g__mymodule_env_args_tl
}
\NewDocumentCommand{\mat}{ O{} m }{
\keys_set:nn { mymodule/math } { #1 }
\begin{\g__mymodule_env_name_tl}\g__mymodule_env_args_tl
#2
\end{\g__mymodule_env_name_tl}
}
\NewDocumentEnvironment{funnymatrix}{ s O{} m }{
\begin{matrix}
\text
{
(Star:~\bool_if:nTF { #1 } { T } { F })
(Optional:~#2)
(Mandatory:~#3)
}
}{
\end{matrix}
}
\ExplSyntaxOff
\section{Current output}
Environment \verb|matrix|:
\[\mat{1}\]
Environment \verb|matrix*| with optional argument:
\[\mat[env=matrix*,env-args=[l]]{1}\]
Environment \verb|funnymatrix| with a star, mandatory and optional argument:
\[\mat[env=funnymatrix,env-args=*[optional]{mandatory}]{1}\]
\section{Desired output}
Environment \verb|matrix|:
\[\mat{1}\]
Environment \verb|matrix*| with optional argument:
\[\begin{matrix*}[l]
1
\end{matrix*}\]
Environment \verb|funnymatrix| with a star, mandatory and optional argument:
\[\begin{funnymatrix}*[optional]{mandatory}
1
\end{funnymatrix}\]
\end{document}
Edit
Skillmon and egreg's solutions work very nicely, but they both seem to give a fatal error when compiling \mat[env=NiceArray,env-args={cc|cc}[baseline=line-3]]{1 & 1 & 1 & 1} (with the package nicematrix). How can either solution be modified such that this works?


[(or the corresponding character).\mat[env=NiceArray,env-args={cc|cc}[baseline=line-3]]{1 & 1 & 1 & 1}, has two problems:line-3does not exist because your matrix has only one row and you should also add braces for the whole value :\mat[env=NiceArray,env-args={{cc|cc}[baseline=line-3]]{1 & 1 & 1 & 1}}to protect the inner-most=