# how to draw a stair matrix using tikZ in Beamer?

I am very new to Beamer and LaTeX. I want to draw a stairs matrix as attached. It should be pretty simple but I just don't know how to draw it. Please only use tikZ (and matrix) and don't use other library. Thanks very much]1

• Welcome to Tex.SX. Helpers are ready to help if you could provide a minimum code that shows where you stumbled, please. – Jesse Feb 15 '16 at 7:56

Reasonably minimal:

\documentclass[tikz,border=5]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\tikz[x=1em, y=1em, inner sep=1em/6]]
\matrix [left delimiter={[}, right delimiter={]}] {
\draw (0,0) \foreach \i [count=\j] in {1,2,1,3,1,2,2,1,1}{
\ifnum\j>1-- ++(0,-1)\fi node [above right] (x-\j) {$x$} -- ++(\i,0)
}
(x-1 -| x-\j) node [left]  {possibly nonzero entries}
(x-1 |- x-\j) node [right] {zero entries};
\\};
\end{document}


• Thanks Wibrow! It's wonderful! That's exactly what I want! As a beginner, I also want to learn something for your code. Can you give me more explanation on \ifnum\j>1-- ++(0,-1)\fi node [above right] (x-\j) {$x$} -- ++(\i,0) or refer me a link? Thanks! – gaotx Feb 15 '16 at 16:25
• @gaotx pretty much everything is in the manual except the \ifnum...\fi trick which uses TeX integer comparison to insert a subpath -- ++(0,-1) (draw a line that is 1 unit below the last known point) if the value of \j is greater than 1. – Mark Wibrow Feb 15 '16 at 19:52

One option:

\documentclass[tikz,border=2mm]{standalone}

\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (A) [matrix of nodes, left delimiter=[,right delimiter={]}]{
x \\
& x\hspace*{3mm}\\
& & x \\
& & & x\hspace*{6mm}\\
& & & & x\\
& & & & & x\hspace*{3mm}\\
& & & & & & x\hspace*{3mm}\\
& & & & & & & x\\
& & & & & & & & x\\};
\foreach \i [remember=\i as \j (initially 1)] in {2,...,9}
\draw (A-\j-\j.south west) -|(A-\i-\i.south west);
\draw (A-9-9.south west)--(A-9-9.south east);
\node[anchor=south west] at ([shift={(5mm,5mm)}]A.south west) {zero entries};
\node[anchor=north east] at ([shift={(-3mm,-3mm)}]A.north east) {possibly nonzero entries};
\end{tikzpicture}
\end{document}