Basic Problem
I'm trying to produce a command sequence \m
that
- Is called from within math mode, with a single argument
list
- "Replaces" all instances
,
and;
inlist
with&
and\\
(resp.) - Creates a
pmatrix
with output of 1 & 2 as its content
For example, the code
\[
\m{a,b;c,d}\m{x;y}
\]
should expand to be equivalent to the following:
\[
\begin{pmatrix}a&b \\ c&d\end{pmatrix}\begin{pmatrix}x \\ y\end{pmatrix}
\]
After spending more time on this than I should have, I came up with the following partial solution to the problem using xstring
:
\def\foo#1{
\StrSubstitute{#1}{,}{&}[\result]
\verbtocs{\bslashes}|\\|
\expandarg
\StrSubstitute{\result}{;}{ \bslashes }[\result]
}
\begin{document}
% Fairly complicated matrix input
\foo{a,b,c;d,e_{f,g},h;i,j_{1,e^n},k}
\[
\begin{pmatrix}
\result
\end{pmatrix}
\]
\end{document}
This is close to what I was hoping for, but I haven't come up with a way to use \foo
as a helper function for \m
to get exactly what I want. I was thinking that defining \m
in the preamble by
\def\m#1{
\foo{#1}
\begin{pmatrix}
\result
\end{pmatrix}
}
would work, but then even calling something as simple as \m{a}
in math mode will produce errors. It looks as though math mode attempts to parse the definition of \m
instead of letting \m
fully expand, but my understanding of how this works is too limited to know how to fix my code.
Is there a way to modify the definitions of \m
and \foo
to make them do what I want? Can I somehow escape math mode in the definition of \m
to avoid these errors?