# tikz animation on beamer slide - centering and improvements

The code below animates 4 simple, nearly identical, processes, maybe familiar to many of you. Each time it adds the completed picture to the list at the top.

My main issue is centering - I am having trouble keeping the ion ($n^+i$ node) centered - both horizontally and vertically. It should not move at all for the entire animation - now it "jumps" around. I managed to fake the horizontal centering with offsetting the entire picture - vertically there is still a minor jump. That's why I have to trimbox when adding these figures to the upper list. I am pretty sure this is not the best solution. This: How to center horizontally tikzpicture in beamer frame using a specific node? made me hopeful, but did not prevent the "wiggling" during animation.

Other nitpicks - Can I loop on only part of the figure list? The way I am doing it seems to copy-pasta. I tried having a figure list and first figure macros, but using \firstfig,\figlist in the foreach line breaks.

This is my first such project - I am both not fluent in latex and in the pgf language - and so any suggestions are welcome, style-wise or method-wise included.

\documentclass[hideothersubsections]{beamer}

\usepackage{tikz}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{calc}
\usepackage{fp}

\tikzset{%
pics/globCoord/.style={%
code={%
\coordinate(offset) at (3.5cm, -1cm);%
\coordinate(dir)    at (1em, -1em);%
\coordinate(rdir)   at (1em, 1em);%
\path (dir) +(rdir) coordinate(turn);%
}%
},%
pics/pathCoord/.style n args={2}{%
code={%
\ifnum#2=0%
\coordinate(start) at (offset);%
\coordinate(end)   at ($(start) + #1*(dir)$);%
\coordinate(direction) (dir);%
\else%
\coordinate(start) at ($(offset) + #1*(dir) + (turn)$);%
\coordinate(end)   at ($(start) + #2*(rdir)$);%
\coordinate(direction) (rdir);%
\fi;%
}%
},%
pics/elec/.style n args={2}{%
code={%
\pic{pathCoord={#1}{#2}};
\draw[thick, decorate, draw=black, decoration={aspect=0},->] (start) -- (end);%
\draw[thick] ($(end)+ 0.5*(direction)$) circle(0.7071em) node {\small$e^-$};%
}%
},%
pics/ph/.style n args={2}{%
code={%
\pic{pathCoord={#1}{#2}};%
\draw[thick, decorate, draw=black, decoration={coil,aspect=0},->] (start) -- (end);%
}%
},%
pics/ion/.style n args={2}{%
code={%
\draw[thick] ($(offset) + (dir) + #1*(dir)$) circle(1.414em) node {#2};%
}%
}%
}
\newcommand{\phion}[6]{%
\begin{tikzpicture}%
\pic{globCoord};%
\ifnum#1>#2%
\pic{ion={#2}{$n^{+#6}$}};%
\pic{#3={#2}{0}};%
\FPeval{\d}{clip(#1-#2)}%
\pic{#4={#2}{\d}};%
\else%
\pic{ion={#2}{$n^{+#5}$}};%
\pic{#3={#1}{0}};%
\fi;%
\end{tikzpicture}%
}
\newcommand{\moves}{4}
\FPeval{\DM}{clip(2*\moves)}
\newcommand{\pics}{2}
\def\raisealign{5pt}
\newcommand{\figlist}{ph/elec/i/i-1,elec/ph/i/i+1,ph/elec/i+1/i,elec/ph/i-1/i}
\newcommand{\pfiglist}{elec/ph/i/i+1,ph/elec/i+1/i,elec/ph/i-1/i}
\begin{document}
\foreach \first/\last/\from/\to [count=\pic from 0] in \figlist {
\begin{frame}[t]{The ionized population}
\raisebox{\raisealign}{Processes: }
\ifnum\pic=0
\else
\raisebox{\raisealign}{-}\scalebox{0.2}{\trimbox{3cm 0cm 1cm 0cm}{\phion{\DM}{\moves}{ph}{elec}{i}{i-1}}}
\ifnum \pic>1
\foreach  \first/\last/\from/\to [count=\current from 2] in \pfiglist {
\if\to i \raisebox{\raisealign}{+} \else \raisebox{\raisealign}{-}\fi
\scalebox{0.2}{\trimbox{3cm 0cm 1cm 0cm}{\phion{\DM}{\moves}{\first}{\last}{\from}{\to}}}
\ifnum\current=\pic \breakforeach \else\fi
}
\else\fi
\fi
\\
\foreach \td in {0,...,\DM} {
\only<\td>{
\phion{\td}{\moves}{\first}{\last}{\from}{\to}
}
}
\end{frame}
}
\begin{frame}[t]{The ionized population}
\raisebox{\raisealign}{Processes: }
\ifnum\pic=0
\else
\raisebox{\raisealign}{-}\scalebox{0.2}{\trimbox{3cm 0cm 1cm 0cm}{\phion{\DM}{\moves}{ph}{elec}{i}{i-1}}}
\ifnum \pic>1
\foreach  \first/\last/\from/\to in \pfiglist {
\if\to i \raisebox{\raisealign}{+} \else \raisebox{\raisealign}{-}\fi
\scalebox{0.2}{\trimbox{3cm 0cm 1cm 0cm}{\phion{\DM}{\moves}{\first}{\last}{\from}{\to}}}
}
\else\fi
\fi
\vfill
\begin{equation*}
\dot{n}_i=-(\frac{\mathcal{L}_i}{4\pi R^2}+R_i n_e)n_i+\frac{\mathcal{L}_{i-1}}{4\pi R^2} n_{i-1}+R_{i+1}n_e n_{i+1}
\end{equation*}
\vfill
\end{frame}
\end{document}

• I do not know why this question got so little upvotes. (At this point only mine.) – user121799 Mar 30 '19 at 16:21
• You will be much better off IMHO if you use the overlay-beamer-styles library and work with visible on=<...>. This will allow you to get rid of all jumps. – user121799 Mar 30 '19 at 16:28
• @marmot Thanks for the feedback! When I have sometime I will read on it. You might want to put it as an answer with an example so I (and others) can up-vote you. If not, once I get around to it I will post my results. – kabanus Mar 31 '19 at 13:18