# 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,
label distance=10pt % added
]

\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}
{"ft_lang_label":"__label__en","ft_lang_prob":0.5400206,"math_prob":0.99836546,"size":4501,"snap":"2019-35-2019-39","text_gpt3_token_len":1931,"char_repetition_ratio":0.19790971,"word_repetition_ratio":0.87223166,"special_character_ratio":0.46123084,"punctuation_ratio":0.17087968,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96711427,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,3,null,3,null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-09-19T04:07:13Z\",\"WARC-Record-ID\":\"<urn:uuid:4186ca76-0687-4815-8b2b-7ee9dd9dd385>\",\"Content-Length\":\"154507\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:09f96d28-d63f-43c0-a265-84b3c123aa35>\",\"WARC-Concurrent-To\":\"<urn:uuid:f911e5f0-a3ef-40dc-aef5-a01b383f8897>\",\"WARC-IP-Address\":\"151.101.1.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/431119/matrix-in-tikz-and-equation\",\"WARC-Payload-Digest\":\"sha1:IGTMP33JB2TKG2XR5DNY2NJUCJJMZTM5\",\"WARC-Block-Digest\":\"sha1:UAIG2LGY4Z6XQNDKGLVTTX3GSAZIRLGG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-39/CC-MAIN-2019-39_segments_1568514573439.64_warc_CC-MAIN-20190919040032-20190919062032-00401.warc.gz\"}"}