1

I am getting two errors when seeking to use copy this matrix over to my beamer presentation slides:

Undefined control sequence.

Package pgf Error: Single ampersand used with wrong catcode. (This one occurs twice).

A reproducible version of the faulty code:

\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}

This is not rendering the matrix properly. It is not showing the even columns:

Incorrect Output

I first successfully created the diagram in a separate project which is coded as follows:

\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}

This correctly rendered the output:

Correct Output

Why the image is not rendering properly in my presentation slides, when I have simply copied the code over? Is there a way to get the desired outcome?

1 Answers1

0

You are missing the fragile frame option:

\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}


Alternatively you could replace all the & by something else, but that's certainly more tedious than using the fragile frame option:

\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}

enter image description here