# using tikz for rectangle in matrix

\begin{equation*}
U^{k} = \begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
{
z^{k}     &  0 & \cdots & s z^{0} & s z^{1} & \cdots & s z^{k-2} & s    z^{k-1} \\
z^{k-1} s &  \vdots&    & -\overline{z} & s^{2} z^{0}   & \cdots & s^{2} z^{k-3} & s^{2} z^{k-2} \\
z^{k-2} s &   & &  & -\overline{z} & s^{2} z^{0} & \cdots & s^{2} z^{k-3} \\
\vdots    &   & &  &   & \ddots & \ddots & \vdots \\
z s       & \vdots&  &   & 0 &   & -\overline{z} & s^{2} z^{0} \\
s         & 0 & \cdots &  &   &   &   & -\overline{z} \\
0         & 1 &   & 0 &  &  &   &   \\
\vdots    &   & \ddots & &  &   & 0 &   \\
0         & 0 &   & 1 &  & &   &   \\
};
\draw (m-2-4.north west) rectangle (m-6-8.south east);
\draw (m-7-2.north west) rectangle (m-9-4.south east);
\end{tikzpicture}
\end{equation*}


thats my code and the result

the one rectangle is to small. any ideas? and to have the "U^k=" in the middle?

-
Welcome to TeX.SX! You may want to have a look to Array cell highlighting tikz although the matrix is not directly typeset with TikZ. Other methods are illustrated in Highlight elements in the matrix. To have U^k in the middle, have a look to use of tikzpicture matrix in align or gather environment. –  Claudio Fiandrino Aug 27 at 10:55

To bring U^k= to the middle use [baseline={(m.center)}] as the option to the \begin{tikzpicture}. and to make the rectangle bigger use (m-6-8.south east-|m-2-8.north east) in

 \draw (m-2-4.north west) rectangle (m-6-8.south east-|m-2-8.north east);


which means x coordinate same as m-2-8.north east and y coordinate same as that of m-6-8.south east.

Code:

\documentclass{article}
\usepackage{tikz,amsmath}
\usetikzlibrary{matrix}
\begin{document}
\begin{equation*}
U^{k} = \begin{tikzpicture}[baseline={(m.center)}]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=)] (m)
{
z^{k}     &  0 & \cdots & s z^{0} & s z^{1} & \cdots & s z^{k-2} & s    z^{k-1} \\
z^{k-1} s &  \vdots&    & -\overline{z} & s^{2} z^{0}   & \cdots & s^{2} z^{k-3} & s^{2} z^{k-2} \\
z^{k-2} s &   & &  & -\overline{z} & s^{2} z^{0} & \cdots & s^{2} z^{k-3} \\
\vdots    &   & &  &   & \ddots & \ddots & \vdots \\
z s       & \vdots&  &   & 0 &   & -\overline{z} & s^{2} z^{0} \\
s         & 0 & \cdots &  &   &   &   & -\overline{z} \\
0         & 1 &   & 0 &  &  &   &   \\
\vdots    &   & \ddots & &  &   & 0 &   \\
0         & 0 &   & 1 &  & &   &   \\
};
\draw (m-2-4.north west) rectangle (m-6-8.south east-|m-2-8.north east);
\draw (m-7-2.north west) rectangle (m-9-4.south east);
\end{tikzpicture}
\end{equation*}
\end{document}


-
\begin{tikzpicture}[baseline] because m.center is already the baseline reference. –  Ignasi Aug 27 at 12:21
@Ignasi I wanted to be explicit ;-). You are right. :-) –  Harish Kumar Aug 27 at 12:22
@Ignasi not if you have bottom delimiters or other items inside the picture. –  percusse Aug 27 at 13:05
This one worked very well for me, thank you very much. –  mfg Aug 27 at 15:13
@percusse My fault. I just tested with the matrix. –  Ignasi Aug 27 at 17:16

I've discovered easy bundle in CTAN and this is a good question to test easybmat package.

As usual you need to know columns number but also rows number. It's possible to select different alignemt (l,c,r) for rows and columns. And it's possible to draw paths between elements.

\documentclass{article}
\usepackage{amsmath}
\usepackage{easybmat}

\begin{document}
\begin{equation*}
U^{k} = \left(\begin{BMAT}{cccccccc}{ccccccccc}
z^{k}     &  0 & \cdots & s z^{0} & s z^{1} & \cdots & s z^{k-2} & s    z^{k-1} \\
z^{k-1} s &  \vdots&    & -\overline{z} & s^{2} z^{0}   & \cdots & s^{2} z^{k-3} & s^{2} z^{k-2} \\
z^{k-2} s &   & &  & -\overline{z} & s^{2} z^{0} & \cdots & s^{2} z^{k-3} \\
\vdots    &   & &  &   & \ddots & \ddots & \vdots \\
z s       & \vdots&  &   & 0 &   & -\overline{z} & s^{2} z^{0} \\
s         & 0 & \cdots &  &   &   &   & -\overline{z} \\
0         & 1 &   & 0 &  &  &   &   \\
\vdots    &   & \ddots & &  &   & 0 &   \\
0         & 0 &   & 1 &  & &   &