[Tex/LaTex] Reduce a matrix to row echelon form:

matricestikz-matrix

How to create this matrix in latex?enter image description here

Best Answer

Since this is very quick to do (except for punching in the entries) here is a version with nicematrix. I agree with @fractal, though, that this is not the purpose of the site.

\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc, fit}
\begin{document}
\[\mathbf{B}=\left[\;\begin{NiceArray}{cccc|c}[name=B]
    1 & 0 & 2 & 0 & -1 \\
    0 & 1& 0 & 3 & 4\\
    0 & 0& 0 & 1 & 2\\
    0 & 0& 0 & 0 & 0\\
\end{NiceArray}\;\right]
\begin{tikzpicture}[remember picture, overlay,
c/.style={fit={(B-#1)},circle,draw,thick,inner sep=0.6pt,node contents={}}]
\path foreach \X in {1-1,2-2,3-4} {node [c=\X]};
\draw[very thin,gray] let \p1=($(B-1-2.south west)-(B-2-1.north east)$) in
 ([xshift=-\x1/2,yshift=-2*\y1/3]B-1-1.south west) 
 -| ([xshift=-\x1/2,yshift=-2*\y1/3]B-2-2.south west)
 -| ([xshift=-\x1/2,yshift=-2*\y1/3]B-3-4.south west)
 -- ([xshift=\x1/3,yshift=-2*\y1/3]B-3-4.south east)
 ([xshift=-\x1/2,yshift=-2*\y1/3]B-3-5.south west)
 -- ([xshift=\x1/2,yshift=-2*\y1/3]B-3-5.south east);
\end{tikzpicture}
\]
\end{document}

enter image description here

There are zillions of alternatives, some will require less packages at the expense of having less control, others will criticize the spacing here (but it is to reproduce the screen shot) and so on and so forth. The question whether such matrices really help the reader to understand what's going on is off-topic, this is merely to say that if you really need to it is easy to create such things.