[Tex/LaTex] Best way to create an system of equations environment

horizontal alignmentmath-modetables

I am trying to write a few systems of equations, and I want the terms to be nicely spaced as below

2x  +   y  +  3z  =  10   \\ 
 x  +   y  +   z  =   6   \\
 x  +  3y  +  2z  =  13 

Now, using some very ugly code, I was able to produce the results above.

\documentclass[10pt,a4paper]{article}
\usepackage{mathtools}
\usepackage{amsmath}

\begin{document}

\begin{align*}
\begin{bmatrix}
\begin{tabular}{r c r c r c r }
$2x$ & $+$ & $ y$ & $+$ & $3z$ & $=$ & $10$ \\
$ x$ & $+$ & $ y$ & $+$ & $ z$ & $=$ & $ 6$ \\
$ x$ & $+$ & $3y$ & $+$ & $2z$ & $=$ & $13$
\end{tabular}
\end{bmatrix}
\end{align*}

\section{Another system of equations, now without the brackets}

\begin{table}[!htpb]
\centering
\begin{tabular}{r c r c r c r }
$2x$ & $+$ & $ y$ & $+$ & $3z$ & $=$ & $10$ \\
$ x$ & $+$ & $ y$ & $+$ & $ z$ & $=$ & $ 6$ \\
$ x$ & $+$ & $3y$ & $+$ & $2z$ & $=$ & $13$
\end{tabular}
\end{table}

\end{document}    

I would very much want a more automatic way to do this, and a simple method for controlling the spacing between elements. I have looked at earlier posts like

Multicol layout for systems of (linear) equations

It seems that I am looking for a simple version of this one. I have no need to use side-by-side equations, nor have numbers in front of them.

Using ideas from the post above, I guess the result is done by redefining commands such as - and + inside of the table? I tried to do something like this, but the code looked rather complex =(

To sum it up: Is there a way to define a simple System of Equations enviroment, with proper alignment and a optional command for defining the spacing?

Best Answer

You can try the package systeme. Its documentation is in French, but there are many examples to play with.

Your example would be input as

\systeme{
2x  +   y  +  3z  =  10,
 x  +   y  +   z  =   6,
 x  +  3y  +  2z  =  13}

To get right alignment in the column of right hand sides, one has to manually modify the package code:

\makeatletter
\def\SYS@makesyspreamble@i#1{%
  \ifnum#1<\SYS@preamblenum
    \SYS@addtotok\SYS@systempreamble{\hfil$##$&\hfil$##$&}% 
    \expandafter\SYS@makesyspreamble@i\expandafter{\number\numexpr#1+\@ne\expandafter}%
  \else
    \SYS@addtotok\SYS@systempreamble{\hfil$##$&$##$&\hfil$##$\null}% 
    \ifSYS@extracol
      \SYS@addtotok\SYS@systempreamble{&\SYS@extracolstart##\SYS@extracolend\hfil\null}% 
    \fi
    \SYS@addtotok\SYS@systempreamble{\cr\SYS@strutup}% 
  \fi
}
\makeatother

The patch is simply changing $##$\hfil into \hfil$##$ but, since this involves # it's not possible to use etoolbox's \patchcmd.

One can modify the distance between the lines by saying something like

\syslineskipcoeff{1.2}

and act on the column spacing with the parameter \tabskip; so, for example,

\[
\syslineskipcoeff{1.2}\setlength{\tabskip}{3pt}
\systeme{
2x  +   y  +  3z  =  10,
 x  +   y  +   z  =   6,
 x  +  3y  +  2z  =  13}
\]

will spread out the equations both vertically and horizontally. The \syslineskipcoeff can also be issued globally, in the preamble of the document; not the horizontal spacing, though, as \tabskip influences all TeX tables, tabular environments included.

Related Question