22

I have about 200 JPEG files that I need to include in a PDF document, one JPEG per page (i.e. 200 pages + intro slide). The following code successfully does this with one JPEG.

\documentclass{beamer}
\usepackage{graphics}

\title{Overview of Jpegs}
\subtitle{Example}
\author{Chris}
\institute{Office}
\date{February 02, 2012}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

\begin{frame}{Slide with jpeg number 1}.
\includegraphics[height=8cm]{File_1.jpg}
\end{frame}

\end{document}

The JPEGs are named File_1.jpg, File_2.jpg, and so on.

I suppose a for-loop would be in place, but no matter how I try, I can't get it to work.

I created the JPEGs in RStudio (programming in R), but maybe this is more a pure LaTeX-question. I prefer to use JPEG as they are smaller than other formats.

1
  • I really like this question and would be happy to donate some points if it doesn't get enough attention on the first go round.
    – DWin
    Feb 3, 2012 at 18:19

2 Answers 2

23

How about using a chunk with <<results=tex, echo=FALSE>>=?

It will allow you to: (a) paste together the LaTeX code you'd like to run (i.e. 200 \begin{frame} ... \includegraphics ... \end{frame} constructs); and (b) place that constructed text verbatim in the *.tex file produced by Sweave(). For more details, see here. It's a FAQ!

<<results=tex,echo=FALSE>>=  
for(i in 1:200){  
    file <- paste("File_", i, ".jpg", sep="")
    cat("\\begin{frame}{Slide with jpeg number ", i, "}.\n", sep="")
    cat("\\includegraphics[height=8cm]{", file, "}\n", sep="")  
    cat("\\end{frame}\n\n")
}  
@
2
  • 1
    Very glad to hear that, especially since my code block was untested ;). By the way, I wasn't sure if you really want the "." at the end of the \begin{frame} line. It looks out of place to me, but I've also never used the beamer class... If there's a reason for it, would you mind explaining what it is? Thanks! Feb 3, 2012 at 18:37
  • I'm trying to make slides (not document), so beamer-class seems good to use. So far so good. Again, thanks for your quick and good response!
    – Chris
    Feb 6, 2012 at 12:25
37

The pgffor package allows for-loop style repetition using the following format:

\foreach \<cmd> in {1,...,<total>} {%
  % stuff
}

Using this as base, the following is probably what you're after:

\documentclass{beamer}% http://ctan.org/pkg/beamer
\usepackage{graphicx}% http://ctan.org/pkg/graphicx
\usepackage{pgffor}% http://ctan.org/pkg/pgffor

\title{Overview of Jpegs}
\subtitle{Example}
\author{Chris}
\institute{Office}
\date{February 02, 2012}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

\foreach \n in {1,...,200} {%
  \begin{frame}{Slide with jpeg number \n}
    \includegraphics[height=8cm]{File_\n.jpg}
  \end{frame}%
}

\end{document}
3
  • 4
    +1 Thanks for this. For the OP's particular situation, I prefer your solution to my own. (The construct I showed is really most useful when the graphics files to be included are being produced by R on the fly, during the Sweave()ing process). Feb 3, 2012 at 18:55
  • 2
    @JoshO'Brien: Agreed. Using the past tense "created", I assumed the OP was free to use a pure LaTeX solution and therefore didn't require an on-the-fly R solution.
    – Werner
    Feb 3, 2012 at 19:03
  • Thanks to you both. Worked examples of R for beamer applications are greatly appreciated.
    – DWin
    Feb 3, 2012 at 19:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .