[Tex/LaTex] Why does \resizebox around tikzpicture gives me undefined control sequence }

scalingtikz-pgf

As indicated in Scale tikz figure to a percentage of \textwidth I was expecting this to work:

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}
\begin{figure}
\resizebox{0.5\textwidth}{!}{%
\begin{tikzpicture}

\matrix(Person) [matrix of nodes, 
                 matrix/.style={rows={1}{fill=gray}},
                 label=above:Person, 
                 column 1/.style={nodes={text width=1cm, align=center}},
                 column 2/.style={nodes={text width=2cm}},
                 column 3/.style={nodes={text width=2.5cm}},
                 column 4/.style={nodes={text width=2cm}},
                 column 5/.style={nodes={text width=2cm, align=center}}
                  ] {
    id & name       & address       & birth date & occupation.id \\
    1  & Mary Smith & Main street 1 & 1970-04-17 & 1 \\
    2  & John Doe   & Highway 2     & 1972-07-24 & 1 \\ 
    3  & Clara Doe  & Highway 2     & 1995-11-11 & 2 \\};
\end{tikzpicture}
}
\end{figure}

\end{document}

However, it gives me:

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

l.24 }

?

What am I doing wrong?

Best Answer

The problem is caused by the & inside the matrix, which are handled in a non-standard way by tikz and cause problems when used as argument in other commands.

The solution is to replace each & by \& and to put ampersand replacement=\& in matrix options, as in the following code:

\documentclass{memoir}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning}

\begin{document}
\begin{figure}
\resizebox{0.5\textwidth}{!}{%
\begin{tikzpicture}
    \matrix(Person) [matrix of nodes, ampersand replacement=\&,
                 matrix/.style={rows={1}{fill=gray}},
                 label=above:Person, 
                 column 1/.style={nodes={text width=1cm, align=center}},
                 column 2/.style={nodes={text width=2cm}},
                 column 3/.style={nodes={text width=2.5cm}},
                 column 4/.style={nodes={text width=2cm}},
                 column 5/.style={nodes={text width=2cm, align=center}}
                  ] {
    id \& name       \& address       \& birth date \& occupation.id \\
    1  \& Mary Smith \& Main street 1 \& 1970-04-17 \& 1 \\
    2  \& John Doe   \& Highway 2     \& 1972-07-24 \& 1 \\ 
    3  \& Clara Doe  \& Highway 2     \& 1995-11-11 \& 2 \\};
\end{tikzpicture}
}
\end{figure}

\end{document}

enter image description here