# Animating vector on top of Bloch Sphere

I have a vector a which can only move in x-y plane and has magnitude of the radius of the sphere. Is there a way to make this a animated. Animated in the sense that it rotates within the x-y plane and the angle changes accordingly.

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{angles,quotes}

\begin{document}
\begin{tikzpicture}

\def\r{3}

% Bloch vector
\draw (0,0) node[circle,fill,inner sep=1] (orig) {} -- (\r/5,-\r/3) node[circle,fill,inner sep=0.7,label=above:$\vec{a}$] (a) {};

% Sphere
\draw (orig) circle (\r);
\draw[dashed] (orig) ellipse (\r{} and \r/3);

% Axes
\draw[->] (orig) -- ++(-\r/5,-\r/3) node[below] (x1) {$x$};
\draw[->] (orig) -- ++(\r,0) node[right] (x2) {$y$};
\draw[->] (orig) -- ++(0,\r) node[above] (x3) {$z$};

%Angles
\pic [draw=gray,text=gray,->,"$\phi$"] {angle = x1--orig--phi};
\pic [draw=gray,text=gray,<-,"$\theta$"] {angle = a--orig--x3};

\end{tikzpicture}
\end{document}


Is it possible in TikZ?

This is easy with this answer.

\documentclass{article}

\usepackage{animate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[active,tightpage]{preview}
\makeatletter
\def\@anim@@newframe{\@ifstar\@anim@newframe\@anim@newframe}
\def\@anim@newframe{\end{preview}\begin{preview}}
\renewenvironment{animateinline}[2][]{%
\let\newframe\@anim@@newframe%
\let\multiframe\@anim@multiframe%
\begin{preview}}{%
\end{preview}}
\makeatother

\usepackage{tikz}
\usetikzlibrary{angles,quotes}

\begin{document}
\begin{animateinline}[autoplay,loop]{2}
\multiframe{21}{i=0+1}{\pgfmathsetmacro{\myangle}{-4*\i}

\begin{tikzpicture}

\def\r{3}

% Bloch vector
\coordinate[label=above:$\vec{a}$] (a) at (\myangle:{\r} and {\r/3});
\coordinate (orig) at (0,0);
\draw (orig) circle(0.1);
\draw[-latex] (orig) -- (a);

% Sphere
\draw (orig) circle (\r);
\draw[dashed] (orig) ellipse (\r{} and \r/3);

% Axes
\draw[->] (orig) -- ++(-\r/5,-\r/3) node[below] (x1) {$x$};
\draw[->] (orig) -- ++(\r,0) node[right] (x2) {$y$};
\draw[->] (orig) -- ++(0,\r) node[above] (x3) {$z$};

%Angles
\pic [draw=gray,text=gray,->,"$\phi$"] {angle = x1--orig--a};
\pic [draw=gray,text=gray,<-,"$\theta$"] {angle = a--orig--x3};

\end{tikzpicture}}
\end{animateinline}
\end{document}


If you want to rotate it over the full plane, use

\documentclass{article}

\usepackage{animate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[active,tightpage]{preview}
\makeatletter
\def\@anim@@newframe{\@ifstar\@anim@newframe\@anim@newframe}
\def\@anim@newframe{\end{preview}\begin{preview}}
\renewenvironment{animateinline}[2][]{%
\let\newframe\@anim@@newframe%
\let\multiframe\@anim@multiframe%
\begin{preview}}{%
\end{preview}}
\makeatother

\usepackage{tikz}
\usetikzlibrary{angles,quotes}

\begin{document}
\begin{animateinline}[autoplay,loop]{2}
\multiframe{91}{i=0+1}{\pgfmathsetmacro{\myangle}{-4*\i}

\begin{tikzpicture}

\def\r{3}

% Bloch vector
\coordinate[label={\myangle+90}:$\vec{a}$] (a) at (\myangle:{\r} and {\r/3});
\coordinate (orig) at (0,0);
\draw (orig) circle(0.1);
\draw[-latex] (orig) -- (a);

% Sphere
\draw (orig) circle (\r);
\draw[dashed] (orig) ellipse (\r{} and \r/3);

% Axes
\draw[->] (orig) -- ++(-\r/5,-\r/3) node[below] (x1) {$x$};
\draw[->] (orig) -- ++(\r,0) node[right] (x2) {$y$};
\draw[->] (orig) -- ++(0,\r) node[above] (x3) {$z$};

%Angles
\pic [draw=gray,text=gray,->,"$\phi$"] {angle = x1--orig--a};
\pic [draw=gray,text=gray,<-,"$\theta$"] {angle = a--orig--x3};

\end{tikzpicture}}
\end{animateinline}
\end{document}


Notice that my code is for exporting the animation. If you do not want to export it, but just incorporate it in your pdf document, comment out the block

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[active,tightpage]{preview}
\makeatletter
\def\@anim@@newframe{\@ifstar\@anim@newframe\@anim@newframe}
\def\@anim@newframe{\end{preview}\begin{preview}}
\renewenvironment{animateinline}[2][]{%
\let\newframe\@anim@@newframe%
\let\multiframe\@anim@multiframe%
\begin{preview}}{%
\end{preview}}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


The animation will then show on selected viewers such as acroread.

• +1 Especially the linked question is amazing :) – Dr. Manuel Kuehner May 3 '18 at 16:14
• If possible should be given bounties. Mny thanks, is there a way to rotate vector to whole plane? – Shamina May 3 '18 at 16:18
• @marmot In my case the animation is not working? Am I doing something stupid? – Shamina May 3 '18 at 16:23
• @Shamina I animated my answer, you need to comment out a part if you want to have the animation in the pdf file. – user121799 May 3 '18 at 16:30