[Tex/LaTex] drawing(uncover) a curve bit-by-bit using tikzpcture and beamer and \onslide

beameroverlaystikz-pgf

I wish to uncover a curve (a potential energy v distance) bit-by-bit while explaining what is going on.

The code below is only ok, the problem is that the curve changes shape from slide 3 to slide 4. I can live with it and am not losing too much sleep over it 😉 but I would like to find the obviously simple solution.

I realise it is to do with the way I am plotting the curve. I plot the curve on each slide as a separate curve with just extra coordinate points. (full curve coordinates given in each and commented out the "extra points").

Ideally, I should be uncovering just sections of the curve.

These solutions Uncover line segments of a TikZ graph in Beamer and Uncover segments of a TikZ path in Beamer work with straight lines not curves…

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{frame}{Real Gases}

\begin{tikzpicture}
\onslide<6->{\draw[green!20, pattern color =green!20, thin,pattern=north west lines] (0,-5) rectangle (2,2)  node[rotate=90,midway,above] {\tiny{\textcolor{green!30!black}{repulsive forces dominant}}};}% drawn first so appear as underlay
\onslide<6->{\draw[red!0, pattern color =red!20, thin,pattern=north west lines] (2,-5) rectangle (10,2)  node[midway,above] {\tiny{\textcolor{red!30!black}{attractive forces dominant}}};}

\onslide<1->{\draw[thick,->] (0,0) --(10,0) node[above left=0.2] {\tiny{Separation}};}
\onslide<1->{\draw[thick,->] (0,-5) --(0,2) node[rotate=90, near end,above] {\tiny{Potential Energy}};}
\onslide<2>{\draw[color=blue, thick,-] plot[smooth] coordinates{%(1.35,2)(1.4,1.75)(1.5,1.5) (2,0) (3,-3) (4,-5)(5,-4.5)(6,-2)(7,-0.8)
(8,-0.3)(9,-0.1) (10,0)};}
\onslide<3>{\draw[color=blue, thick,-] plot[smooth] coordinates{%(1.35,2)(1.4,1.75)(1.5,1.5)(2,0)(3,-3)
(4,-5)(5,-4.5)(6,-2)(7,-0.8)(8,-0.3)(9,-0.1) (10,0)};}
\onslide<4>{\draw[color=blue, thick,-] plot[smooth] coordinates{%(1.35,2)(1.4,1.75)(1.5,1.5)
(2,0) (3,-3) (4,-5)(5,-4.5)(6,-2)(7,-0.8)(8,-0.3)(9,-0.1) (10,0)};}
\onslide<5->{\draw[color=blue, thick,-] plot[smooth] coordinates{(1.35,2)(1.4,1.75)(1.5,1.5)(2,0) (3,-3) (4,-5)(5,-4.5)(6,-2)(7,-0.8)(8,-0.3)(9,-0.1) (10,0)};}
\end{tikzpicture}
\end{frame}
\end{document}

Best Answer

If you're happy to swap out the smooth plot for a hobby curve then you can use a feature of that package to plot only part of the curve. In short, I wanted to be able to compute a curve but only draw parts of it. So I added a blank key which means that the current segment is not drawn. However it is still used as part of the computation of the curve meaning that the curve is the same whether or not that segment is drawn.

By applying the blank key to the coordinates in the right order, I can successively reveal parts of the curve as you require. The mechanism for applying a style on a particular slide comes from Matthew Leingang's answer to How to make beamer overlays with Tikz node.

I tried to fit the basic idea of your curve. If you need a more precise fit then you need to add more control points.

\documentclass{beamer}
%\url{https://tex.stackexchange.com/q/116482/86}
\usepackage{tikz}
\usetikzlibrary{patterns,hobby}

\tikzset{
  onslide/.code args={<#1>#2}{%
    \only<#1>{\pgfkeysalso{#2}}%
  },
  alt/.code args={<#1>#2#3}{%
    \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}%
  }
}

\begin{document}
\begin{frame}{Real Gases}
\begin{tikzpicture}
\draw<6->[green!20, pattern color =green!20, thin,pattern=north west lines] (0,-5) rectangle (2,2)  node[rotate=90,midway,above,font=\tiny,text=green!30!black] {repulsive forces dominant};
% drawn first so appear as underlay

\draw<6->[red!0, pattern color =red!20, thin,pattern=north west lines] (2,-5) rectangle (10,2)  node[midway,above,font=\tiny,text=red!30!black] {attractive forces dominant};

\draw[thick,->] (0,0) --(10,0) node[above left=0.2,font=\tiny] {Separation};
\draw[thick,->] (0,-5) --(0,2) node[rotate=90, near end,above,font=\tiny] {Potential Energy};

\draw<2->[color=blue, thick,-,use Hobby shortcut]   ([out angle=-70]1.35,2) .. ([onslide=<-4>{blank}]2,0) .. ([onslide=<-3>{blank}]3,-4) .. ([onslide=<-3>{blank}]4,-5) .. ([onslide=<-2>{blank}]5,-4.5) .. ([onslide=<-2>{blank}]8,-0.3) ..  ([in angle=180]10,0);
\end{tikzpicture}
\end{frame}
\end{document}