[Tex/LaTex] Issues with system of equations: systeme fix and align environment

alignequationssystemexstring

I'm writing some notes on linear algebra and to ease my work I've decided to use the systeme package. When I discovered that systeme has no built-in feature of writing systems of equations with dots and non-numeric coefficients, I've decided to use the fix suggested in this answer.

I need to write the full process of row-reduction of a specific system of equations, and I've done it before using the cases environment inside an align* environment. But the cases environment won't align the coefficients as desired, although each system is just where I wanted it to be. The fix suggested in this post yields an error when used inside the align* environment. What can I do?

A test sample for the error:

\documentclass{article}

\usepackage{amsmath}
\usepackage{xstring}

\def\typesystem#1{%
    \begingroup\expandarg
    \baselineskip=1.5\baselineskip% 1.5 to enlarge vertical space between lines
    \StrSubstitute{\noexpand#1}+{&+&}[\tempsystem]%
    \StrSubstitute\tempsystem={&=&}[\tempsystem]%
    \StrSubstitute\tempsystem,{\noexpand\cr}[\tempsystem]%
    \vcenter{\halign{&$\hfil\strut##$&${}##{}$\cr\tempsystem\crcr}}%
    \endgroup
}

\begin{document}

\begin{align*}
    \left\{\typesystem{3x + 2y = 1, x - 3y = 2}\right.
\end{align*}

\end{document}

It yields:

! Argument of \@xs@next has an extra }.
<inserted text> 
                \par 
l.20 \end{align*}

My situation in which each system is where it's desired but the coefficients inside them are not aligned (using align* and cases):

enter image description here

Best Answer

I'd stay with systeme as the code for \typesystem is very faulty.

Anyway, an additional pair of braces will do:

\documentclass{article}
\usepackage{amsmath,xstring}

\newcommand\typesystem[1]{%
    \begingroup\expandarg
    \linespread{1.5}\selectfont
    \StrSubstitute{\noexpand#1}+{&+&}[\tempsystem]%
    \StrSubstitute\tempsystem-{&-&}[\tempsystem]%
    \StrSubstitute\tempsystem={&=&}[\tempsystem]%
    \StrSubstitute\tempsystem{\noexpand\missing}{{}&&}[\tempsystem]%
    \StrSubstitute\tempsystem,{\noexpand\cr}[\tempsystem]%
    \left\{\vcenter{\halign{&$\hfil\strut##$&${}##{}$\cr\tempsystem\crcr}}\right.%
    \endgroup
}
\newcommand\minus{{-}}
\newcommand{\missing}{}

\begin{document}
\begin{alignat*}{2}
{\typesystem{
  2x_1 - x_2 - x_3 = 1,
  4x_1 - 3x_2 + x_3 = 0,
  \minus x_1 + x_2 + 2x_3 = \minus2
}}
&& {}\xrightarrow{L_2\rightarrow L_2 - 2L_1}{}
&{\typesystem{
  2x_1 - x_2 - x_3 = 1,
{} - x_2 + 3x_3 = \minus2,
  \minus x_1 + x_2 + 2x_3 = \minus2
}}
\\
&& {}\xrightarrow{L_1\leftrightarrow L_3}{}
&{\typesystem{
  \minus x_1 +x_2 +2x_3 = \minus2,
{} - x_2 +3x_3 = \minus2,
  2x_1 -x_2 -x_3 = 1
}}
\\
&& {}\xrightarrow{L_3\rightarrow L_3 + 2L_1}{}
&{\typesystem{
  \minus x_1 +x_2 +2x_3 = \minus2,
{}  - x_2 +3x_3 = \minus2,
\missing  x_2 +3x_3 = \minus3
}}
\end{alignat*}

\end{document}

enter image description here

With systeme:

\documentclass{article}
\usepackage{amsmath,systeme}

\begin{document}
\begin{alignat*}{2}
\systeme{
  2x_1 - x_2 - x_3 = 1,
  4x_1 - 3x_2 + x_3 = 0,
  -x_1 + x_2 + 2x_3 = -2
}
&& {}\xrightarrow{L_2\rightarrow L_2 - 2L_1}{}
&\systeme{
  2x_1 - x_2 - x_3 = 1,
  -x_2 + 3x_3 = -2,
  -x_1 + x_2 + 2x_3 = -2
}
\\
&& {}\xrightarrow{L_1\leftrightarrow L_3}{}
&\systeme{
  -x_1 +x_2 +2x_3 = -2,
  -x_2 +3x_3 = -2,
  2x_1 -x_2 -x_3 = 1
}
\\
&& {}\xrightarrow{L_3\rightarrow L_3 + 2L_1}{}
&\systeme{
  -x_1 +x_2 +2x_3 = -2,
  -x_2 +3x_3 = -2,
  x_2 +3x_3 = -3
}
\end{alignat*}

\end{document}

enter image description here