3

I'm learning how to produce animations in LaTeX. After some trials with different pictures, I would like to animate the following picture and start from the answer that Marmot provided me in this question Drawing a simple plane in 3D space. In particular, I would like to move the gray rectangle around (0,0,0) and lift it up, in order to illustrate what happens if it starts to intesect the positive orthant. The following MWE does something else, as it animates the dimensions of the rectangle. I think I should rather define the rectangle in terms of a plane and then I will change its slope. How can I do?

\documentclass{beamer}

\usepackage{amsmath}    % need for subequations
\usepackage{amsfonts}
\usepackage{tikz}

\usetikzlibrary{calc, shadings} 
\usetikzlibrary{calc,intersections} 
\usetikzlibrary{positioning,arrows.meta}
\usepackage{pgf,tikz,pgfplots}
\pgfplotsset{compat=1.15}
\usepackage{mathrsfs}
\usetikzlibrary{arrows}
\usepackage{animate}


\usepackage{tikz-3dplot}

\usetikzlibrary{3d} %add it to draw the plane
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
  \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
  \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
  \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
  \tikz@canvas@is@plane
}
\makeatother

\let\oldemptyset\emptyset 



\begin{document}
\begin{frame}

\tdplotsetmaincoords{105}{-30}

\begin{animateinline}[loop,poster=first,controls]{30}
\multiframe{36}{rt=0+10}
{

    \begin{tikzpicture}[tdplot_main_coords,font=\small]
     \tdplotsetrotatedcoords{00}{30}{0}
    \begin{scope}[tdplot_rotated_coords]

    \begin{scope}[canvas is xy plane at z=0]
    \fill[blue!40,fill opacity=0.3] (-{cos(\rt)*5},-{sin(\rt)*5}) rectangle ({cos(\rt)*5},{sin(\rt)*5}); 
    \draw[very thick] (-2,0) -- (2,0); %draws market line
    \path (-150:2) coordinate (H) (-1.5,0) coordinate(X);
    \pgflowlevelsynccm

    \draw[very thick,-stealth,gray] (0,0) -- (-30:1.5); %draws ``projection'' of $\Phi$
    \end{scope}

    \draw[stealth-] (H) -- ++ (-1,0,0.2) node[pos=1.3]{$H$}; %draws arrow
    \draw[stealth-] (X) -- ++ (0,1,0.2) node[pos=1.3]{$X$}; %draws arrow
    \draw[very thick,-stealth] (0,0,0) coordinate (O) -- (0,0,3) node[right]{$\varphi$}; %draws vector 
    \end{scope}

    \pgfmathsetmacro{\Radius}{1.5} %using a macro
    \draw[-stealth]  (O)-- (2.5*\Radius,0,0) node[pos=1.15] {spot $0$};
    \draw[-stealth] (O) -- (0,3.5*\Radius,0) node[pos=1.15] {spot $2$};
    \draw[-stealth] (O) -- (0,0,2.5*\Radius) node[pos=1.05] {spot $1$};
    \end{tikzpicture} 
}


\end{animateinline}

\end{frame}
\end{document}
Nenne
  • 697
  • 1
    Does \fill[blue!40,fill opacity=0.3] (-{cos(\rt)*5},-5,-{sin(\rt)*5}) -- (-{cos(\rt)*5},5,-{sin(\rt)*5}) -- ({cos(\rt)*5},5,{sin(\rt)*5}) -- ({cos(\rt)*5},-5,{sin(\rt)*5}) -- cycle; outside the canvas is xy plane ... scope give you what you want? –  Jun 26 '19 at 14:20
  • 1
    Add a common bonding box (\useasboundingbox ( ... , ... ) rectangle ( ... , ... );) large enough to encompass all objects. – AlexG Jun 26 '19 at 14:35
  • 1
    @AlexG Nice to see you! If you test such animations, how do you do it? I can only view the on acroread, which does however not seem to reload the file. So I compile, open acroread, find something that needs to be improved, close acroread, compile again etc. Is there a better way? –  Jun 26 '19 at 14:48
  • @AlexG What parameters should I put inside the round brakets of '\useasboundingbox'? – Nenne Jun 26 '19 at 15:07
  • @marmot should I leave the parameters in the rectangle at this line of the code? '\fill[blue!40,fill opacity=0.3] (-{cos(\rt)5},-{sin(\rt)5}) rectangle ({cos(\rt)5},{sin(\rt)5});' – Nenne Jun 26 '19 at 15:10
  • 1
    @marmot: Replace doc class with \documentclass[export]{standalone} and open PDF in the viewer of your choice. Then PgUp/PgDn. – AlexG Jun 26 '19 at 15:14
  • 1
    @marmot : Even better: Replace doc class with \documentclass[dvisvgm]{article}\pagestyle{empty}, then latex+latex+dvisvgm --font-format=woff --exact --zoom=-1 and open SVG in Chromium. After changes to the sources and recompilation just click reload button in the web browser. – AlexG Jun 26 '19 at 15:20
  • @Nenne Depends on what you wish to achieve. Your rectangle changes dimensions, as you say. My suggestion rotates the rectangle about the y-axis. Keeping both may not make too much sense. –  Jun 26 '19 at 15:20
  • @marmot no, the rectangle does not change its dimensions, but its slope only – Nenne Jun 26 '19 at 15:21
  • @Nenne In that case I feel that my suggestion above makes sense. If you want to rotate about another axis, permute the coordinate. Alternatively you could work with rotated coordinates and draw the rectangle in the plane, as you did, but in this case you want to animate the rotated coordinates \tdplotsetrotatedcoords, then start a scope tdplot_rotated_coords and then the canvas is xy plane ... scope. –  Jun 26 '19 at 16:35
  • @marmot what could be a good option to fill \useasboundingbox ( ... , ... ) rectangle ( ... , ... ); ? I tried to throw some numbers in, but the upper part of the axis is no longer visible – Nenne Jun 26 '19 at 19:18
  • In this answer there is a method that finds the bounding box for animations. I have not yet tried it out with animateinline, though, but I believe it should work there, too. If you tell me whether or not the above suggestion is correct, or what you would like to have instead, I could try out. –  Jun 26 '19 at 19:23

1 Answers1

5

This is something that rotates the plane (but I do not know if these are the planes and rotation axes you had in mind). It also adjusts the bounding box in such a way that it fits all animations steps fit tightly. To this end, a slight variation of this answer is used but you may also be interested in this answer, which I did not try out here. Please note that you need to compile twice. The animated gif is produced along the lines of this comment and, in particular, this answer, and the file has comments what you need to do in order to recreate the gif.

%\documentclass[export]{standalone} % <- use for multipage pdf
\documentclass{beamer}  % <- comment out for multipage pdf
\usepackage{animate}
\usepackage{tikz}


\usepackage{tikz-3dplot}

\usetikzlibrary{3d} %add it to draw the plane
\makeatletter
\tikzoption{canvas is xy plane at z}[]{%
  \def\tikz@plane@origin{\pgfpointxyz{0}{0}{#1}}%
  \def\tikz@plane@x{\pgfpointxyz{1}{0}{#1}}%
  \def\tikz@plane@y{\pgfpointxyz{0}{1}{#1}}%
  \tikz@canvas@is@plane
}

\def\ExportBB{\edef\figbb{(\xmin,\ymin) rectangle (\xmax,\ymax)}
\immediate\write\@mainaux{\xdef\string\figbb{\figbb}\relax}}
\makeatother

\let\oldemptyset\emptyset 



\begin{document}
\begin{frame} % <- comment out for multipage pdf

\tdplotsetmaincoords{105}{-30}
\pgfmathsetmacro{\xmin}{0}
\pgfmathsetmacro{\xmax}{0}
\pgfmathsetmacro{\ymin}{0}
\pgfmathsetmacro{\ymax}{0}
\begin{animateinline}[loop,poster=first,controls]{30}
\multiframe{36}{rt=0+10}
{

    \begin{tikzpicture}[tdplot_main_coords,font=\small]
    \ifdefined\figbb\relax
    \path \figbb;
    \fi

    % this is the stuff that gets rotated
    \tdplotsetrotatedcoords{00}{\rt}{0}
    \begin{scope}[tdplot_rotated_coords]

     \begin{scope}[canvas is xy plane at z=0]
     \fill[blue!40,fill opacity=0.3] (-5,-5) rectangle (5,5);
     % (-{cos(\rt)*5},-{sin(\rt)*5}) rectangle ({cos(\rt)*5},{sin(\rt)*5}); 
     \pgflowlevelsynccm

     \draw[very thick,-stealth,gray] (0,0) -- (-30:1.5); %draws ``projection'' of $\Phi$
     \end{scope}

     \draw[very thick,-stealth] (0,0,0) coordinate (O) -- (0,0,3) node[right]{$\varphi$}; %draws vector 
    \end{scope}

    \begin{scope}[canvas is xy plane at z=0]
     \draw[very thick] (-2,0) -- (2,0); %draws market line
     \path(-1.5,0) coordinate(X);
     \path (-150:2) coordinate (H);
    \end{scope}
    \draw[stealth-] (X) -- ++ (0,1,0.2) node[pos=1.3]{$X$}; %draws arrow
    \draw[stealth-] (H) -- ++ (-1,0,0.2) node[pos=1.3]{$H$}; %draws arrow

    \pgfmathsetmacro{\Radius}{1.5} %using a macro
    \draw[-stealth]  (O)-- (2.5*\Radius,0,0) node[pos=1.15] {spot $0$};
    \draw[-stealth] (O) -- (0,3.5*\Radius,0) node[pos=1.15] {spot $2$};
    \draw[-stealth] (O) -- (0,0,2.5*\Radius) node[pos=1.05] {spot $1$};
    \path let \p1=(current bounding box.south west),
    \p2=(current bounding box.north east)
    in \pgfextra{%
    \pgfmathsetmacro{\xmin}{min(\x1,\xmin)}
    \pgfmathsetmacro{\xmax}{max(\x2,\xmax)}
    \pgfmathsetmacro{\ymin}{min(\y1,\ymin)}
    \pgfmathsetmacro{\ymax}{max(\y2,\ymax)}
    \xdef\xmin{\xmin pt}
    \xdef\xmax{\xmax pt}    
    \xdef\ymin{\ymin pt}
    \xdef\ymax{\ymax pt}    
    };
    \end{tikzpicture} 
\ExportBB   
}


\end{animateinline}

\end{frame} % <- comment out for multipage pdf
\end{document}

enter image description here

  • Thank you, @marmot, and sorry for the late reply. Your suggestion was good, and the code above does exactly what I wanted. However, if I understood correctly, I'd like the line $X$ not to move. – Nenne Jun 27 '19 at 06:59
  • in fact your code is still a little difficult for me, so I ask you where I can find a very good reference about macros in LaTeX... I still do not understand completely how they work – Nenne Jun 27 '19 at 07:33
  • @Nenne So only the plane should rotate or the plane and the line that connects to \varphi? –  Jun 27 '19 at 18:00
  • The plane and also the vector $\varphi$ – Nenne Jun 27 '19 at 19:49
  • 1
    @Nenne Done. Depending on what your aims are you may need to move the stuff with H to the rotated coordinates. –  Jun 27 '19 at 20:40
  • Thank you for your help with the code. Can you suggest me some useful material about how to learn to write macros in Latex? – Nenne Jul 01 '19 at 08:51
  • 1
    @Nenne I actually do not know a tutorial for that. Personally I learn most from examples. You can try texdoc texbytopic. –  Jul 01 '19 at 13:17
  • If this code is included in latex and I print the pdf, the printed image is the original picture, obviously without any animation, isn't it? – Nenne Jul 03 '19 at 12:58
  • 1
    @Nenne Honestly, I do not know. I never printed out slides with animations. The only thing I know is that there are many options concerning the handout but I almost never used those. –  Jul 03 '19 at 13:08