1

I'd like to create a gif using tikz. However, using the pgfonlayer to create multiple pages results in the nodes being shifted whenever an edge is added between them. This makes the gif very shaky.

Is there a way to "lock down" the 9 nodes, so that arrows between these nodes appear smoothly in a gif?

This is a working example of 2 pages being created, but they are not correctly aligned.

\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{lmodern}

\usepackage{verbatim} \usepackage{tikz} \usetikzlibrary{arrows,shapes}

\tikzstyle{box}=[rectangle, draw, thick, minimum size=6mm] \tikzstyle{up_arrow}=[->, shorten >= 1pt, >=stealth',semithick,bend left] \tikzstyle{down_arrow}=[->, shorten >= 1pt, >=stealth',semithick,bend right]

\begin{document} \pgfdeclarelayer{background} \pgfsetlayers{background,main}

\begin{frame} \begin{figure} \begin{tikzpicture}[bend angle=45][overlay] \node[box] [label=below:{0}] (a0) {a}; \node[box] [label=below:{1}] (a1) [right of=a0] {b}; \node[box] [label=below:{2}] (a2) [right of=a1] {c}; \node[box] [label=below:{3}] (a3) [right of=a2] {a}; \node[box] [label=below:{4}] (a4) [right of=a3] {d}; \node[box] [label=below:{5}] (a5) [right of=a4] {d}; \node[box] [label=below:{6}] (a6) [right of=a5] {c}; \node[box] [label=below:{7}] (a7) [right of=a6] {a}; \node[box] [label=below:{8}] (a8) [right of=a7] {c}; \node[box] [label=below:{9}] (a9) [right of=a8] {d};

        \begin{pgfonlayer}{background}
            \draw<1-> [down_arrow] [draw=purple] (a6) to (a8);
            \draw<2-> [up_arrow] [draw=blue] (a5) to (a9);
        \end{pgfonlayer}
    \end{tikzpicture}    
\end{figure}

\end{frame}

\end{document}

enter image description here

  • Welcome to TeX.SX! You can set the bounding box of the tikzpicture. Try adding something like \useasboundingbox (0,1.25); right before \end{tikzpicture}. This increases the upper bound of the bouding box and should prevent shifting. – Jasper Habicht Aug 16 '22 at 08:37
  • @JasperHabicht Thanks, that almost solves my issue. However, I'm still getting shaking gif when trying to connect nodes from the bottom instead of top (The code in the post was a trimmed down version, I have around 9 edges). Could you please point me to the documentation of useasboundingbox so that I can explore other fine tuning options? – adaptatron Aug 16 '22 at 08:56
  • \useasboundingbox just takes any path. You can use a rectangle that is large enough to fit all elements of your picture. – Jasper Habicht Aug 16 '22 at 09:02

1 Answers1

3

You can define a bounding box that is as large as the picture with both arrows adding something like to your code:

\useasboundingbox (-1,-2) rectangle (10,2);

You can also make use of the overlay-beamer-styles that allows you to make certain paths of the picture invisible or visble on different slides.

\documentclass[hyperref={pdfpagelabels=false}]{beamer}
\usepackage{tikz}
\usetikzlibrary{overlay-beamer-styles}

\tikzstyle{box}=[rectangle, draw, thick, minimum size=6mm] \tikzstyle{up_arrow}=[->, shorten >= 1pt, >=stealth, semithick, bend left] \tikzstyle{down_arrow}=[->, shorten >= 1pt, >=stealth, semithick, bend right]

\begin{document} \pgfdeclarelayer{background} \pgfsetlayers{background,main}

\begin{frame} \begin{figure} \begin{tikzpicture}[bend angle=45] \node[box] [label=below:{0}] (a0) {a}; \node[box] [label=below:{1}] (a1) [right of=a0] {b}; \node[box] [label=below:{2}] (a2) [right of=a1] {c}; \node[box] [label=below:{3}] (a3) [right of=a2] {a}; \node[box] [label=below:{4}] (a4) [right of=a3] {d}; \node[box] [label=below:{5}] (a5) [right of=a4] {d}; \node[box] [label=below:{6}] (a6) [right of=a5] {c}; \node[box] [label=below:{7}] (a7) [right of=a6] {a}; \node[box] [label=below:{8}] (a8) [right of=a7] {c}; \node[box] [label=below:{9}] (a9) [right of=a8] {d};

        \begin{pgfonlayer}{background}
            \draw[visible on=<1->, down_arrow] [draw=purple] (a6) to (a8);
            \draw[visible on=<2->, up_arrow] [draw=blue] (a5) to (a9);
        \end{pgfonlayer}
    \end{tikzpicture}    
\end{figure}

\end{frame}

\end{document}

enter image description here

Note that the arrows library is deprecated and you should probably use the newer arrows.meta library.