3

This code

\documentclass{beamer}
\usepackage{tikz}
\begin{document}
\begin{frame}{Test}
  \begin{tikzpicture}
    \draw(0, 0) -- (3, 0);
    \only<2>{
      \draw[->](0, 0) -- (0,4);
    }
  \end{tikzpicture}
\end{frame}
\end{document}

produces the output

enter image description here

The horizontal line is common to the image in each slide and I would like it to remain in the same position in each slide. However, the addition of the vertical line caused the horizontal line to be displaced.

How to solve?

Viesturs
  • 7,895
  • 1
    Quick fix: Just draw a short white horizontal line at the maximal vertical coordinate that is used in the in the animation. (\draw[white](0, 4) -- (3, 4);) –  Jan 28 '18 at 15:08
  • 1
    The subject line of your post is not what you're asking: the issue is not image size but image location. Anyway, have you heard of the command \phantom? You could code the vertical line in \phantom mode on the first page. Also, you could code both items in a single page while using \pause or \uncover. – KCd Jan 28 '18 at 22:49

2 Answers2

6

I've had these problems over and over and I ended up fixing them by additional white lines. Sam Carter taught me to use \path commands instead. In your case this amounts to adding \path(0, 4) -- (3, 4);. And one can then do some nice animations. I am adding an example that illustrates the possibilities.

\documentclass{beamer}
\usepackage{animate}
\usepackage{tikz}
%from https://tex.stackexchange.com/a/67588/121799
\tikzset{RPY/.code args={#1,#2,#3}{
    % roll, pitch, yaw
    \pgfmathsetmacro{\rollangle}{#1}%
    \pgfmathsetmacro{\pitchangle}{#2}%
    \pgfmathsetmacro{\yawangle}{#3}%
    % to what vector is the x unit vector transformed, and which 2D vector is this?
    \pgfmathsetmacro{\newxx}{cos(\yawangle)*cos(\pitchangle)}
    \pgfmathsetmacro{\newxy}{sin(\yawangle)*cos(\pitchangle)}
    \pgfmathsetmacro{\newxz}{-sin(\pitchangle)}
    \path (\newxx,\newxy,\newxz);
    \pgfgetlastxy{\nxx}{\nxy};
    % to what vector is the y unit vector transformed, and which 2D vector is this?
    \pgfmathsetmacro{\newyx}{cos(\yawangle)*sin(\pitchangle)*sin(\rollangle)-sin(\yawangle)*cos(\rollangle)}
    \pgfmathsetmacro{\newyy}{sin(\yawangle)*sin(\pitchangle)*sin(\rollangle)+ cos(\yawangle)*cos(\rollangle)}
    \pgfmathsetmacro{\newyz}{cos(\pitchangle)*sin(\rollangle)}
    \path (\newyx,\newyy,\newyz);
    \pgfgetlastxy{\nyx}{\nyy};
    % to what vector is the z unit vector transformed, and which 2D vector is this?
    \pgfmathsetmacro{\newzx}{cos(\yawangle)*sin(\pitchangle)*cos(\rollangle)+ sin(\yawangle)*sin(\rollangle)}
    \pgfmathsetmacro{\newzy}{sin(\yawangle)*sin(\pitchangle)*cos(\rollangle)-cos(\yawangle)*sin(\rollangle)}
    \pgfmathsetmacro{\newzz}{cos(\pitchangle)*cos(\rollangle)}
    \path (\newzx,\newzy,\newzz);
    \pgfgetlastxy{\nzx}{\nzy};
    \pgfkeysalso{%
      /tikz/x={(\nxx,\nxy)},
      /tikz/y={(\nyx,\nyy)},
      /tikz/z={(\nzx,\nzy)}
    }
  }
}

\newcommand{\savedx}{0}
\newcommand{\savedy}{0}
\newcommand{\savedz}{0}
%
\newcommand{\rotateRPY}[4][0/0/0]% point to be saved to \savedxyz, roll, pitch, yaw
{   \pgfmathsetmacro{\rollangle}{#2}
    \pgfmathsetmacro{\pitchangle}{#3}
    \pgfmathsetmacro{\yawangle}{#4}

    % to what vector is the x unit vector transformed, and which 2D vector is this?
    \pgfmathsetmacro{\newxx}{cos(\yawangle)*cos(\pitchangle)}% a
    \pgfmathsetmacro{\newxy}{sin(\yawangle)*cos(\pitchangle)}% d
    \pgfmathsetmacro{\newxz}{-sin(\pitchangle)}% g
    \path (\newxx,\newxy,\newxz);
    \pgfgetlastxy{\nxx}{\nxy};

    % to what vector is the y unit vector transformed, and which 2D vector is this?
    \pgfmathsetmacro{\newyx}{cos(\yawangle)*sin(\pitchangle)*sin(\rollangle)-sin(\yawangle)*cos(\rollangle)}% b
    \pgfmathsetmacro{\newyy}{sin(\yawangle)*sin(\pitchangle)*sin(\rollangle)+ cos(\yawangle)*cos(\rollangle)}% e
    \pgfmathsetmacro{\newyz}{cos(\pitchangle)*sin(\rollangle)}% h
    \path (\newyx,\newyy,\newyz);
    \pgfgetlastxy{\nyx}{\nyy};

    % to what vector is the z unit vector transformed, and which 2D vector is this?
    \pgfmathsetmacro{\newzx}{cos(\yawangle)*sin(\pitchangle)*cos(\rollangle)+ sin(\yawangle)*sin(\rollangle)}
    \pgfmathsetmacro{\newzy}{sin(\yawangle)*sin(\pitchangle)*cos(\rollangle)-cos(\yawangle)*sin(\rollangle)}
    \pgfmathsetmacro{\newzz}{cos(\pitchangle)*cos(\rollangle)}
    \path (\newzx,\newzy,\newzz);
    \pgfgetlastxy{\nzx}{\nzy};

    % transform the point given by #1
    \foreach \x/\y/\z in {#1}
    {   \pgfmathsetmacro{\transformedx}{\x*\newxx+\y*\newyx+\z*\newzx}
        \pgfmathsetmacro{\transformedy}{\x*\newxy+\y*\newyy+\z*\newzy}
        \pgfmathsetmacro{\transformedz}{\x*\newxz+\y*\newyz+\z*\newzz}
        \xdef\savedx{\transformedx}
        \xdef\savedy{\transformedy}
        \xdef\savedz{\transformedz}     
    }
}

\begin{document}
\definecolor{darkgreen}{rgb}{0.0, 0.6, 0.2}
\newcount\myangle
\newcommand{\somedrawing}%
{   \coordinate (a) at (-2,0,0);
    \coordinate (b) at (-2,4,0);
    \coordinate (c) at (2,4,0);
    \coordinate (d) at (2,0,0);
    \draw[fill=gray,opacity=0.3] (a)--(b)--(c)--(d)--(a);
}
\begin{frame}
\frametitle{Orbifold pillow}
\label{frm:Pillow}
\animate<5-50>
\animatevalue<5-52>{\myangle}{0}{180}
\transduration<5-50>{0.4}
\begin{overlayarea}{\textwidth}{\textheight}
\begin{tikzpicture}[scale=0.9,odot/.style={inner sep=3pt,outer sep=2pt,minimum
width=0.2cm,circle,line width=2pt}]
    \path (-5,-4,0)--(-5,4,0);
    \draw[fill=gray!70] (-2,-4,0)--(6,-4,0)--(6,4,0)--(-2,4,0)--(-2,-4,0);
\only<2>{
   \draw[color=blue,line width=0.1cm] (-2,-4,0) -- (6,-4,0);
   \draw[color=blue,line width=0.1cm] (-2,4,0) -- (6,4,0);
   \draw[color=darkgreen,line width=0.1cm] (-2,-4,0) -- (-2,4,0);
   \draw[color=darkgreen,line width=0.1cm] (6,-4,0) -- (6,4,0);
}
\pause
\only<3-4>{
   \draw[fill=gray,opacity=0.3] (-2,-4,0)--(2,-4,0)--(2,4,0)--(-2,4,0)--(-2,-4,0);
   \draw[->,line width=5pt,red] (0.5,0.2,0) arc[radius=1.5cm, start angle=180, end angle=0];
%   \node at (2,2.2,0){\ROT{$\Z2$}};
}
\pause
\only<4>{
\node[odot,fill=blue,draw=red] (n1) at (-2,-4,0) {};    
\node[odot,fill=blue,draw=red] (n2) at (2,-4,0) {}; 
\node[odot,fill=blue,draw=red] (n3) at (-2,0,0) {}; 
\node[odot,fill=blue,draw=red] (n4) at (2,0,0) {};
}
\pause
\only<5->{
    \draw[fill=gray,opacity=0.3] (-2,-4,0)--(2,-4,0)--(2,0,0)--(-2,0,0)--(-2,-4,0);
    \begin{scope}[RPY={\the\myangle,0,0},scale=1.11] %% blue plane
     \somedrawing
    \end{scope}
}   
\node at (6.8,-3.8){\hyperlink{frm:Pillow}{\beamergotobutton{back}}};
\end{tikzpicture}
\end{overlayarea}
\end{frame}
\end{document}

enter image description here

4

In cases like this I try to change the bounding box of the picture. The easiest way here is to add an empty node at (0,4),

\begin{frame}{Test}
  \begin{tikzpicture}
    \draw(0, 0) -- (3, 0);
    \node[inner sep=0pt] at (0,4){};
    \only<2>{
      \draw[->](0, 0) -- (0,4);
    }
  \end{tikzpicture}
\end{frame}

An alternative is to specifically set the bounding box to (0,0) rectangle (3,4). That will include the complete coordinate system.

\begin{frame}{Test}
  \begin{tikzpicture}
    \useasboundingbox (0,0) rectangle (3,4);
    \draw(0, 0) -- (3, 0);
    \only<2>{
      \draw[->](0, 0) -- (0,4);
    }
  \end{tikzpicture}
\end{frame}

A bit more general, the current bounding box can be expande with the point (0,4) with the following.

\begin{frame}{Test}
  \begin{tikzpicture}
    \draw(0, 0) -- (3, 0);
    \useasboundingbox (current bounding box.south west) -- (current bounding box.north east) -- (0,4);
    \only<2>{
      \draw[->](0, 0) -- (0,4);
    }
  \end{tikzpicture}
\end{frame}

All three examples above give the result:

enter image description here

StefanH
  • 13,823