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.

1

2 Answers 2

53

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}
1
  • 2
    I had a hash in a URL (\href), and this method didn't work. However, I screened that with a backslash, and \# worked fine. Commented May 30, 2022 at 9:05
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}
2
  • 1
    should fragile be used always if the frame contains a tikzpicture?
    – Viesturs
    Commented Dec 6, 2018 at 23:21
  • 6
    @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 Commented Dec 6, 2018 at 23:30

Not the answer you're looking for? Browse other questions tagged .