# Uncover segments of a TikZ path in Beamer

I have a question about uncovering segments of a TikZ path in Beamer? As a simple example, consider the following path made up of a horizontal and a vertical segment.

\begin{tikzpicture}
\draw(0,0)--(1,0)--(1,1);
\end{tikzpicture}

On the slide, I want to show the horizontal segment first, and then the vertical segment. One way to do this is the following:

\begin{tikzpicture}
\draw<1->(0,0)--(1,0);
\draw<2->(1,0)--(1,1);
\end{tikzpicture}

However, this produces two disjoint line segments (it's clear if you zoom in on the point (1,0)). Is there an alternative way to uncover the two segments as a connected path, rather than two disjoint lines?

-
– hpesoj626 Nov 20 '12 at 5:25
Another related post: tex.stackexchange.com/questions/6135/… – hpesoj626 Nov 20 '12 at 5:26
@hpesoj626, thanks and I read these posts before. But what the methods mentioned therein work for overlays between different TikZ commands (i.e. they are about overlays between \sometikzcommand1[options] <overlay_part_1>; and \sometikzcommand2[options] <overlay_part_2>;). In my problem, however, I need overlay within ONE TikZ command, i.e. \tikzcommand[options] <overlay_part_1> ... <overlay_part_2>. I'm not sure I see how to apply the methods suggested in those two posts... – Herr K. Nov 20 '12 at 5:46

The “related” and in the comments linked question deal, sadly, only with content that is not on one path.

# The manual solution

Creating two paths, where the second (longer one) is only uncovered in the second slide.

\visible<1>{\draw(0,0)--(1,0);}
\visible<2->{\draw(0,0)--(1,0)--(1,1);}

We could use the first \draw without \visible as it just gets over-drawn, but maybe you want to play around with opacity or hide stuff again.

# The better (?) solution

Changing the line to in the first slide to move to (better would be just leave me alone but use the correct bounding box so that the picture doesn't jump around but in this case it works just fine).

In this particular minimal example, beamer doesn't know that there's a second slide when we use only onslide=<1>, so we need to clarify that.

## Variant A

Specifying the second (and following) operation as line to or empty as the default to path is line to (a.k.a. --).

## Variant B

Creating a second slide somehow (\only<2>{}), again using the fact that the default to is a line to operation.

## Better-er

Creating a TikZ style that combines hiding and showing.

hideshow/.style args={<#1><#2>#3}{%
onslide=<#1>{move to},
onslide=<#2>{#3}
}

# Code

\documentclass{beamer}
\usepackage{tikz}
\tikzset{
onslide/.code args={<#1>#2}{% http://tex.stackexchange.com/a/6155/16595
\only<#1>{\pgfkeysalso{#2}}
},
hideshow/.style args={<#1><#2>#3}{%
onslide=<#1>{move to},
onslide=<#2>{#3}
}
}
\begin{document}

\begin{frame}% manual
\begin{tikzpicture}[scale=5,very thick]
\visible<1>{\draw(0,0)--(1,0);}
\visible<2->{\draw(0,0)--(1,0)--(1,1);}
\end{tikzpicture}
\end{frame}

\begin{frame}% "better" A
\begin{tikzpicture}[scale=5,very thick]
\draw(0,0)--(1,0) to[onslide=<1>{move to},onslide=<2>{}] (1,1);% or onslide=<2>{line to}
\end{tikzpicture}
\end{frame}

\begin{frame}\only<2>{}% "better" B
\begin{tikzpicture}[scale=5,very thick]
\draw(0,0)--(1,0) to[onslide=<1>{move to}] (1,1);
\end{tikzpicture}
\end{frame}

\begin{frame}% "better-er"
\begin{tikzpicture}[scale=5,very thick]
\draw(0,0)--(1,0) to[hideshow=<1><2>{}] (1,1);
\end{tikzpicture}
\end{frame}

\end{document}

# Output (cropped)

Apparently such key is not needed, see the output of the following code:

\documentclass[beamer]{standalone}
\usepackage{tikz}
\tikzset{
onslide/.code args={<#1>#2}{
\only<#1>{\pgfkeysalso{#2}}
},
hideshow/.style args={<#1><#2>#3}{%
onslide=<#1>{move to},
onslide=<#2>{#3}
}
}
\begin{document}
\begin{frame}
\begin{tikzpicture}[scale=5,very thick]
\draw[line width=1cm,line join=round](0,0)--(1,0) to[hideshow=<1><2->{}] (1,1);
\end{tikzpicture}
\end{frame}
\end{document}
-

Next code shows how to use decoration library to draw a path step by step in several beamer frames. I mixed Jake's answer to How to animate a path or how to draw starting fraction of a complex but linear path? and overlay drawing possibilities of path shown in Prim's algorithm example on TeXample.net.

The complete code is

\documentclass{beamer}
\usepackage{tikz}
\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}}

\begin{document}

\begin{frame}
\begin{tikzpicture}
\node[fill=blue!15,draw] at (0,0) (a) {A};
\node[fill=blue!15,draw] at (2,2) (b) {B};
\foreach \pos in {0.25,0.5,0.75,1}
\path<+->[draw, middle segment=\pos, thick, red] (a) -| (b);
\end{tikzpicture}
\end{frame}

\end{document}

and the result (in one compound figure while I learn how to make animated gifs) is

Two first frames show a different thickness here due a rendering problem with the viewer.

-