[Tex/LaTex] Automatic beamer presentation with one slide generated for each image in a directory

beamerpresentations

I would like a simple script to "scrape" every image in a directory into a single beamer presentation as follows:

$ ls 

%img1.jpg

%img2.jpg

%img3.jpg


$ *run script*

%OUTPUT SOMETHING LIKE THIS:

\documentclass{beamer}

% for themes, etc.
\mode<presentation>
{ \usetheme{boxes} }

\usepackage{times}  % fonts are up to you
\usepackage{graphicx}

\section{Images in this Directory}

\begin{frame}
\frametitle{img1.jpg}
\begin{center}
        \includegraphics[width=4in]{img1.jpg}
\end{center}
\end{frame}

\begin{frame}
\frametitle{img2.jpg}
\begin{center}
        \includegraphics[width=4in]{img2.jpg}
\end{center}
\end{frame}


\begin{frame}
\frametitle{img3.jpg}
\begin{center}
        \includegraphics[width=4in]{img3.jpg}
\end{center}
\end{frame}

\end{document}

Best Answer

Option 1

Here's a perl script that can be used in the current directory:

perl createslides.plx

or if you make it executable and add it to your path, then simply

createslides.plx

You can specify the image extensions in

my %imgextensions=("png"=>1,"jpg"=>1);

and turn them on and off with a 1 or 0 respectively

createslides.plx

#!/usr/bin/perl

use strict;
use warnings;

# list extensions to work with
my %imgextensions=("png"=>1,"jpg"=>1);

# open the current directory
my $dir = './';
opendir(DIR, $dir) or die $!;

my @lines=();               # @lines: stores lines for beamer
my $extension ='';

# setup the documentclass and preamble
push(@lines,"\\documentclass{beamer}\n");
push(@lines,"% for themes, etc.\n");
push(@lines,"\\mode<presentation>\n");
push(@lines,"{ \\usetheme{boxes} }\n");
push(@lines,"\\usepackage{graphicx}\n");
push(@lines,"\\begin{document}\n");
push(@lines,"\\section{Images in this Directory}\n");

# loop through filenames
while (my $filename = readdir(DIR))
{
   # get the file extension
   $filename =~ m/\.(.*)$/;
   $extension=$1;
   if(scalar($imgextensions{$extension}))
   {
        push(@lines,"\\begin{frame}\n");
        push(@lines,"\\frametitle{$filename}\n");
        push(@lines,"\\begin{center}\n");
        push(@lines,"\\includegraphics[width=4in]{$filename}\n");
        push(@lines,"\\end{center}\n");
        push(@lines,"\\end{frame}\n");
    }
}

# close directory
closedir(DIR);

# end the documentclass
push(@lines,"\\end{document}\n");
print(@lines);

# create slides.tex
open (MYFILE, '>slides.tex');
print MYFILE @lines;
close (MYFILE); 

exit

Option 2 (first attempt, not as good as option 1)

Here's a perl script to automate the task, which you can save as (for example) createslides.plx

You can call it with:

find . -type f \( -name "*.jpg" \) -print0|xargs -0 perl createslides.plx 

or else with the following if you have multiple extensions:

find . -type f \( -name "*.png" -or -name "*.jpg" \) -print0|xargs -0 perl createslides.plx 

createslides.plx

#!/usr/bin/perl

use strict;
use warnings;

my $filename='';
my @lines=();               # @lines: stores lines for beamer

# setup the documentclass and preamble
push(@lines,"\\documentclass{beamer}\n");
push(@lines,"% for themes, etc.\n");
push(@lines,"\\mode<presentation>\n");
push(@lines,"{ \\usetheme{boxes} }\n");
push(@lines,"\\usepackage{graphicx}\n");
push(@lines,"\\begin{document}\n");
push(@lines,"\\section{Images in this Directory}\n");

# loop through filenames
while (@ARGV)
{
    # get filename from arguments
    $filename = shift @ARGV; 

    push(@lines,"\\begin{frame}\n");
    push(@lines,"\\frametitle{$filename}\n");
    push(@lines,"\\begin{center}\n");
    push(@lines,"\\includegraphics[width=4in]{$filename}\n");
    push(@lines,"\\end{center}\n");
    push(@lines,"\\end{frame}\n");
}

# end the documentclass
push(@lines,"\\end{document}\n");
print(@lines);

# create slides.tex
open (MYFILE, '>slides.tex');
print MYFILE @lines;
close (MYFILE); 

exit

It will create a file slides.tex- a sample output is included below:

\documentclass{beamer}
% for themes, etc.
\mode<presentation>
{ \usetheme{boxes} }
\usepackage{graphicx}
\begin{document}
\section{Images in this Directory}
\begin{frame}
\frametitle{./Tux.jpg}
\begin{center}
\includegraphics[width=4in]{./Tux.jpg}
\end{center}
\end{frame}
\end{document}