# When using tikz math don't stay below in environment

I was having a similar problem as shown here, but when I put the matrix inside a tikz block, the Example environment don't skip a line and put everything in the same line. As in the similar problem, I'd like to put the matrix one line below and centered.

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{tikz}
\newtheorem{xmp}{Example}

\begin{document}
\begin{xmp}
$\mathbf{A}_{4\times 4}= \begin{tikzpicture}$$\begin{bmatrix*}[r] -9 & -1 & 0 & -3 \\ -5 & -8 & -9 & 4 \\ 7 & 3 & -1 & -7 \\ -8 & 1 & 2 & 7 \end{bmatrix*}$$ \end{tikzpicture}$
\end{xmp}

\end{document}


I'm putting the matrix inside the tikz block because if I use the matrix library I cannot right align the numbers using the {bmatrix*}[r] environment provided by the mathtools package.

• That's a rather odd thing to do. If you remove \begin{tikzpicture} $$ and $$ \end{tikzpicture}, do you get what you need? – Torbjørn T. Jul 28 '18 at 17:06
• It's inside a tikz block because I'll do some highlights and paths inside the matrix. – Levy Jul 28 '18 at 17:09
• I see. You're not supposed to place text directly inside a tikzpicture though, place the bmatrix inside a node. I'll add an answer, – Torbjørn T. Jul 28 '18 at 17:13

Don't place the bmatrix directly in the tikzpicture, place it inside a node. You can right align the nodes in a matrix of math nodes by the way, set nodes={anchor=base east}.

Note the baseline key added to the tikzpicture environment, used to improve its vertical alignment.

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newtheorem{xmp}{Example}

\begin{document}
\begin{xmp}
$\mathbf{A}_{4\times 4}= \begin{bmatrix*}[r] -9 & -1 & 0 & -3 \\ -5 & -8 & -9 & 4 \\ 7 & 3 & -1 & -7 \\ -8 & 1 & 2 & 7 \end{bmatrix*}$
\end{xmp}

\begin{xmp}
$\mathbf{A}_{4\times 4}= \begin{tikzpicture}[baseline] \node [inner sep=0pt, name=m] { $$\begin{bmatrix*}[r] -9 & -1 & 0 & -3 \\ -5 & -8 & -9 & 4 \\ 7 & 3 & -1 & -7 \\ -8 & 1 & 2 & 7 \end{bmatrix*}$$}; \end{tikzpicture}$
\end{xmp}
\begin{xmp}
$\mathbf{A}_{4\times 4}= \begin{tikzpicture}[baseline] \matrix [name=m, inner xsep=0, inner ysep=2pt, matrix of math nodes, right delimiter={]}, left delimiter={[}, column sep=8pt, row sep=5pt, nodes={anchor=base east, inner sep=0pt}]{ -9 & -1 & 0 & -3 \\ -5 & -8 & -9 & 4 \\ 7 & 3 & -1 & -7 \\ -8 & 1 & 2 & 7 \\ }; \end{tikzpicture}$
\end{xmp}
\end{document}

• Great! This really worked. Thank you very much. – Levy Jul 28 '18 at 17:58

i do not understand what is benefits if you put matrix inside nodes. if you like to have it as image, where each matrix element has a name, that you need to write matrix as tikz picture:

\documentclass[10pt,a4paper]{article}
%\usepackage[latin1]{inputenc} % default in article is utf8
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, matrix}
\newtheorem{xmp}{Example}

\usepackage{url}

\begin{document}
\begin{xmp}
\mathbf{A}_{4\times 4}= \begin{tikzpicture}[baseline] \matrix (m) [matrix of math nodes,% nodes={inner sep=1pt, text width=3ex, align=right}, left delimiter={[},right delimiter={]}, column sep=1ex, row sep=1ex, ] { -9 & -1 & 0 & -3 \\ -5 & -8 & -9 & 4 \\ 7 & 3 & -1 & -7 \\ -8 & 1 & 2 & 7 \\ }; \end{tikzpicture}
\end{xmp}
\begin{xmp}
\mathbf{A}_{4\times 4}= \begin{tikzpicture}[baseline] \matrix (m) [matrix of math nodes,% nodes={inner sep=1pt, text width=3ex, align=right}, left delimiter={[},right delimiter={]}, column sep=1ex, row sep=1ex, ] { -9 & -1 & 0 & -3 \\ -5 & -8 & -9 & 4 \\ 7 & 3 & -1 & -7 \\ -8 & 1 & 2 & 7 \\ }; \scoped[on background layer] \node[fill=red!30,inner sep=1pt, fit=(m-2-2) (m-4-4)] {}; \end{tikzpicture}
\end{xmp}
an example of the representation of determinants calculation (taken from \url{http://tex.stackexchange.com/questions/257043/})
$\mathbf{A}_{3\times 3}= \begin{tikzpicture}[baseline, strip/.style = { draw=#1,%color line width=1em, opacity=0.2, line cap=round,% only if you like them ... shorten <=-1mm, shorten >=-1mm, }, ] \matrix (m) [matrix of math nodes, column sep=1em, nodes={text height=1ex,text width=2ex} ] { |[red]|+ & |[red]|+ & \color{red}+\color{blue}- & |[blue]|- & |[blue]|- \\[3.3mm,between origins] a_1 & b_1 & c_1 & a_1 & a_2 \\ a_2 & b_2 & c_2 & a_2 & b_2 \\ a_3 & b_3 & c_3 & a_3 & b_3 \\ }; \draw[thick] (m-2-1.north) -| (m-4-1.south west) -- (m-4-1.south); \draw[thick] (m-2-3.north) -| (m-4-3.south east) -- (m-4-3.south); \draw[strip=blue]% (m-4-1.center) edge (m-2-3.center) (m-4-2.center) edge (m-2-4.center) (m-4-3.center) -- (m-2-5.center); \draw[strip=red] (m-2-1.center) edge (m-4-3.center) (m-2-2.center) edge (m-4-4.center) (m-2-3.center) -- (m-4-5.center); \end{tikzpicture}$
$\det A = (\textcolor{red} {a_1b_2c_3 + b_1c_2a_3 + c_1a_2b_3}) - (\textcolor{blue}{a_3b_2c_1 + b_3c_2a_1 + c_3a_2b_1})$
\end{document}