Problems with def

You can use xparse for this job, because the r argument type takes care of nesting.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\twomate}{>{\SplitArgument{3}{,}}r()}{%
  \maketwomate#1%
}
\NewDocumentCommand{\maketwomate}{mmmm}{%
  \begin{pmatrix}#1&#2\\#3&#4\end{pmatrix}%
}
\NewDocumentCommand{\pwr}{r()}{^{#1}}

\begin{document}

\[
\twomate(3e\pwr(-3t),e\pwr(2t),-e\pwr(-3t),-2e\pwr(2t) )
\]

\end{document}

However, using () as delimiters doesn't seem a good idea. The following is as clear and behaves better with syntax coloring of front ends.

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\NewDocumentCommand{\twomate}{>{\SplitArgument{3}{,}}m}{%
  \maketwomate#1%
}
\NewDocumentCommand{\maketwomate}{mmmm}{%
  \begin{pmatrix}#1&#2\\#3&#4\end{pmatrix}%
}
\NewDocumentCommand{\pwr}{m}{^{#1}}

\begin{document}

\[
\twomate{3e\pwr{-3t},e\pwr{2t},-e\pwr{-3t},-2e\pwr{2t}}
\]

\end{document}

enter image description here


Your \def contains a very specific sequence defined as the parameter text:

%           1   2   3   4   5
\def\twomate(<1>,<2>,<3>,<4>){ ... }
%           ^   ^   ^   ^   ^
%           │   │   │   │   │
%           │   └ comma ┘   │
%           └─── bracket ───┘

This parameter text is matched exactly in order (almost like a first-come-first-served style) to extract the four arguments <1>, <2>, <3> and <4>. Here's how the elements are grabbed for \twomate with the above notation:

%       1           2         3           4          5
\twomate(3e\pwr(-3t),e\pwr(2t),-e\pwr(-3t),-2e\pwr(2t))
%       ^           ^         ^           ^          ^
%       │           │         │           │          │
%       │           └────── comma ────────┘          │
%       └───────────────── bracket ──────────────────┘

It should be clear that the last bracket ) isn't properly captured for \pwr. The way around it is to hide \pwr(.) from \twomate:

\documentclass{article}

\usepackage{amsmath}

\def\twomate(#1,#2,#3,#4){\begin{pmatrix}#1&#2\\#3&#4\end{pmatrix}}
\def\pwr(#1){^{#1}}

\begin{document}

\[
  \twomate(3e\pwr(-3t),e\pwr(2t),-e\pwr(-3t),{-2e\pwr(2t)})
\]

\end{document}

This solves the problem only temporarily. If you nest elements, you'll run into similar problems because of the parameter text pattern matching. In general, it is safer to group arguments using {...}.


TeX's delimited parameters parsing doesn't take nesting into account properly. So if you call \twomate(3e\pwr(-3t), ...), the final ) for \twomate isn't found at the end of that line but at the end of \pwr(-3t). The improperly formed call of \pwr then causes trouble.

To hide nested calls of your commands, put them into { ... } groups (though this probably defeats the purpose):

\documentclass{article}
\usepackage{amsmath}

\def\twomate(#1,#2,#3,#4){\begin{pmatrix}#1&#2\\#3&#4\end{pmatrix}}
\def\pwr(#1){^{#1}}
\def\twoclmn(#1,#2){\begin{pmatrix}#1\\#2\end{pmatrix}}

\begin{document}
\[ \twomate({3e\pwr(-3t)},{e\pwr(2t)},{-e\pwr(-3t)},{-2e\pwr(2t)}) \]
\end{document}

enter image description here

By the way, don't use $$ ... $$ for display math environments but LaTeX's or amsmath's variants like \[ ... \].