Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want a retangle with 80 diagonal lines:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------
\foreach \i in {1,2,...,80} {
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\i}; 
}
%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}

\end{document}

enter image description here

Now I have a data file that looks like

4, % Line 1 is the top left corner
5,
50,
51,
52,
6,
7,
8,
9,
10,
22,
...

Now I want that the 80 lines appear in a animate animation in that order line-by-line. I made a animated gif manually to show the desired output:

enter image description here

But from here I do not have the right idea to proceed.

Solution from Jake

Thanks for the answers! I accept Jake's answer because it uses an external file which is important for me. My real example hat over 1200 lines. Here's the solution (Jake's answer is missing the filecontents envoronment):

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{animate}

\usepackage{filecontents}

\begin{filecontents}{index.dat}
4,
5,
50,
51,
52,
6,
7,
8,
9,
10,
22,
\end{filecontents}

\begin{document}

\pgfplotstableread[col sep=comma]{index.dat}\indextable

\begin{animateinline}[controls]{2}
\multiframe{11}{imax=0+1}{
\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------

\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------

\foreach\i in {0,...,\imax}{
\pgfplotstablegetelem{\i}{[index]0}\of{\indextable}
\edef\currentindex{\pgfplotsretval}
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\currentindex};
}

%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}}
\end{animateinline}

\end{document}

Here is the result:

enter image description here

share|improve this question

3 Answers 3

up vote 4 down vote accepted

You can read the data file containing the ordered values into a PGFPlots table macro and then loop over that within the animateinline environment:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{animate}

\usepackage{filecontents}


\begin{document}

\pgfplotstableread[col sep=comma]{index.dat}\indextable

\begin{animateinline}{2}
\multiframe{10}{imax=0+1}{
\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------

\foreach\i in {0,...,\imax}{
\pgfplotstablegetelem{\i}{[index]0}\of{\indextable}
\edef\currentindex{\pgfplotsretval}
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\currentindex};
}




%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}}
\end{animateinline}

\end{document}
share|improve this answer
    
Hello. Is the \edef important? Or could I use \newcommand? –  Manuel Kuehner May 10 at 8:22

This works with beamer. The loop is outside axis.

\documentclass{beamer}

\usepackage{tikz}
\usepackage{pgfplots}

  \tikzset{
    invisible/.style={opacity=0},
    visible on/.style={alt={#1{}{invisible}}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
  }

\begin{document}

\begin{frame}
\begin{tikzpicture}
\foreach \i [count=\j] in  {4,5,50,51,52,6,7} {
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  

\addplot[
domain=0:60,
line width=1pt, visible on=<\j->] 
  {4/6*x+40-\i}; 
\end{axis}}
\end{tikzpicture}
\end{frame}
\end{document}

Later I'll made a gif and add some more information.

enter image description here enter image description here

share|improve this answer
    
Great - thanks. Can you expand it so that I can use an external file with the line order? Using it in Beamer is very helpful. –  Manuel Kuehner May 9 at 16:14

This is an attempt where the list of order can be changed for more flexibility. App is a command that takes two arguments #1= # of frames and #2= the order of line drawing. Be aware that the #1 in \multiframe{#1} should be the last in the App list.

The App command itself is a whiledo loop that draw off-diagonal lines.

Please cut and paste to show the animation take places.

enter image description here

enter image description here

Code

\documentclass[border=10pt]{standalone}
\usepackage{animate}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}


\newcounter{n}
\setcounter{n}{01}
\newcommand{\App}[2]{
\whiledo{\value{n}=#1 \and \icount=#1}{
 \stepcounter{n}            % #1= # of frame
     \foreach \i in {#2} {  % #2=list
\addplot[
domain=0:60,
line width=1pt,
]
  {4/6*x+40-\i}; 
}}}


\begin{document}
%\begin{center}
\begin{animateinline}[loop,poster =first, controls]{1}
\multiframe{10}{icount=0+1}
{
\begin{tikzpicture}
\begin{axis}[
%------------------------
%% Options
%------------------------
    width= 100mm,
    %height = 50mm,
    xmax=70,
    xmin=0,
    ymax=50,
    ymin=0,
    axis x line=bottom,
    axis y line=left,
    enlargelimits
]
%------------------------
%% Define Clipping
%------------------------
\pgfplotsextra{%
    \clip (axis cs:0,0) rectangle (axis cs:60,40);
    % http://www.latex-community.org/forum/viewtopic.php?f=45&t=22837&start=0
}   
%------------------------    
%% Loop
%------------------------
\App{1}{4}
\App{2}{4,5}
\App{3}{4,5,50}
\App{4}{4,5,50,51}
\App{5}{4,5,50,51,52}
\App{6}{4,5,50,51,52,6}
\App{7}{4,5,50,51,52,6,7,8}
\App{8}{4,5,50,51,52,6,7,8,9}
\App{9}{4,5,50,51,52,6,7,8,9,10}
\App{10}{4,5,50,51,52,6,7,8,9,10,22}
%------------------------
%% Draw retangle
%------------------------
\draw[red,line width=2pt] (axis cs:0,0) rectangle (axis cs:60,40);  
\end{axis}     
\end{tikzpicture}
}
\end{animateinline}
%\end{center}
\end{document}
share|improve this answer
    
Thanks! The \App approach is not useful when I have a huge amount of lines. But I like the idea and will use it together with other problems! –  Manuel Kuehner May 9 at 16:15
    
Ya, I understand. Just trying to help out. –  Jesse May 9 at 16:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.