0

Edit: I swapped \begin{aligned} &\ro{r_2+-1r_1}\\ &\end{aligned} with &\ro{r_2+-1r_1}\\ , added some linebreaks an now it looks quite well.

I am working on a python matrix editor(it's still WIP) which outputs matrix equations as LaTex. In this case I tried to calculate an inverse matrix. The matrices should align beneath one another, but the line continues beyond the document.

Screenshot of the LaTex document in Overleaf

I also tried adding linebreaks(\\and\newline) but it didn't turn out as I hoped. enter image description here

I want it to look something like this but better aligned.(This format doesn't always work, and causes a huge amount of warning/error messages)

enter image description here

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

\usepackage{nicefrac}

\newenvironment{sysmatrix}[1]
 {\left(\begin{array}{@{}#1@{}}}
 {\end{array}\right)}
\newcommand{\ro}[1]{%
  \xrightarrow{\mathmakebox[\rowidth]{#1}}%
}
\newlength{\rowidth}% row operation width
\AtBeginDocument{\setlength{\rowidth}{3em}}

\begin{document}

\begin{alignat*}{2}
\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
1 & 1 & -1 & -1 & 0 & 1 & 0 & 0 \\
1 & -1 & 1 & -1 & 0 & 0 & 1 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_2+-1r_1}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
1 & -1 & 1 & -1 & 0 & 0 & 1 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_3+-1r_1}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_2\leftrightarrow r_3}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_4+-1r_1}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & -2 & -2 & 0 & -1 & 0 & 0 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_4+-1r_2}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & -2 & 2 & 0 & 0 & -1 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_4+-1r_3}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_1+1/2r_2}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & 0 & 1 & 0 & \dfrac{\strut 1}{\strut 2} & 0 & \dfrac{\strut 1}{\strut 2} & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_1+1/2r_2}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & -1 & 1 & -1 & 0 & 0 & 1 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}
&\begin{aligned}
&\ro{r_1+1/2r_2}\\
&\end{aligned}
\begin{sysmatrix}{rrrr|rrrr}
1 & -2 & 1 & -2 & \dfrac{\strut -1}{\strut 2} & 0 & \dfrac{\strut 3}{\strut 2} & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}
\end{alignat*}
\end{document}

I'm still very new to LaTex and don't really know much about formating... I hope you can help me.

3
  • 1
    what is the intention of \begin{aligned} &\ro{r_2+-1r_1}\\ &\end{aligned} a single row, single column alignment can't do anything useful? Jan 3 at 22:53
  • And what does +- in r_2+-1r_1 and other places denote? "Plus/minus", maybe? If so, it would be better to write \pm.
    – Mico
    Jan 3 at 23:09
  • Thank you for the advice, it look better already
    – TheBaum
    Jan 4 at 11:36
1

Simplifying the markup, and squeezing the white space in the arrays, I also widened teh text block.

enter image description here

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

\addtolength\oddsidemargin{-1cm}
\addtolength\textwidth{2cm}
\usepackage{nicefrac}

\newenvironment{sysmatrix}[1]
 {\setlength\arraycolsep{3pt}
\left(\begin{array}{@{}#1@{}}}
 {\end{array}\right)}
\newcommand{\ro}[1]{%
  \xrightarrow{\mathmakebox[\rowidth]{#1}}%
}
\newlength{\rowidth}% row operation width
\AtBeginDocument{\setlength{\rowidth}{3em}}
\begin{document}

\begin{alignat*}{2}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
1 & 1 & -1 & -1 & 0 & 1 & 0 & 0 \\
1 & -1 & 1 & -1 & 0 & 0 & 1 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}
&\ro{r_2+-1r_1}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
1 & -1 & 1 & -1 & 0 & 0 & 1 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}\\
\ro{r_3+-1r_1}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}
&\ro{r_2\leftrightarrow r_3}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
1 & -1 & -1 & 1 & 0 & 0 & 0 & 1 \\
\end{sysmatrix}\\
\ro{r_4+-1r_1}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & -2 & -2 & 0 & -1 & 0 & 0 & 1 \\
\end{sysmatrix}
&\ro{r_4+-1r_2}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & -2 & 2 & 0 & 0 & -1 & 1 \\
\end{sysmatrix}\\
\ro{r_4+-1r_3}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 1 & 1 & 1 & 1 & 0 & 0 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}
&\ro{r_1+1/2r_2}
&\begin{sysmatrix}{rrrr|rrrr}
1 & 0 & 1 & 0 & \dfrac{\strut 1}{\strut 2} & 0 & \dfrac{\strut 1}{\strut 2} & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}\\
\ro{r_1+1/2r_2}
&\begin{sysmatrix}{rrrr|rrrr}
1 & -1 & 1 & -1 & 0 & 0 & 1 & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}
&\ro{r_1+1/2r_2}
&\begin{sysmatrix}{rrrr|rrrr}
1 & -2 & 1 & -2 & \dfrac{\strut -1}{\strut 2} & 0 & \dfrac{\strut 3}{\strut 2} & 0 \\
0 & -2 & 0 & -2 & -1 & 0 & 1 & 0 \\
0 & 0 & -2 & -2 & -1 & 1 & 0 & 0 \\
0 & 0 & 0 & 4 & 1 & -1 & -1 & 1 \\
\end{sysmatrix}
\end{alignat*}
\end{document}
2
  • Thank you very much, this solved my main problem. I still got two little Problems. 1. If I use a lot more matrices, they continue beyond the bottom of the Page. I tried \pagebreak and \newpage but they don't work so well... 2. There is a blank page at the beginning of the document and I don't know how to remove it with LateX.
    – TheBaum
    Jan 4 at 11:43
  • \allowdisplaybreaks will allow page breaks. @TheBaum Jan 4 at 11:47

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.