# Matrix in TikZ and Equation

The following code draw the matrix I want:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=0.1cm,column sep=0.1cm] (m) {
1 & *     &   *  & \dots & *   & 0 & \dots & 0 \\
0 & \pm 1 &   *  & \dots & *   & 0 & \dots & 0 \\
0 & 0     &\pm 1 & \dots & *   & 0 & \dots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   \dots   &\pm 1& 0 & \dots & 0 \\
0 &  0    & 0    &  \dots   & 0  & * & \dots & * \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   0   & 0   & * & \dots & *\\ };

\draw[dashed] ($0.5*(m-1-5.north east)+0.5*(m-1-6.north west)$) -- ($0.5*(m-8-6.south east)+0.5*(m-8-5.south west)$);

\draw[dashed] ($0.5*(m-5-1.south west)+0.5*(m-6-1.north west)$) -- ($0.5*(m-5-8.south east)+0.5*(m-6-8.north east)$);

\node[above=1pt of m-1-1] (top-1) {};
\node[above=1pt of m-1-5] (top-5) {};
\node[above=-1pt of m-1-6] (top-6) {};
\node[above=-1pt of m-1-8] (top-8) {};

\node[right=4pt of m-1-8] (right-1) {};
\node[right=4pt of m-5-8] (right-5) {};
\node[right=4pt of m-6-8] (right-6) {};
\node[right=4pt of m-8-8] (right-8) {};

\node[rectangle,above delimiter=\{] (del-top-1) at ($0.5*(top-1.south) +0.5*(top-5.south)$) {\tikz{\path (top-1.south west) rectangle (top-5.north east);}};
\node[above=10pt] at (del-top-1.north) {$k_1$};
\node[rectangle,above delimiter=\{] (del-top-2) at ($0.5*(top-6.south) +0.5*(top-8.south)$) {\tikz{\path (top-6.south west) rectangle (top-8.north east);}};
\node[above=10pt] at (del-top-2.north) {$k-k_1$};

\node[rectangle,right delimiter=\}] (del-right-1) at ($0.5*(right-1.west) +0.5*(right-5.west)$) {\tikz{\path (right-1.north east) rectangle (right-5.south west);}};
\node[right=22pt] at (del-right-1.west) {$k_1$};
\node[rectangle,right delimiter=\}] (del-right-2) at ($0.5*(right-6.west) +0.5*(right-8.west)$) {\tikz{\path (right-6.north east) rectangle (right-8.south west);}};
\node[right=22pt] at (del-right-2.west) {$k-k_1$};

\end{tikzpicture}

\end{document} Now since I want to insert it in a equation I used the baseline option for tikz:

\documentclass{article} \usepackage{tikz} \usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning} \begin{document} \begin{equation}  Q= \begin{tikzpicture}[baseline] \matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=0.1cm,column sep=0.1cm] (m) {
1 & *     &   *  & \dots & *   & 0 & \dots & 0 \\
0 & \pm 1 &   *  & \dots & *   & 0 & \dots & 0 \\
0 & 0     &\pm 1 & \dots & *   & 0 & \dots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   \dots   &\pm 1& 0 & \dots & 0 \\
0 &  0    & 0    &  \dots   & 0  & * & \dots & * \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   0   & 0   & * & \dots & *\\ };

\draw[dashed] ($0.5*(m-1-5.north east)+0.5*(m-1-6.north west)$) -- ($0.5*(m-8-6.south east)+0.5*(m-8-5.south west)$);

\draw[dashed] ($0.5*(m-5-1.south west)+0.5*(m-6-1.north west)$) -- ($0.5*(m-5-8.south east)+0.5*(m-6-8.north east)$);

\node[above=1pt of m-1-1] (top-1) {}; \node[above=1pt of m-1-5] (top-5) {}; \node[above=-1pt of m-1-6] (top-6) {}; \node[above=-1pt of m-1-8] (top-8) {};

\node[right=4pt of m-1-8] (right-1) {}; \node[right=4pt of m-5-8] (right-5) {}; \node[right=4pt of m-6-8] (right-6) {}; \node[right=4pt of m-8-8] (right-8) {};

\node[rectangle,above delimiter=\{] (del-top-1) at ($0.5*(top-1.south) +0.5*(top-5.south)$) {\tikz{\path (top-1.south west) rectangle (top-5.north east);}}; \node[above=10pt] at (del-top-1.north) {$k_1$}; \node[rectangle,above delimiter=\{] (del-top-2) at ($0.5*(top-6.south) +0.5*(top-8.south)$) {\tikz{\path (top-6.south west) rectangle (top-8.north east);}}; \node[above=10pt] at (del-top-2.north) {$k-k_1$};

\node[rectangle,right delimiter=\}] (del-right-1) at ($0.5*(right-1.west) +0.5*(right-5.west)$) {\tikz{\path (right-1.north east) rectangle (right-5.south west);}}; \node[right=22pt] at (del-right-1.west) {$k_1$}; \node[rectangle,right delimiter=\}] (del-right-2) at ($0.5*(right-6.west) +0.5*(right-8.west)$) {\tikz{\path (right-6.north east) rectangle (right-8.south west);}}; \node[right=22pt] at (del-right-2.west) {$k-k_1$};

\end{tikzpicture} \end{equation}

\end{document}


Unfortunately the result is this: What happened to the parenthesis?

• You are already using TikZ so you can remove those ugly and misaligned dots and draw it properly. – percusse May 11 '18 at 13:41
• could use a little negative \vspace above the rows with \vdots. otherwise looks good (aside from the glitch). – barbara beeton May 11 '18 at 15:14

You're nesting tikzpictures, that is often a bad idea. Here's an alternative approach using the fit library. \documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning,fit}
\begin{document}
\begin{equation}
Q=
\begin{tikzpicture}[
baseline,
]

\matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=0.1cm,column sep=0.1cm] (m) {
1 & *     &   *  & \dots & *   & 0 & \dots & 0 \\
0 & \pm 1 &   *  & \dots & *   & 0 & \dots & 0 \\
0 & 0     &\pm 1 & \dots & *   & 0 & \dots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   \dots   &\pm 1& 0 & \dots & 0 \\
0 &  0    & 0    &  \dots   & 0  & * & \dots & * \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   0   & 0   & * & \dots & *\\ };

\draw[dashed] ($0.5*(m-1-5.north east)+0.5*(m-1-6.north west)$) -- ($0.5*(m-8-6.south east)+0.5*(m-8-5.south west)$);

\draw[dashed] ($0.5*(m-5-1.south west)+0.5*(m-6-1.north west)$) -- ($0.5*(m-5-8.south east)+0.5*(m-6-8.north east)$);

\node[
fit=(m-1-1)(m-1-5),
inner xsep=0,
above delimiter=\{,
label=above:$k_1$
] {};

\node[
fit=(m-1-6)(m-1-8),
inner xsep=0,
above delimiter=\{,
label=above:$k-k_1$
] {};

\node[
fit=(m-1-8)(m-5-8),
inner xsep=15pt,inner ysep=0,
right delimiter=\},
label=right:$k_1$
] {};

\node[
fit=(m-6-8)(m-8-8),
inner xsep=15pt,inner ysep=0,
right delimiter=\},
label=right:$k-k_1$
] {};

\end{tikzpicture}
\end{equation}

\end{document}

• On the cover of the TikZ & PGF manual Till Tantau himself use tikzpictures in nodes, it is probably not always so bad as the people on TeX.SX are used to say ;) – Kpym May 11 '18 at 14:15
• @Kpym: is not so bad if you know what you are doing, otherwise one might end up in issues like tex.stackexchange.com/q/18647/13304 ;) – Claudio Fiandrino May 11 '18 at 15:14
• @Kpym My impression is that in most cases nesting is completely unnecessary anyway, so when it is a practice that can cause unexpected and "strange" problems if you don't know what you're doing, why not recommend against it? I don't see any nesting on the cover of the manual, by the way. (And it was Claudio who wrote that previous comment, so perhaps you should have notified him.) – Torbjørn T. May 13 '18 at 8:59

Since you are loading already the decorations.pathreplacing library and make all the preparations, I could not resist using them. ;-)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,decorations.pathreplacing, calc, positioning}
\begin{document}
\begin{equation}  Q=
\begin{tikzpicture}[baseline]
\matrix [matrix of math nodes,left delimiter=(,right delimiter=),row sep=0.1cm,column sep=0.1cm] (m) {
1 & *     &   *  & \dots & *   & 0 & \dots & 0 \\
0 & \pm 1 &   *  & \dots & *   & 0 & \dots & 0 \\
0 & 0     &\pm 1 & \dots & *   & 0 & \dots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   \dots   &\pm 1& 0 & \dots & 0 \\
0 &  0    & 0    &  \dots   & 0  & * & \dots & * \\
\vdots & \vdots & \vdots & \ddots & \vdots & \vdots & \ddots & \vdots\\
0 &  0    & 0    &   0   & 0   & * & \dots & *\\ };

\draw[dashed] ($0.5*(m-1-5.north east)+0.5*(m-1-6.north west)$) -- ($0.5*(m-8-6.south east)+0.5*(m-8-5.south west)$);

\draw[dashed] ($0.5*(m-5-1.south west)+0.5*(m-6-1.north west)$) -- ($0.5*(m-5-8.south east)+0.5*(m-6-8.north east)$);

\node[above=1pt of m-1-1] (top-1) {};
\node[above=1pt of m-1-5] (top-5) {};
\node[above=-1pt of m-1-6] (top-6) {};
\node[above=-1pt of m-1-8] (top-8) {};

\node[right=10pt of m-1-8] (right-1) {};
\node[right=10pt of m-5-8] (right-5) {};
\node[right=10pt of m-6-8] (right-6) {};
\node[right=10pt of m-8-8] (right-8) {};

\draw[thick,decorate,decoration=brace] (top-1.west) -- (top-5.east)
node[midway,above=1mm] {$k_1$};

\draw[thick,decorate,decoration=brace] (top-6.west) -- (top-8.east)
node[midway,above=1mm] {$k-k_1$};

\draw[thick,decorate,decoration=brace] (right-1.north) -- (right-5.south)
node[midway,right=1mm] {$k_1$};

\draw[thick,decorate,decoration=brace] (right-6.north) -- (right-8.south)
node[midway,right=1mm] {$k-k_1$};

\end{tikzpicture}
\end{equation}

\end{document} 