# TikZ matrix fails inside makebox or framebox

I want to draw a framebox around a TikZ picture. But it won't compile. I try makebox instead of framebox, but the problem persists.

Here is a MWE. pdflatex will raise Undefined control sequence \pgf@matrix@last@nextcell@options.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\makebox{%
\begin{tikzpicture}
\matrix (m) [matrix of nodes] {
text & text \\
};
\end{tikzpicture}%
}

\end{document}


Thanks for the answers. They look as good but I can only accept one answer.

Problems with similar nature can be found here

The later explains why & causes the trouble.

Here you have three different solutions.

The first one uses background library to draw the background rectangle associated to any tikzpicture.

The second draw the matrix node without drawing inner matrix nodes. It's possible to use this solution because you draw a matrix. With any other figure you should use previous solution.

And the third uses a tcbox (from tcolorbox). It's similar to fbox but with more configuration options. In this case you have to use ampersand replacement option into the matrix text.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, backgrounds}
\usepackage{tcolorbox}
\begin{document}

\begin{tikzpicture}[show background rectangle]
\matrix (m) [matrix of nodes] {
text & text \\
};
\end{tikzpicture}%

\begin{tikzpicture}%[show background rectangle]
\matrix (m) [draw, matrix of nodes] {
text & text \\
};
\end{tikzpicture}%

\tcbox[sharp corners, colback=white, size=fbox]{\begin{tikzpicture}
\matrix (m) [matrix of nodes, ampersand replacement=\&] {
text \& text \\
};
\end{tikzpicture}}

\end{document}


I found option ampersand replacement=\& (as follows) will solve the problem.

\matrix (m) [matrix of nodes,ampersand replacement=\&] {
text \& text \\
};


Though I would still appreciate if someone can explain why & causes troubles insides \makebox.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,fit}

\begin{document}
%   \makebox{%
\begin{tikzpicture}
\matrix (m) [matrix of nodes] {
text & text \\
};
\node[draw,fit=(m-1-1) (m-1-2),inner sep=0pt]{};
\end{tikzpicture}%
%   }
\end{document}


If you don't want to use ampersand replacement, you have also the following workaround: compose in a box and, then put the box as argument of your command.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\setbox1=\hbox{%
\begin{tikzpicture}
\matrix (m) [matrix of nodes] {
text & text \\
};
\end{tikzpicture}%
}

\fbox{\box1}

\end{document}


From TikZ & PGF manual (v 3.1.8b, page 321) is noted:

1. If you place a matrix on a path, the matrix contents will be collected into a macro, which tokenizes them. This means that & will lose its meaning as an alignment character, resulting in an error. If you need to place a matrix on a path, use ampersand replacement to work around that problem.

So, inserting tikzpicture in frame is such a case.

It is not clear, what is intention of your frame around tikzpicture. If you like to have frame only around matrix, than you can simply write:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,
draw] % <---
{
text & text \\
};
\end{tikzpicture}%
\end{document}


In the case, that frame is around more complex image content, than you can use fit library and solve your problem on the following way:

\documentclass[border=3.141592mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit,
matrix,
positioning}

\begin{document}
\begin{tikzpicture}
\matrix (m1) [matrix of nodes,
draw]
{
first & matrix \\
};
\matrix (m2) [matrix of nodes,
draw,
right=of m1]
{
second      & matrix \\
left cell   & right cell \\
};
\draw[->]   (m1) -- (m2);
\node[draw=red, fit=(current bounding box)]{}; % <---
\end{tikzpicture}%
\end{document}