# Bespoke environment for systems of linear equations

## My problem

I often write systems of linear equations, with the following alignment requirements :

• all multiple of the unknowns should be right-aligned
• all +, - and = operation signs should be centered, with proper left/right spacing for those "fully-featured" equations

## My original MWE :

\documentclass[12pt,border=10pt]{standalone}
\usepackage{mathtools}
\begin{document}
My matrix is :
$M = \begin{bmatrix*}[r] -1 & 2 & -3 \\ 4 & 0 & -1 \\ 7 & 8 & 9 \\ \end{bmatrix*}$
thus the equation $M X = X$
$\def\arraycolsep{0pt} \left \{ \begin{array}{*3{rc}r} - x & {}+{} & 2 y & {}-{} & 3 z & {}={} & x \\ 4 x & & & {}-{} & z & {}={} & y \\ 7 x & {}+{} & 8 y & {}+{} & 9 z & {}={} & z \\ \end{array} \right.$
\end{document}


## My wish

I do not see how to do that using the environments defined in the amsmath or mathtools package

What I would like is to have a linearEquations environment enabling me to simply write :

\begin{linearEquations}{3}
- x & + & 2 y & - & 3 z & = & x \\
4 x &   &     & - &   z & = & y \\
7 x & + & 8 y & + & 9 z & = & z \\
\end{linearEquations}


to produce the same result.

## My attempt

What I have tried so far is something like :

\newenvironment{linearEquations}[1]
{
%before
\def\arraycolsep{0pt}
\left \{
\begin{array}{*#1{r@{{}}c@{{}}}r}
}
{
%after
\end{array}
\right.
}


but it does not yield the proper {} spacing before/after the operation signs

Can you help ? Cheers,

## A possible solution

As per Werner's answer, the line \begin{array}{*#1{r@{\:}c@{\:}}r} seems to work in this case, but maybe only does so in certain cases ?

I suggest making two changes to the setup of the linearEquations environment:

• use \setlength\arraycolsep{0pt}, not \def\arraycolsep{0pt}; and

• define a new column type called, say, C for the columns of binary and relational operators:

\newcolumntype{C}{>{{}}c<{{}}}


With these modifications, you custom environment should be fine:

\documentclass{article}
\usepackage{array} % for '\newcolumntype' macro
\newcolumntype{C}{>{{}}c<{{}}}
\newenvironment{linearEquations}[1]{%
%before
\setlength\arraycolsep{0pt}
\left\{\,
\begin{array}{*{#1}{rC}r}}{%
%after
\end{array}
\right.
}
\begin{document}
$\begin{linearEquations}{3} - x & + & 2 y & - & 3 z & = & x \\ 4 x & & & - & z & = & y \\ 7 x & + & 8 y & + & 9 z & = & z \\ \end{linearEquations}$
\end{document}


Something like this?

\documentclass[12pt,border=10pt]{standalone}
\usepackage{mathtools}
\usepackage{systeme}
\begin{document}
My matrix is:
$M = \begin{bmatrix*}[r] -1 & 2 & -3 \\ 4 & 0 & -1 \\ 7 & 8 & 9 \\ \end{bmatrix*}$
thus the equation $M X = X$