# Math mode inside tikz failing - Updated

Edit: Based on @Clara's answer I've been able to get close to the solution I want. The new code is appended and I've added the output, now that I get output! But there are still issues -- see after the new code.

Original Question:

I've used inline mathmode ($,$) without any problems inside a tikzpicture environment in nodes, but the following code gives a 'missing $' error: \documentclass[11pt,reqno]{amsbook} \usepackage{amsmath} \usepackage{amssymb} \usepackage{tikz} \begin{document} \begin{tikzpicture} $X = \left[ \begin{matrix} X_1 \\ X_2 \\ \ldots \\ X_n \end{matrix} \right],$ \end{tikzpicture} \end{document}  The expression inside the tikz environment compiles fine outside it. What am I missing? In the interest of full disclosure I'm trying to put three expressions side by side. I figured if I put them in three stacked tikzpictures it would do the job, but then I ran into this problem. Perhaps there's a more elegant way to achieve my goal, and I'm open to that, but I'd also like to know (for the future) what's wrong with the above. BTW, based on a post here I tried putting the tikzpicture inside the $,$ but that didn't help. New code: \documentclass[11pt,reqno]{amsbook}% % \usepackage{amsmath} \usepackage{amssymb} \usepackage{tikz} \begin{document} $f_\mathbf{X}= \frac{1}{2\pi}|\Sigma|^{-1} e{-(A^\textup{T} \Sigma^{-1}A)},$ \vspace{0.5cm} where \\ \begin{tikzpicture} \draw(0,0) node{$\mathbf{X}  = \left[ \begin{matrix} X_1 \\
X_2 \\ \ldots \\ X_n \end{matrix} \right],$}; \end{tikzpicture} \begin{tikzpicture} \draw(0,0) node{$A=\left[\begin{matrix} x_1-\mu_1[![enter image description here][1]][1] \\
x_2-\mu_2 \\
\ldots \\ x_n-\mu_n \end{matrix} \right]$, and}; \end{tikzpicture} \begin{tikzpicture} \draw(0,-4) node{$
\Sigma = \left[ \begin{matrix} \sigma_{11} & \sigma_{12} & \ldots & \sigma_{1n} \\
\sigma_{21} & \sigma_{22} & \ldots & \sigma_{2n} \\
\sigma_{31} & \sigma_{32} & \ldots & \sigma_{3} \\
\hspace{2cm} \ldots \\
sigma_{n1} & \sigma_{n2} & \ldots & \sigma_{nn}
\end{matrix} \right].}; \end{tikzpicture} \end{document}  How do I align the images. I've tried using the vertical coord on the third picture to no avail. Also, while the third picture is most out of whack, the others aren't great either.[![enter image description here][2]][2] • Why do you need such matrix inside tikzpicture? It just seems unnecessary Sep 16 at 3:22 • @LuisTurcio. I believe I explained what I was trying to accomplish and why I thought the tikz environment would work. If you have a better solution that achieves my goal please share it. Sep 16 at 5:20 • The better solution for this is simple: Do not use tikzpicture Sep 16 at 6:51 • You can only put drawing commands at the top level inside a tikzpicture. If you want to put text (including maths) then you need to put it inside a node. Alignment between tikzpictures can be a bit tricky which is why it's best here not to use them at all. Alternatively, you could put them all inside the same tikzpicture whereupon alignment would be simpler. Sep 16 at 17:01 • @AndrewStacey. That helps my general understanding. Fortunately, Clara has provided a non-tikz solution. From that answer I also learned that items can be aligned within the eqn environment -- useful intel in addition to solving my immediate problem. A good day! Sep 16 at 18:03 ## 1 Answer \documentclass[11pt,reqno]{amsbook} \usepackage{amsmath} \usepackage{tikz} \usepackage{lipsum} \begin{document} \lipsum[1][1-3] \begin{tikzpicture} \node at (0,0) {X = \left[\begin{matrix} X_1 \\ X_2 \\ \ldots \\ X_n \end{matrix} \right]\$};
\end{tikzpicture}
\lipsum[1][1-3]
\end{document}


\documentclass{amsbook}
\usepackage{amsmath}
\begin{document}
\begin{equation*}
f_\mathbf{X}=\frac{1}{2\pi}|\Sigma|^{-1}e{-(A^\textup{T}\Sigma^{-1}A)},
\end{equation*}
where
\begin{equation*}
\mathbf{X} = \left[
\begin{matrix}
X_1    \\
X_2    \\
\vdots \\
X_n
\end{matrix}
\right],
A=\left[
\begin{matrix}
x_1-\mu_1 \\
x_2-\mu_2 \\
\vdots    \\
x_n-\mu_n
\end{matrix}
\right],
\text{ and }
\Sigma = \left[
\begin{matrix}
\sigma_{11} & \sigma_{12} & \cdots & \sigma_{1k} \\
\sigma_{21} & \sigma_{22} & \cdots & \sigma_{2k} \\
\sigma_{31} & \sigma_{32} & \cdots & \sigma_{3k} \\
\vdots      & \vdots      & \ddots & \vdots      \\
sigma_{n1}  & \sigma_{n2} & \cdots & \sigma_{nk}
\end{matrix}
\right].
\end{equation*}
\end{document}


• Thanks, that solves my immediate problem and I'm checking the box and up-ticking. But I confess I'm still confused about why my original code failed. If you can shed light on that it would be great. BTW, why does the vector intrude into the text below, is there a way to prevent that? Sep 16 at 5:14
• @TonyK I have add my answer. You don't have to use tikz. Sep 16 at 6:47
• Thank you Clara. For what it's worth, I tried several versions of align to solve this before trying tikz. I'm a user of Latex and all its components, not a practitioner. Sep 16 at 16:35
• Yikes, I just noticed the original post indicated I was taking the inverse of a non-square matrix!! Changed to avoid offend the math sensibilities of anyone reading the post in the future. Sep 16 at 16:56
• Also, thanks for improving the ellipses -- above and beyond. Looks better. Sep 16 at 17:04