TikZ matrix fails inside makebox or framebox
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}