我在文章类中做了一个交换图tikz
,一切正常。下一个代码是文章类中的 MWE。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{center}
\begin{tikzpicture}
\matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
{ A & B \\
C & D \\};
\path[-stealth]
(m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
(m-1-2) edge node [right] {g} (m-2-2)
(m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{document}
但是,当我将代码复制到Beamer文档中时,出现了错误,如下面代码所示。
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{frame}
\begin{center}
\begin{tikzpicture}
\matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
{ A & B \\
C & D \\};
\path[-stealth]
(m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
(m-1-2) edge node [right] {g} (m-2-2)
(m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
有什么想法吗?我做错什么了吗?
答案1
对于您的代码,您需要使用选项fragile
或frame
使用ampersand replacement
密钥:
\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{frame}
\begin{center}
\begin{tikzpicture}[ampersand replacement=\&]
\matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
{ A \& B \\
C \& D \\};
\path[-stealth]
(m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
(m-1-2) edge node [right] {g} (m-2-2)
(m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{frame}
\begin{frame}[fragile]
\begin{center}
\begin{tikzpicture}
\matrix(m)[matrix of math nodes,row sep=2em,column sep=4em,minimum width=2em]
{ A & B \\
C & D \\};
\path[-stealth]
(m-1-1) edge node [above] {e} (m-1-2) edge node [left] {f} (m-2-1)
(m-1-2) edge node [right] {g} (m-2-2)
(m-2-1) edge node [below] {h} (m-2-2);
\end{tikzpicture}
\end{center}
\end{frame}
\end{document}
另一种选择是使用tikz-cd
包,以便于编写交换图;在这种情况下,您需要使用fragile
选项frame
:
\documentclass{beamer}
\usepackage{tikz-cd}
\usetikzlibrary{matrix}
\begin{document}
\begin{frame}[fragile]
\begin{center}
\begin{tikzcd}
A\arrow{r}{e}\arrow{d}[swap]{f} & B\arrow{d}{g} \\
C\arrow{r}[swap]{h} & D \\
\end{tikzcd}
\end{center}
\end{frame}
\end{document}