# Horizontal Line in Array

Good evening,

I'm trying to achieve following picture in latex, but i can't do it. Can someone help me?

This is what I have so far:

\documentclass[a4paper,12pt]{scrartcl}

\usepackage{booktabs}
\usepackage[utf8] {inputenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
\usepackage[T1]{fontenc}

\begin{document}

\begin{center}
$\left(\begin{array}{rrrr} 2&2&-1&3 \\ \cmidrule{2-4} 0&1&3&4 \\ 0&1&3&4 \\ 0&3&9&6 \\ 0&1&3&-2 \end{array}\right)$
\end{center}

\end{document}


You're missing the vertical line | in the column specification and a \multicolumn{1}{r} for the element in the top left corner:

\documentclass{article}

\begin{document}

$\left( \begin{array}{ r | *{3}{r} } \multicolumn{1}{r}{2} & 2 & -1 & 3 \\ \cline{2-4} 0 & 1 & 3 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 3 & 9 & 6 \\ 0 & 1 & 3 & -2 \end{array} \right)$

\end{document}


For a more evenly distributed spacing around the last three columns, consider using \phantom{-}2 in the first row:

\documentclass{article}

\begin{document}

$\left( \begin{array}{ r | *{3}{r} } \multicolumn{1}{r}{2} & \phantom{-}2 & -1 & 3 \\ \cline{2-4} 0 & 1 & 3 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 3 & 9 & 6 \\ 0 & 1 & 3 & -2 \end{array} \right)$

\end{document}

• Thank you very much ! Dec 30, 2020 at 21:03

With nicematrix. The environments of nicematrix create PGF/Tikz nodes under the rows, columns and cells and the array and it's possible to use them to draw whatever you want with Tikz.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

$\begin{pNiceMatrix}[r,right-margin,columns-width=auto] 2 & 2 & -1 & 3 \\ 0 & 1 & 3 & 4 \\ 0 & 1 & 3 & 4 \\ 0 & 3 & 9 & 6 \\ 0 & 1 & 3 & -2 \CodeAfter \tikz \draw ([xshift=4pt]last-|2) |- (2-|last) ; \end{pNiceMatrix}$

\end{document}


• Thank you very much! Dec 30, 2020 at 21:14
• Is it also possible to draw dotted lines ? Dec 30, 2020 at 21:18
• @Francesco: Yes. Put \draw [dotted] instead of \draw. Dec 30, 2020 at 21:29
• To my eyes the horizontal line is a little too low. Dec 30, 2020 at 21:58
• @Anush That's why this approach is more powerful. You only need e.g. \tikz \draw ([xshift=4pt]row-6-|col-2) |- ([yshift=2pt]row-2-|col-5) ;, or whatever yshift you like. And have access to many other things such as decorated lines.
– user232027
Jan 1, 2021 at 1:20