5

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

enter image description here

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$
reads :
$
\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 ?

6

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:

enter image description here

\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}
8

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$
reads:
\systeme{
 -x+2y-3z=x,
 4x-z=y,
 7x+8y+9z=z
}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.