5

This question already has an answer here:

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.

marked as duplicate by user36296 beamer Mar 16 '18 at 14:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Welcome to TeX.Stackexchange! – user36296 Mar 16 '18 at 11:37
7

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

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}
  • should fragile be used always if the frame contains a tikzpicture? – Viesturs Dec 6 '18 at 23:21
  • 1
    @Viesturs Not necessarily, but is it necessary e.g. if # occurs somewhere. This might also be hidden in some tikz libraries, so even if your code does not contain #, fragile may still be necessary – user36296 Dec 6 '18 at 23:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.