正如所指出的将 tikz 图形缩放到 \textwidth 的百分比我期望这会起作用:
\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}
然而,它给了我:
! Undefined control sequence.
<argument> \pgf@matrix@last@nextcell@options
l.24 }
?
我究竟做错了什么?
答案1
该问题是由&
矩阵内部引起的,tikz 以非标准方式处理矩阵内部,并在其他命令中用作参数时导致问题。
解决方案是将每个替换&
为\&
并放入ampersand replacement=\&
矩阵选项,如以下代码所示:
\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}