4

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

  • welcome to tex.sx. i'm a bit puzzled as to what, exactly, you would like to be lined up. is it that (for example) all the "case braces" on the right-hand side should be in line vertically, or that all instances of x_1 on the left side should end at the same horizontal point (and similarly within the cases on the right-hand side), or a combination of all these things? or something else? i'm thinking that alignat* might be more appropriate here, at least with regard to the three cases on the right-hand side. – barbara beeton Aug 12 '16 at 15:44
  • @barbarabeeton thanks! Ideally, I would like everything lined up - the arrows having all the same length, the case braces in line vertically and all instances of the variables (even when they are missing) to end at the same horizontal point. But if only the last one is achievable (or at least without much manual whitespace tweaking), that's fine for me. I'll have to write lots of systems, some of them with dots and non-numeric coefficients, so efficiency comes first here. – Henrique Augusto Souza Aug 12 '16 at 15:51

2 Answers2

3

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

egreg
  • 1,121,712
  • it is probably overkill, but wouldn't phantom minus signs be appropriate in the right-hand side for the non-negative equalities? – barbara beeton Aug 12 '16 at 19:25
  • @barbarabeeton I think that \typesystem is inadequate anyway, so I didn't bother too much. A better definition of \minus is \newcommand{\minus}{{-}}, which I edited in. – egreg Aug 12 '16 at 19:26
  • uh, i was actually referring to the example with \systeme, since you had explicitly deprecated \typesystem. (i did read your explanations.) – barbara beeton Aug 12 '16 at 19:43
  • @barbarabeeton Oh, I misread your comment. There's an answer of mine for getting right alignment in the right-hand side: http://tex.stackexchange.com/a/247078/4427 – egreg Aug 12 '16 at 20:00
  • that's a nice one! mention it in your answer. – barbara beeton Aug 12 '16 at 20:08
2

Here is a tabstackengine alternative for presenting systems of equations. However, because we are using a space as the term separator, the syntax is rigid and one cannot freely throw in stray blank spaces. In general, things to remember are:

  1. if one wants the operators (+, -, =) to line up, then they can not attached to either term (i.e., must form their own column); If right alignment of the terms is all that is required, without operator alignment, then operators can be placed next to leading or trailing terms as long as you are consistent in the syntax across all equations of the system;

  2. {} is used as a placeholder for an empty term;

  3. In order to achieve this syntax, leading minus signs will be treated as a binary operation, unless presented as {-};

  4. stray spaces are a no-no, including leading and trailing spaces, as well as those surrounding the comma , equation separator.

Here is the MWE.

In response to barbara's comment, here is what the result looks like if I group the leading minus signs as {-} instead of -, to prevent them from being treated as binary operators. Additionally, to show flexibility of the solution, the inter-line baselineskip can be set in the preamble, e.g., \setstackgap{L}{1.2\normalbaselineskip}

\documentclass{article}
\usepackage{amsmath}
\usepackage{tabstackengine}
\def\typesystem#1#2{\savestack{#1}{\setstackEOL{,}\setstackTAB{ }
  $\left\{\ensurestackMath{\tabbedCenterstack[r]{#2}}\right.$}}
\TABbinary
\stackMath
\setstackgap{S}{12pt}
\setstackgap{L}{1.2\normalbaselineskip}
\begin{document}
\typesystem{\systemA}{2x_1  - x_2  - x_3  =    1,4x_1 - 3x_2 +  x_3 =    0,{-}x_1 + x_2 + 2x_3 = {-2}}
\typesystem{\systemB}{2x_1  - x_2  - x_3  =    1,{}   - x_2  + 3x_3 = {-}2,{-}x_1 + x_2 + 2x_3 = {-2}}
\typesystem{\systemC}{{-}x_1 + x_2 + 2x_3 = {-2},{}   - x_2  + 3x_3 = {-}2,2x_1   - x_2 - x_3  =  1}
\typesystem{\systemD}{{-}x_1 +x_2  +2x_3  = {-2},{}   -x_2   +3x_3  = {-}2,{} x_2 +3x_3        = {-3}}
\typesystem{\systemE}{{-}x_1 +x_2  +2x_3  = {-2},{}   -x_2   +3x_3  = {-}2,{} {}  +6x_3        = {-5}}

\systemA\quad
\alignShortunderstack{%
  \xrightarrow{L_2\rightarrow L_2 - 2L_1}&\systemB\\
  \xrightarrow{L_1\leftrightarrow L_3}&\systemC\\
  \xrightarrow{L_3\rightarrow L_3 + 2L_1}&\systemD\\
  \xrightarrow{L_3\rightarrow L_3 + L_2}&\systemE
}
\end{document}

enter image description here

  • Thanks for the suggestion! I gave it a try, and it also works fine with dots (vdots included). I couldn't make it work straight away with non-numerical coefficients, but I'll lookup it's documentation to see what may be done. – Henrique Augusto Souza Aug 12 '16 at 16:35
  • @HenriqueAugustoSouza Since I am the package author, I would be happy to look further, if I can understand the requirements better. – Steven B. Segletes Aug 12 '16 at 17:00
  • @HenriqueAugustoSouza Please see my revision. – Steven B. Segletes Aug 12 '16 at 17:07
  • small quibble ... the space between the braces and what follows looks too "generous" to me. – barbara beeton Aug 12 '16 at 17:42
  • @barbarabeeton As I said in my answer (point 3), leading minus signs are treated as binary, unless grouped separately as {-}. See addendum. – Steven B. Segletes Aug 12 '16 at 17:54
  • binary or unary, that's irrelevant; the binary nature isn't in respect to the "case" braces, only to what follows. that said, the "tightened" version looks a lot better (and more "normal") to me. thanks. – barbara beeton Aug 12 '16 at 18:21
  • @barbarabeeton You can say it is irrelevant, but compare x${}-Z$\par x$-Z$ to see why the extra space is introduced by adding a blank group before the - – Steven B. Segletes Aug 12 '16 at 18:25
  • @StevenB.Segletes I'll stay with the other solution for now as it doesn't involve changing my current package selection and editing older files much. I thanks for your suggestion, and I'll keep in mind this alternative! Using non-numerical coefficients on the linear system seems still a major feature missing in systeme, and if it is/becomes a feature oftabstackengine I will be surely very inclined to give it a try, as I'll need it a lot soon (to write arbitrary systems, with equations such as a_{11}x_1 + \cdots + a_{1n}x_n = b_n, for example). – Henrique Augusto Souza Aug 12 '16 at 18:56
  • please compare the content of these two components: on the right side, the first line of the first case group, and the third line of the second case group. the purported content is identical, but the presentation is different. taking into account your warnings, i tried to make the output consistent, but everything i tried only made things worse. it seems that this approach is quite fragile. how might one fix these two elements to make the presentation the same? – barbara beeton Aug 12 '16 at 19:03
  • @barbarabeeton I realize if one wants to line up all the operators, they must be in their own columns, separated by spaces (unless the special case arises that all coefficients are equal width). – Steven B. Segletes Aug 12 '16 at 19:21