Preventing math mode from parsing macro input as math?

Here's an implementation with expl3 functions:

\documentclass{article}
\usepackage{xparse,amsmath}
\ExplSyntaxOn
\NewDocumentCommand{\matlabmatrix}{m}
 {
  \asql_matlab_matrix:n { #1 }
 }

\seq_new:N \l_asql_rows_seq
\seq_new:N \l_asql_one_row_seq
\tl_new:N \l_asql_matrix_tl

\cs_new_protected:Npn \asql_matlab_matrix:n #1
 {
  % clear the token list variable containing the final data
  \tl_clear:N \l_asql_matrix_tl
  % split the argument at the semicolon
  \seq_set_split:Nnn \l_asql_rows_seq { ; } { #1 }
  % build one row at a time
  \seq_map_inline:Nn \l_asql_rows_seq
   {
    \__asql_build_row:n { ##1 }
   }
  % print the matrix
  \begin{pmatrix}
  \tl_use:N \l_asql_matrix_tl
  \end{pmatrix}
 }

% the inner function
\cs_new_protected:Npn \__asql_build_row:n #1
 {
  % split the input at commas
  \seq_set_split:Nnn \l_asql_one_row_seq { , } { #1 }
  % add the row to the token list variable
  % items are separated by &
  \tl_put_right:Nx \l_asql_matrix_tl
   { \seq_use:Nnnn \l_asql_one_row_seq { & } { & } { & } }
  % add also the \\ row terminator
  \tl_put_right:Nn \l_asql_matrix_tl { \\ }
 }
\ExplSyntaxOff

\begin{document}
\begin{gather*}
\matlabmatrix{a,b;c,d}\matlabmatrix{x;y} \\
\matlabmatrix{a,b,c;d,e_{f,g},h;i,j_{1,e^n},k}
\end{gather*}
\end{document}

It's quite straightforward: we first split the argument at semicolons; then each item is split at commas and, row by row, the contents of the matrix is built. Finally the contents is inserted between \begin{pmatrix} and \end{pmatrix} for printing.

enter image description here


This code should do what you want:

\documentclass{article}
\usepackage{xstring,amsmath}
\newcommand*\mmm[1]{%
    \begingroup\expandarg
    \StrSubstitute{\noexpand#1},&[\result]%
    \StrSubstitute\result{\noexpand;}{\noexpand\\}[\result]%
    \begin{pmatrix}\result\end{pmatrix}\endgroup
}
\begin{document}
\[\mmm{a,b,c;d,e_{f,g},h;i,j_{1,e^n},k}\]

\[\mmm{a}\]
\end{document}

Here is a method the old-fashioned way, using macros with delimited parameters.

\documentclass{article}
\usepackage{amsmath} % for pmatrix environment

\newtoks\asqltoks
\makeatletter

\def\gobtilundef #1\undef {}

\def\matlabmatrix #1{\asqltoks{\begin{pmatrix}}\@asqlA #1;\undef;}

% \def\@asqlA #1;{\gobtilundef #1\@asqlE\undef\@asqlR #1,\undef,}
% update: simplified to ->
\def\@asqlA #1;{\@asqlR #1,\undef,}

\def\@asqlB #1;{\gobtilundef #1\@asqlE\undef
                \asqltoks\expandafter{\the\asqltoks \\}\@asqlR #1,\undef,}

\def\@asqlE\undef #1\undef,\undef,{%
    \asqltoks\expandafter{\the\asqltoks\end{pmatrix}}\the\asqltoks }

\def\@asqlR #1,{\asqltoks\expandafter{\the\asqltoks #1}\@asqlS }
\def\@asqlS #1,{\gobtilundef #1\@asqlZ\undef
                \asqltoks\expandafter{\the\asqltoks &#1}\@asqlS }
\def\@asqlZ #1\@asqlS {\@asqlB }

\makeatother

\begin{document}

$\matlabmatrix {m}$

$\matlabmatrix {m,n}$

$\matlabmatrix {m,n;p,q}$

$\matlabmatrix {a,b,c;d,e_{f,g},h;i,j_{1,e^n},k}$

\end{document}

matrices