LaTeX 文档中的 Tikzpicture 无法在 Beamer 中编译

LaTeX 文档中的 Tikzpicture 无法在 Beamer 中编译

可能重复:
使用 Beamer 中的 tikz 节点矩阵时出现未知错误

我的 TeX 文档中有以下tikzpicture环境,但我无法使其在 Beamer 中工作。我收到错误:

! Package pgf Error: No shape named m-1-3 is known. 
! Package pgf Error: No shape named m-1-4 is known.

以下是代码

\begin{frame}[fragile] 
\frametitle{Data set-up}
\begin{tikzpicture}[description/.style={fill=white,inner sep=2pt}]
\matrix (m) [matrix of math nodes, row sep=3em,
column sep=2.5em, text height=1.5ex, text depth=0.25ex] 
{\&\&Z_{1}\&Z_{2}\&\& \\
R \& \& \& \& \& Y  \\ };
\path[->,font=\scriptsize]
(m-1-3) edge node[description]{$ \alpha_{1} $} (m-2-1)
(m-1-4) edge node[description] {$ \alpha_{2} $}(m-2-1)
(m-1-3) edge node[description]{$ \beta_{1} $} (m-2-6)
(m-1-4) edge node[description] {$ \beta_{2} $}(m-2-6);
\path[->,loosely dashed, font=\scriptsize]
(m-2-1) edge node[auto, swap] {$(MAR)$}(m-2-6);
\end{tikzpicture}
\end{frame}

答案1

问题是由于您的代码使用\&作为列分隔符,但默认值为&。要使用,\&您应该添加选项ampersand replacement=\&

此代码可以正常工作:

\documentclass{beamer}
\usepackage{tikz,amsmath}
\usetikzlibrary{matrix}

\begin{document}

\begin{frame}[fragile] 
\frametitle{Data set-up}
\begin{tikzpicture}[description/.style={fill=white,inner sep=2pt}]
\matrix (m) [matrix of math nodes, row sep=3em,
 column sep=2.5em, text height=1.5ex, text depth=0.25ex,
 ampersand replacement=\&] % <= notice the missing option
{\&\&Z_{1}\&Z_{2}\&\& \\
R \& \& \& \& \& Y  \\ };
\path[->,font=\scriptsize]
(m-1-3) edge node[description]{$ \alpha_{1} $} (m-2-1)
(m-1-4) edge node[description] {$ \alpha_{2} $}(m-2-1)
(m-1-3) edge node[description]{$ \beta_{1} $} (m-2-6)
(m-1-4) edge node[description] {$ \beta_{2} $}(m-2-6);
\path[->,loosely dashed, font=\scriptsize]
(m-2-1) edge node[auto, swap] {$(MAR)$}(m-2-6);
\end{tikzpicture}
\end{frame}    

\end{document}

图形结果:

在此处输入图片描述

相关内容