31

I'm using beamer class and I want to add a figure to my slides, however I keep getting this error :

    ! Illegal parameter number in definition of \iterate.
     <to be read again> 
               1
      l.229 \end{frame}

         ?

This is my code :

\begin{frame} 
\begin{figure}
 \centering
 \begin{tikzpicture}[
  node distance = 0pt,
  shorten <>/.style = {shorten <=#1, shorten >=#1},
  start chain = going right,
   box/.style = {shape=rectangle, draw, fill=#1,
            minimum width=6mm, minimum height=9mm, outer sep=0pt,
            node contents={},
            on chain},
   box/.default = none,
    arrow/.style = {draw=blue!60!black, thick, shorten <>=1mm, 
            out=90, in=90, looseness=3,
            -{Straight Barb[bend]}},
     arbox/.style = {inner sep=0pt, minimum size=5pt},
  %crbox/.style = {inner sep=0pt,
          %  node contents={\scriptsize\color{red}$\boldsymbol{\times}$}
           % },
    label distance = -3pt,
     sx/.style = {xshift=#1pt}
                    ]
   \node (n0) [box,dashed];
      \foreach \i in {1,2,3}
          \ifnum\i<1
              \node (n\i) [box]
                  \else
              \node (n\i) [box=gray!25]
            \fi;
           \node (n14) [box,dashed];

\draw[ultra thick,dotted,shorten <=1mm]  (n0)  -- + (-9mm,0mm);
\draw[ultra thick,dotted,shorten <=1mm]  (n14) -- + (+9mm,0mm);
   %\fill[black!75] 

   \end{tikzpicture}

      \end{figure}
       \end{frame}

I don't know exactly why figures inside a frame make a problem ? I would appreciate it if anyone could help me with that.

Math 2018
  • 311

2 Answers2

51

The frame environment is implemented by assigning its content to a macro multiple times. During this process, the # is interpreted. So to use # in a frame, you have to replace every # by ####:

\begin{frame} 
\begin{figure}
 \centering
 \begin{tikzpicture}[
  node distance = 0pt,
  shorten <>/.style = {shorten <=####1, shorten >=####1},
  start chain = going right,
   box/.style = {shape=rectangle, draw, fill=####1,
            minimum width=6mm, minimum height=9mm, outer sep=0pt,
            node contents={},
            on chain},
   box/.default = none,
    arrow/.style = {draw=blue!60!black, thick, shorten <>=1mm, 
            out=90, in=90, looseness=3,
            -{Straight Barb[bend]}},
     arbox/.style = {inner sep=0pt, minimum size=5pt},
  %crbox/.style = {inner sep=0pt,
          %  node contents={\scriptsize\color{red}$\boldsymbol{\times}$}
           % },
    label distance = -3pt,
     sx/.style = {xshift=####1pt}
  ]
    \node (n0) [box,dashed];
    \foreach \i in {1,2,3}
      \ifnum\i<1
        \node (n\i) [box]
      \else
        \node (n\i) [box=gray!25]
      \fi;
    \node (n14) [box,dashed];

    \draw[ultra thick,dotted,shorten <=1mm]  (n0)  -- + (-9mm,0mm);
    \draw[ultra thick,dotted,shorten <=1mm]  (n14) -- + (+9mm,0mm); 
  \end{tikzpicture}
\end{figure}
\end{frame}
29

If you have fragile content in your frame, such as your tikz picture, use the fragile frame option.

You also don't need \centering as figures are centred by default in beamer.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{chains}

\begin{document}

\begin{frame}[fragile] 
  \begin{figure}
    %\centering
    \begin{tikzpicture}[
      node distance = 0pt,
      shorten <>/.style = {shorten <=#1, shorten >=#1},
      start chain = going right,
      box/.style = {shape=rectangle, draw, fill=#1,
      minimum width=6mm, minimum height=9mm, outer sep=0pt,
      node contents={},
      on chain},
      box/.default = none,
      arrow/.style = {draw=blue!60!black, thick, shorten <>=1mm, 
      out=90, in=90, looseness=3,
      -{Straight Barb[bend]}},
      arbox/.style = {inner sep=0pt, minimum size=5pt},
      %crbox/.style = {inner sep=0pt,
      %  node contents={\scriptsize\color{red}$\boldsymbol{\times}$}
      % },
      label distance = -3pt,
      sx/.style = {xshift=#1pt}
    ]
    \node (n0) [box,dashed];
    \foreach \i in {1,2,3}
    \ifnum\i<1
      \node (n\i) [box]
    \else
      \node (n\i) [box=gray!25]
    \fi;
    \node (n14) [box,dashed];
    \draw[ultra thick,dotted,shorten <=1mm]  (n0)  -- + (-9mm,0mm);
    \draw[ultra thick,dotted,shorten <=1mm]  (n14) -- + (+9mm,0mm);
   \end{tikzpicture}
 \end{figure}
\end{frame}

\end{document}