当我尝试将此矩阵复制到我的 Beamer 演示幻灯片时出现两个错误:
未定义控制序列。
程序包 pgf 错误:单个“&”符号与错误的 catcode 一起使用。(此错误出现了两次)。
错误代码的可重现版本:
\documentclass{beamer}
\graphicspath{{Images/}{./}}
\usepackage{amssymb, amsmath,tikz-cd}
\usepackage{booktabs}
\usetheme{Madrid}
\usefonttheme{default}
\usepackage{palatino}
\usepackage[default]{opensans}
\usepackage{amssymb, amsmath, tikz-cd}
\useinnertheme{circles}
\begin{document}
\begin{frame}{Needleman-Wunsch Algorithm}
$\begin{pmatrix}
\begin{tikzcd}[cramped, row sep = 8, column sep = 4]
0\arrow[r]\arrow[dr]\arrow[d] & 2\arrow[r] & 3\arrow[r] & 4\arrow[r]\arrow[dr] & 9 \\
5\arrow[d] & 1\arrow[r]\arrow[dr]\arrow[d] & 2\arrow[r]\arrow[dr]\arrow[d] & 3\arrow[dr]\arrow[d] & 7 \\
8\arrow[d] & 4\arrow[r]\arrow[dr]\arrow[d] & 5\arrow[r]\arrow[dr] & 6 & 8\arrow[d] \\
15\arrow[d] & 11\arrow[d] & 7\arrow[d] & 6\arrow[dr]\arrow[d] & 10 \\
18 & 14 & 10 & 9 & 11 \\
\end{tikzcd}
\end{pmatrix}$
\end{frame}
\end{document}
这无法正确渲染矩阵。它无法显示偶数列:
我首先在一个单独的项目中成功创建了该图表,其代码如下:
\documentclass{beamer}
\usepackage{amssymb, amsmath,tikz-cd}
\begin{document}
$\begin{pmatrix}
\begin{tikzcd}[cramped, row sep = 8, column sep = 4]
0\arrow[r]\arrow[dr]\arrow[d] & 2\arrow[r] & 3\arrow[r] & 4\arrow[r]\arrow[dr] & 9 \\
5\arrow[d] & 1\arrow[r]\arrow[dr]\arrow[d] & 2\arrow[r]\arrow[dr]\arrow[d] & 3\arrow[dr]\arrow[d] & 7 \\
8\arrow[d] & 4\arrow[r]\arrow[dr]\arrow[d] & 5\arrow[r]\arrow[dr] & 6 & 8\arrow[d] \\
15\arrow[d] & 11\arrow[d] & 7\arrow[d] & 6\arrow[dr]\arrow[d] & 10 \\
18 & 14 & 10 & 9 & 11 \\
\end{tikzcd}
\end{pmatrix}$
\end{document}
这正确地呈现了输出:
为什么我复制了代码后,幻灯片中的图像却无法正确显示?有没有办法获得所需的结果?
答案1
您缺少fragile
框架选项:
\documentclass{beamer}
\graphicspath{{Images/}{./}}
\usepackage{amssymb, amsmath,tikz-cd}
\usepackage{booktabs}
\usetheme{Madrid}
\usefonttheme{default}
\usepackage{palatino}
\usepackage[default]{opensans}
\usepackage{amssymb, amsmath, tikz-cd}
\useinnertheme{circles}
\begin{document}
\begin{frame}[fragile]{Needleman-Wunsch Algorithm}
$\begin{pmatrix}
\begin{tikzcd}[cramped, row sep = 8, column sep = 4]
0\arrow[r]\arrow[dr]\arrow[d] & 2\arrow[r] & 3\arrow[r] & 4\arrow[r]\arrow[dr] & 9 \\
5\arrow[d] & 1\arrow[r]\arrow[dr]\arrow[d] & 2\arrow[r]\arrow[dr]\arrow[d] & 3\arrow[dr]\arrow[d] & 7 \\
8\arrow[d] & 4\arrow[r]\arrow[dr]\arrow[d] & 5\arrow[r]\arrow[dr] & 6 & 8\arrow[d] \\
15\arrow[d] & 11\arrow[d] & 7\arrow[d] & 6\arrow[dr]\arrow[d] & 10 \\
18 & 14 & 10 & 9 & 11 \\
\end{tikzcd}
\end{pmatrix}$
\end{frame}
\end{document}
或者,您可以用其他东西替换所有内容&
,但这肯定比使用fragile
框架选项更繁琐:
\documentclass{beamer}
\graphicspath{{Images/}{./}}
\usepackage{amssymb, amsmath,tikz-cd}
\usepackage{booktabs}
\usetheme{Madrid}
\usefonttheme{default}
\usepackage{palatino}
\usepackage[default]{opensans}
\usepackage{amssymb, amsmath, tikz-cd}
\useinnertheme{circles}
\begin{document}
\begin{frame}{Needleman-Wunsch Algorithm}
$\begin{pmatrix}
\begin{tikzcd}[cramped, row sep = 8, column sep = 4,ampersand replacement=\&]
0\arrow[r]\arrow[dr]\arrow[d] \& 2\arrow[r] \& 3\arrow[r] \& 4\arrow[r]\arrow[dr] \& 9 \\
5\arrow[d] \& 1\arrow[r]\arrow[dr]\arrow[d] \& 2\arrow[r]\arrow[dr]\arrow[d] \& 3\arrow[dr]\arrow[d] \& 7 \\
8\arrow[d] \& 4\arrow[r]\arrow[dr]\arrow[d] \& 5\arrow[r]\arrow[dr] \& 6 \& 8\arrow[d] \\
15\arrow[d] \& 11\arrow[d] \& 7\arrow[d] \& 6\arrow[dr]\arrow[d] \& 10 \\
18 \& 14 \& 10 \& 9 \& 11 \\
\end{tikzcd}
\end{pmatrix}$
\end{frame}
\end{document}