# Animate dynamically generated graphs in PDF using LateX

I am new to the board and I am hoping the good folks here can help me as I have been looking all over the internet but not been able to find a solution to my particular problem. However I'm relatively new to Latex so it could be I'm not asking the correct question in my searches.

Let me start by providing some context of what I'm trying to do. I am trying to create animated graphs that will be dynamically generated when the document is created. The graphs will be different from one another so it's not simply updating the same graph with new information (I have found information on this). I am running Latex/knitr within R to generate the overall document as well as embed the graphs into a PDF document. I am having R do all the number crunching and using ggplot2 to produce several bar graphs (all different from one another). The document will already be 150-200 pages long so I'd like to save real estate by having all related graphs on one page and use the animate package to allow users to click/cycle through the graphs in place.

The following code is successfully generating the graphs and saving them in my figures folder as PDFs however the document is failing to generate and gives me the error message: "Package animate Error: Contents of first frame must not have zero width". I get the same message for height as well.

\documentclass{article}
\usepackage{eso-pic}
\usepackage{helvet}
\usepackage{xcolor,colortbl}
\usepackage{booktabs}
\usepackage{wallpaper}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{makecell}
\usepackage{animate}
\usepackage{graphicx}
\usepackage{media9}

\begin{document}
\begin{animateinline}[controls]{1}

<<echo=TRUE,warning=FALSE,include=FALSE,fig.show="asis",fig.width=5,fig.height=5>>=

for(list_iter in 1: length(output_list)){

figure_data <- output_list[[list_iter]]
if(list_iter!=1){
data_vector <- 7

knit_child('bar_plot.Rnw')

if(list_iter<length(output_list)){
cat("\\ \\newframe")
}
}
}

@

\end{animateinline}

\end{document}


The bar_plot.rnw is doing exactly what you would expect, generating the plots from the data given and then returning the graph. Again, this works as the individual PDFs are being generated. I just can't seem to get them to populate in the document.

I greatly appreciate any help/insights you could provide. If I have not provided enough information please let know.

• A warm welcome to TeX.SE! Great first question! – A Feldman Apr 7 '16 at 14:46
• Can you make this compilable? There are a couple of ways you might do this. One would be to provide a couple of minimal files to create plots such that the process you're following could be reproduced. Better, if possible, would be to find a way to reproduce the basic problem using standard images, say. (E.g. example-image-a, example-image-b, example-image-c.) I don't know whether the second is possible, which is why I've also mentioned the first. (The second is better because people wouldn't need R etc. to reproduce.) – cfr Apr 7 '16 at 22:28
• Thanks for your reply @cfr. A potential solution occurred to me last night that I will be trying today. If it happens to work I'll certainly post the code back here for the community. If it fails I will update my original question/post with a compilable example as requested. – EagleStat Apr 8 '16 at 12:56

Alright, well after 2.5 days searching the internet apparently all it took was a post here to get my brain on track. I was finally successful in getting the dynamically created charts included in the document. The issue was I was failing to properly tell the code to include the graph within the Latex environment. The following code now works:

\documentclass{article}
\usepackage{eso-pic}
\usepackage{helvet}
\usepackage{xcolor,colortbl}
\usepackage{booktabs}
\usepackage{wallpaper}
\usepackage{fancyhdr}
\usepackage{array}
\usepackage{makecell}
\usepackage{animate}
\usepackage{graphicx}
\usepackage{media9}

\begin{document}

\begin{animateinline}[controls]{1}

<<echo=FALSE,warning=FALSE,include=FALSE>>=

out <- ""
for(list_iter in 1: length(output_list)){

figure_data <- output_list[[list_iter]]
if(list_iter!=1){
data_vector <- 7
out = c(out, knit_expand(file='bar_plot.Rnw'))
}
}

@
\Sexpr{paste(out, collapse = '\n')}

\end{animateinline}

\end{document}


For those not familiar with R, what this is doing is building a vector of graphs in the "out" variable. What I was missing previously was the "\Sexpr" command that let's Latex know that the "out" variable from knitr chunk needed to be included in the document. I had previously tried "print(out)" and "cat(out)" to accomplish this but those did not work. Also you'll notice that the "\new frame" command in my original post is now gone. I moved this into the bar_plot.rnw file, so it's still in use (as it needs to be) but simply being applied in a different part of the code.