选项1

选项1

我想要一个简单的脚本来将目录中的每个图像“抓取”到单个投影仪演示文稿中,如下所示:

$ ls 

%img1.jpg

%img2.jpg

%img3.jpg


$ *run script*

%输出如下内容:

\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}

答案1

选项1

以下是perl可以在当前目录中使用的脚本:

perl createslides.plx

或者如果你让它可执行并将它添加到你的路径中,那么只需

createslides.plx

您可以在

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

并分别使用1或打开或关闭它们0

创建幻灯片

#!/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

选项 2(首次尝试,效果不如选项 1)

这是一个perl自动执行任务的脚本,您可以将其保存为(例如)createslides.plx

你可以这样调用它:

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

或者如果您有多个扩展,则使用以下命令:

find . -type f \( -name "*.png" -or -name "*.jpg" \) -print0|xargs -0 perl 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

它将创建一个文件slides.tex- 示例输出如下:

\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}

答案2

\multiinclude软件包中的函数xmpmulti也许可以帮你完成这个任务。我可能记错了,但我相信你可以使用命令来完成这个任务\multiinclude[<+>][format=jpg,graphics={width=\textwidth}]{img}。将文件命名为“img-0.jpg”、“img-1.jpg”、“img-2.jpg”……等等。

例子:

\documentclass{beamer}
\usepackage{xmpmulti}

\begin{document}

\begin{frame}{Frame Title Here}
\multiinclude[<+>][format=jpg,graphics={width=\textwidth}]{img}
\end{frame}

\end{document}

在此示例中,图像文件与主文件放在同一个文件夹中。可以在以下位置找到三个可用于此示例的搞笑图片文件https://www.dropbox.com/sh/rtsm7ypcdcxza17/6NySKH_yB8

更多信息\multiinclude请参阅 Beamer 用户指南第 14.1.3 节。该指南应位于 LateX 安装的 ..\doc\latex\beamer\doc 文件夹中。

答案3

Reinhard Kotucha 曾给我发过他编写的一个 perl 脚本,用于处理所有图像文件 (*.jpg、*.png、*.pdf) 并将其放入 pdf 文件中,每页一个图像。它不使用 beamer 包,但它确实对我很有用,有好几次,也许对你来说也是如此。

pdfcat目录

#!/usr/bin/env perl

## pdfcatdir
## Copyright 2006 Reinhard Kotucha <[email protected]>
#
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either version 1.3
# of this license or (at your option) any later version.
# The latest version of this license is in
#   http://www.latex-project.org/lppl.txt
# 
# The current maintainer is Reinhard Kotucha.

my $version="20090305";

print STDERR "This is pdfcatdir, Version $version\n"; 
use Getopt::Long;
$Getopt::Long::autoabbrev=0;
Getopt::Long::Configure ("bundling");

sub usage {
    print <<'EOF'
usage: 
    pdfcatdir [options] <directory1> [<directory2> ...]

    Concatenate all PDF, PNG, or JPEG files in directory <directory>.
    The name of the resuling file will be "<directory>.pdf".

    Options:

        -a | --author
       Add an author entry to the info dictionary.

        -d | --debug
       Do not delete temporary files.

    -f | --fullscreen
       Start browser in fullscreen mode.

    -h | --help
       Display this message.

    -k | --keywords
       Add keywords to the info dictionary.

    -r | --replacefonts
       Replace fonts if possible.

    -s | --subject
       Add a subject entry to the info dictionary.

    -t | --title
       Add a title entry to the info dictionary.

    -v | --verbose
       Verbose output to screen.

    Files:

    pdfcatdir first reads a file "pdfcatdir.info" if it exists.
    Then it reads a file "<directory>.info" if it exists.
    Only data set in "<directory>.info" will overwrite data set 
    in "pdfcatdir.info".  Everything can be overwritten by 
    command-line arguments.

        Syntax:

        <key>: <value>

        <key> is case insensitive.

        Valid keys are Author, Title, Subject, Keywords.
        Everything after "#" or "%" is ignored.

    You can use pdfcatdir to create a slide show from a directory
    which contains images produced by digital cameras.  It is sometimes
    necessary to rotate individual images.  For this purpose pdfcatdir
    looks for a file "<directory>.rotate".

        Syntax:

        <filename>: <rot>

        <filename> is case sensitive.  It shouldn't contain the directory
        name.  <rot> is an integer number which denotes steps of 
        90 degrees.  Positive numbers mean counterclockwise rotation.
        Everything after "#" or "%" is ignored.

EOF
;#'
exit 1;
}


GetOptions 
    "author|a=s",
    "fullscreen|f",
    "help|h",
    "keywords|k=s",
    "replacefonts|r",
    "subject|s=s",
    "title|t=s",
    "debug|d",
    "verbose|v";

usage if $opt_help;
usage if (@ARGV < 1);
$^W=1; ### if $opt_debug;

sub debug {
    my $foo=shift;
    print STDERR "DEBUG: $foo" if $opt_debug;
}


# Read the TeX code below __DATA__ into an array.
# We probably need it more than once.

while (<DATA>) {
    push @saved_texcode, $_;
}

# Read a file "<dirname>.rotate" and create a hash. 

sub get_rotates {
    my $dirname=shift;
    my %rotate=();
    if (-f "$dirname.rotate") {
    open ROTATE, "$dirname.rotate";
    while (<ROTATE>) {
        my ($key, $value);
        s/(#|%).*//g; 
        next if /^\s*$/;
        ($key,$value)=/(.*?):\s*(.*)/;
        $rotate{$key}=((($value+1)%4)-1)*90;
    }
    close ROTATE;
    }
    return %rotate;
}

# Prosess the PDF info files.  

sub eval_info_file {
    my ($key, $value);
    s/(#|%).*//g;
    unless (/^\s*$/) {
    ($key,$value)=/(.*?):\s*(.*)/;
    $key=lc($key);
    $_=$value;
    s/\s+/ /g;
    s/\s*,\s*/, /g;
    $value=$_;
    $info{$key}=$value;
###    print "$key -> $value\n";
    }
}

# These values will appear in the pdfinfo dictionary unless they will
# be overwrirtten by a file "pdfcatdir.info" or "<dirname>.info".

sub set_pdfinfo_defaults {
    %info=();
    $info{"author"}="This file had been produced automatically by pdftex."; 
    $info{"title"}=""; 
    $info{"subject"}=""; 
    $info{"keywords"}=""; 
}

# If a file "pdfcatdir.info" exists, variables set in this file will
# overwrite default values.

sub set_pdfinfo_pdfcatdir {
    if (-f "pdfcatdir.info") {
    open INFO, "pdfcatdir.info" or die "Can't open pdfcatdir.info";
    while (<INFO>) {
        eval_info_file; 
    }
    close INFO;
    }
}

# If a file "<dirname>.info" exists, variables set in this file will
# overwrite default values and variables set in "pdfcatdir.info".

sub set_pdfinfo_dirname {
    my $dirname=shift;
    if (-f "$dirname.info") {
    open DIRINFO, "$dirname.info";
    while (<DIRINFO>) {
        eval_info_file; 
    }
    close DIRINFO;
    }
}

# Every variable in the pdfinfo dictionary can be overwritten by a 
# command-line argument.

sub set_pdfinfo_cli {
    $info{"author"}=($opt_author) if $opt_author;
    $info{"title"}=($opt_title) if $opt_title;
    $info{"subject"}=($opt_subject) if $opt_subject;
    $info{"keywords"}=($opt_keywords) if $opt_keywords;
}

# Determine the final values fir the PDF info dictionary.

sub set_pdfinfo {
    my $dirname=shift;
    set_pdfinfo_defaults;
    set_pdfinfo_pdfcatdir;
    set_pdfinfo_dirname "$dirname";
    set_pdfinfo_cli;
}

# Return a sorted list containing all usable files in a particular
# directory.

sub create_filelist {
    my $dirname=shift;
    my @allfiles=();
    opendir DIR, "$dirname" or die "Can't open directory '$dirname'";
    while ($_=readdir DIR) {
    next if /^\..*/; # skip all dot files
    next unless /\.(jpg|jpeg|pdf|png)$/i;
    next if -d $_; # skip subdirs
    push @allfiles, "$dirname/$_"
    }
    closedir DIR;
    my @sorted_files=sort @allfiles;
    return @sorted_files;
}

# Write a file "<dirname>.files".

sub write_file_list {
    my $dirname=shift;
    my @sorted_files=@_;
    open FILES, ">$dirname.files";
    for $pdf (@sorted_files) {
    ($pdfbasename=$pdf)=~s/.*\///;
    my $angle=0;
    if (exists $rotate{"$pdfbasename"}) {
        $angle=$rotate{"$pdfbasename"};
        debug "Rotate: '$dirname/$pdfbasename' by $angle degrees\n";
    }
    printf FILES "%03d:%s\n", $angle, $pdf;
    }
    close FILES;
}

# Create and process the TeX files.

sub write_tex_file {
    my $dirname=shift;
    debug "Open: \"$dirname.tex\".\n";
    open TEX, ">$dirname.tex" or die "Can't open \"dirname.tex\".\n";
    my $bookmark_mode=
    '\pdfcatalog{/PageMode/UseOutlines/PageLayout/SinglePage}';
    my $fullscreen_mode=
    '\pdfcatalog{/PageMode/FullScreen}';
    set_pdfinfo "$dirname";
    debug "PDF_Author:   $info{author}\n";
    debug "PDF_Title:    '$info{title}'\n";
    debug "PDF_Subject:  '$info{subject}'\n";
    debug "PDF_Keywords: '$info{keywords}'\n";
    my @texcode;
    for (@saved_texcode) {
    push @texcode, $_;
    }
    for (@texcode) {
    last if /__END__/;
    if ($opt_verbose) {
        s/\@interactionmode\@/\\def\\verbose{}\\errorstopmode/;
    } else {
        s/\@interactionmode\@/\\batchmode/;
    }
    if ($opt_replacefonts) {
        s/\@replacefonts\@//;
    } else {
        s/\@replacefonts\@/\\pdfmapfile{}/;
    }
    if ($opt_fullscreen) {
        s/\@pdfcatalog\@/$fullscreen_mode/;
    } else {
        s/\@pdfcatalog\@/$bookmark_mode/;
    }
    s/\@title\@/$info{title}/;
    s/\@author\@/$info{author}/;
    s/\@keywords\@/$info{keywords}/;
    s/\@subject\@/$info{subject}/;
    print TEX;
    }
    print TEX "\\input \"$dirname.files\"\n";
    print TEX '\errorstopmode\end', "\n";
    close TEX;
}

sub run_pdftex {
    my $dirname=shift;
    my @texcommand=qw(pdftex -ini);
    push @texcommand, "$dirname";

    if ($opt_debug) {
    my @commandlist;
    for my $entity (@texcommand) {
        push @commandlist, "[$entity]";
        $texcommand=join " ", @commandlist;
    }
    debug "TeX-command: $texcommand\n";
    }
    system @texcommand;
}

# find directories.

for my $dirname (@ARGV) {
    if ($dirname=~/\/$/) {
    $dirname=substr "$dirname", 0, length ($dirname) -1;
    }
    push @directories, $dirname if (-d $dirname);
} 

## main()

for my $dirname (@directories) {
    debug "Directory: '$dirname'\n";

    @sorted_files=create_filelist "$dirname";
    foreach $file (@sorted_files) {
    debug "File: '$file'\n";
    }
    %rotate=get_rotates "$dirname";
    if (@sorted_files >0) {
    write_file_list "$dirname", @sorted_files;
    write_tex_file "$dirname", @sorted_files;
    run_pdftex "$dirname";
    unlink ("$dirname.tex", "$dirname.log", "$dirname.files")
        unless $opt_debug;
    }
}
print "@texcode\n";
__DATA__
%% This is an automatically generated file. Do not change anything here.

\catcode`\{=1 \catcode`\}=2 \catcode`\#=6 \catcode`^=7 
\catcode`\^^M=13 \let^^M=\par
\pdfoutput=1
\pdfoptionpdfminorversion=4
\pdfcompresslevel=0
\pdfhorigin0pt 
\pdfvorigin0pt
\def\space{ }
\def\linewidth{70}
\endlinechar=-1 \newlinechar=`\^^J
@interactionmode@ 
@replacefonts@
@pdfcatalog@
\pdfinfo {
  /Title      (@title@)
  /Author     (@author@)
  /Subject    (@subject@)
  /Keywords   (@keywords@)
  /Creator    (pdfTeX and pdfcatdir)
}
\def\bar#1{\if#1m=\expandafter\bar\fi}
\edef\bar{\expandafter\bar\romannumeral\number\linewidth 000\relax}


\count0=1
\def\recurse{\pdfximage page \count1 {\dir/\file}
  \edef\Rotate{/Rotate \pdfrotate}
  \expandafter\pdfpageattr\expandafter{\Rotate}
  \ifnum\count1=1 \pdfoutline goto page \number\count0 {/Fit} {\file}
    \ifx\verbose\undefined\errorstopmode
      \message{\file:\space\the\pdflastximagepages}
      \ifnum\pdflastximagepages>1 \message{pages}\else\message{page}\fi
      \ifnum\rotate=0 \else\message{rotated \rotate}\fi
      \batchmode
    \else
      \message{^^J\bar^^J}
      \message{\file:\space\the\pdflastximagepages}
      \ifnum\pdflastximagepages>1 \message{pages}\else\message{page}\fi 
      \ifnum\rotate>0 \message{rotated \rotate}\fi
      \message{\bar^^J^^J}
    \fi
  \fi
  \setbox0\hbox{\pdfrefximage\pdflastximage}
  \pdfpagewidth=\wd0 \pdfpageheight=\ht0 \shipout\box0 
  \advance\count0 by 1 \advance\count1 by 1
  \ifnum\count1=\pdflastximagepages\relax
  \else\expandafter\recurse\fi}

\def\process#1:#2/#3^^M{\count20=#1 
    \edef\rotate{\number\count20 }\multiply\count20by-1
    \edef\pdfrotate{\number\count20 }
    \edef\dir{#2}\edef\file{#3}\count1=1 \recurse\par}

\catcode32=13 \let \space
\everypar{\setbox2\lastbox\process}\endlinechar=`\^^M %
__END__

相关内容