# drawing(uncover) a curve bit-by-bit using tikzpcture and beamer and \onslide

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}

-
I think plotting the sequence x_n using tikz could be a valid starting point. –  Claudio Fiandrino May 28 '13 at 9:18
Another related question and solution: how to animate a path or how to draw starting fraction of a complex but linear path –  Ignasi May 28 '13 at 9:35

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{http://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}

-
When I run this through pdflatex I get the following error: "! Package pgfkeys Error: I do not know the key '/tikz/blank' and I am going to ignore it. Perhaps you misspelled it." Am I missing something? I copied the code directly. When I deleted the "{blank}" I still get errors... –  Leeser May 28 '13 at 11:41
@Leeser Do you have the latest version of the hobby package? I think I updated too late to get it to CTAN before TL2012 was frozen so if you've just used tlmgr then it might not have updated. –  Andrew Stacey May 28 '13 at 12:39
Yep I have the version from September 2012. When i try to copy the files from CTAN to my texmf-dist it just won't paste (OSX). How can I update it. Excuse my simplistic question, only recently switched from PC to Apple... –  Leeser May 28 '13 at 14:08
@Leeser take a look at tex.stackexchange.com/q/1137/86 as a place to start –  Andrew Stacey May 28 '13 at 15:20
Thank you , newest version installed locally, works a treat –  Leeser May 29 '13 at 15:13

Next code shows how to use Jake's answer to How to animate a path or how to draw starting fraction of a complex but linear path? with your example.

Because this solution draws first portion of a path I reversed your coordinates. The main problem will be to adjust which portion do you want to draw in each slide.

\documentclass{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations}

% A simple empty decoration, that is used to ignore the last bit of the path
\pgfdeclaredecoration{ignore}{final}
{
\state{final}{}
}

% Declare the actual decoration.
\state{initial}[
width={0pt},
next state=middle
]
{\decoration{moveto}}

\state{middle}[
next state=final
]
{\decoration{curveto}}

\state{final}
{\decoration{ignore}}
}

\tikzset{middle segment/.style={decoration={middle},decorate, segment length=#1}}

\newcommand{\mypath}{(10,0)(9,-0.1)(8,-0.3)(7,-0.8)(6,-2)(5,-4.5)(4,-5)(3,-3)(2,0)(1.5,1.5)(1.4,1.75)(1.35,2)}

\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<3>{\draw[middle segment=0.3,color=blue, thick,-] plot[smooth] coordinates{\mypath};}
\onslide<4>{\draw[middle segment=0.6,color=blue, thick,-] plot[smooth] coordinates{\mypath};}
\onslide<5>{\draw[middle segment=0.8,color=blue, thick,-] plot[smooth] coordinates{\mypath};}
\onslide<6>{\draw[color=blue, thick,-] plot[smooth] coordinates{\mypath};}

\end{tikzpicture}
\end{frame}
\end{document}


-
looks good, hence the upvote, but I'm still not happy with the end point of the curve (not your fault!, maybe I'm losing more sleep over it than I previously thought, never happy!). Maybe AndrewStacey's Hobby approach gives better endings. –  Leeser May 28 '13 at 11:46
@Leeser. Thaks for upvote. You can apply same solution once you decide where to place end point. For potential energy diagrams, Clemens Niederberger created endiagram package. May be it's useful for you. –  Ignasi May 28 '13 at 11:58
I had seen the 'endiagram' package but it's not what I need. Useful for other aspects alright though. –  Leeser May 28 '13 at 12:58