6

In beamer, is it possible to uncover a sequence of connected line segments drawn in tikz? For example, in the following figure, I want to show the line segments in the third \draw one by one after each -- (except the first one). Is there a way to achieve this, or is it just impossible?

\begin{tikzpicture}[scale=.5]
  \draw[<->,>=latex](0,10)--(0,0)--(10,0)node[below]{$Q$};
  \draw<2-> node[left]at(0,10){$P$} node[below]at(10,0);
  \draw<3->[very thick, red](0,6)--(4,6)--(4,3)--(6.67,3)--(6.67,2)--(9.33,2)--(9.33,0);
\end{tikzpicture}
Mensch
  • 65,388
Herr K.
  • 17,946
  • 4
  • 61
  • 118

1 Answers1

5

The idea is to use a loop; the obvious choice would be to use \foreach and the remember ... as ... (initially ...) syntax; however, after struggling for over half an hour with fruitless variations of a double-variable use of this syntax, I decided to do a manual version:

\documentclass{beamer}
\usepackage{tikz}

\begin{document}

\begin{frame}
\begin{tikzpicture}[scale=0.5]
\draw<+->[<->,>=latex] (0,10) node[left] {$P$}-- (0,0) -- (10,0) node[below]{$Q$};
\xdef\lastx{0}
\xdef\lasty{6}
\foreach \x/\y in {4/6,4/3,6.667/3,6.667/2,9.33/2,9.33/0}
{
  \draw<+->[very thick,red] (\lastx,\lasty) -- (\x,\y);
  \xdef\lastx{\x}\xdef\lasty{\y}
}
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128