# tikz matrix of nodes in tabularx

I try to put two tikzpictures containing each a matrix of nodes next to each other in a tabularx spanning the whole textwidth. Here is what I tried

\documentclass[11pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tabularx}{\textwidth}{XX}
\begin{tikzpicture}
\matrix [matrix of nodes]
{
8 & 1 & 6 \\
3 & 5 & 7 \\
4 & 9 & 2 \\
};
\end{tikzpicture} & \begin{tikzpicture}
\matrix [matrix of nodes]
{
8 & 1 & 6 \\
3 & 5 & 7 \\
4 & 9 & 2 \\
};
\end{tikzpicture}
\end{tabularx}
\end{document}


However, with this code I get the following error

! Undefined control sequence. \pgf@matrix@last@nextcell@options

l.24 \end{tabularx}

The control sequence at the end of the top line of your error message was never \def'ed. If you have misspelled it (e.g., \hobx'), typeI' and the correct spelling (e.g., I\hbox'). Otherwise just continue, and I'll forget about whatever was undefined.

What am I doing wrong?

Many thanks.

Welcome to TeX.SE! There are too many & around, meaning that if the compiler sees a & it does not now whether this is to separate table or matrix cells. So you need to distinguish between these two options, which can be done by using ampersand replacement for the & of the matrices.

\documentclass[11pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tabularx}{\textwidth}{XX}
\begin{tikzpicture}
\matrix [matrix of nodes,ampersand replacement=\&]
{
8 \& 1 \& 6 \\
3 \& 5 \& 7 \\
4 \& 9 \& 2 \\
};
\end{tikzpicture} & \begin{tikzpicture}
\matrix [matrix of nodes,ampersand replacement=\&]
{
8 \& 1 \& 6 \\
3 \& 5 \& 7 \\
4 \& 9 \& 2 \\
};
\end{tikzpicture}
\end{tabularx}
\end{document}
`