根据文章自动生成 Beamer 演示文稿草稿

根据文章自动生成 Beamer 演示文稿草稿

我有一篇包含多个章节、小节和图表的 LaTeX 论文。我用来展示这篇论文的幻灯片大部分都包含相同的章节、小节和图表。因此,我有兴趣自动生成具有以下功能的演示文稿草稿的 Beamer 代码:

  • 论文的章节和小节都保存在幻灯片中。
  • 当论文的某个小节中引用某个图表时,该图表将插入到相应小节的幻灯片中。

我想知道是否已经存在类似的东西。否则,编写一个脚本来解析论文并生成 Beamer 草稿将相当简单。

为了进一步阐明我的问题,我举了以下例子:

纸:

\documentclass{article}
\usepackage[affil-it]{authblk}

\author[1]{Phill}
\author[1]{Bill}
\affil[1]{Instutite of LaTeX}
\title{Paper 1}
\begin{document}
\section{sec1}
\subsection{sec1.1}

See the data in \ref{fig:1}.

\section*{Figures}
\begin{figure}
    \includegraphics[scale = 0.5]{example-image}
    \label{fig:1}
\end{figure}
\end{document}

结果幻灯片:

\documentclass{beamer}
\title{Paper 1}
\author{Phill and Bill}
\Institute{Instutite of LaTeX}
\begin{document}
\begin{frame}
 \titlepage
\end{frame}
\section{sec1}
\subsection{sec1.1}
\begin{frame}{\secname : \subsecname}
\begin{figure}
    \includegraphics[scale = 0.5]{example-image}
    \label{fig:1}
\end{figure}
\end{frame}

\end{document}

答案1

我继续编写了一个 Python 脚本来解决这个问题。本质上,Beamer 演示文稿将保留文章的格式(部分子部分),并为每个图形创建一个新幻灯片,使图形适合整个幻灯片。

要使用它,您只需在与文章文件相同的目录中运行它即可,该文件应命名main.tex为。输出演示文稿将写入slides.tex

import re
import os

def main():
    """
    Automatically generates a beamer presentation draft from an article
    Notes:
           - Some functionality has been disabled by comments.
    """

    article_file_path = 'main.tex'
    beamer_file_path = 'slides.tex'

    written_figures = []

    with open(article_file_path, 'r') as art:
        with open(beamer_file_path, 'w') as outf:
           # in_frame = False
            for line in art:

                # Preamble
                #(currently ignoring)

                # Copy header
                # Ignores unnumbered sections
                if line.startswith('\section{') or line.startswith(r'\subsection'):

                 #   if in_frame:
                  #      outf.write(r'\end{frame}'+'\n')
                   #     in_frame = False

                    outf.write(safe_line(line))

                if line.startswith(r'\subsubsection'):
                    outf.write('%' + safe_line(line))

                    # Create frame for subsections
               #     if line.startswith(r'\subsection'):
                #        outf.write(r'\begin{frame}{\secname: \subsecname}'+'\n')
                 #       in_frame = True

                # Insert figure
                fig_refs = re.findall(r'\\ref{fig:([\w\d -_]+)}', line)
                if fig_refs:
                    for fig_ref in fig_refs:
                        if fig_ref not in written_figures:
                            fig_frame = safe_line(find_fig_str(article_file_path, fig_ref))
                            outf.write(fig_frame)
                            written_figures.append(fig_ref)

            #if in_frame:
             #   outf.write(r'\end{frame}' + '\n')
              #  in_frame = False

def safe_line(line):
    """ Processes latex control sequences which cause beamer to break
     * Remove \label{}, \todo{}
     * Add \protect infront of \textit{}
    """
    sline = line
    sline = re.sub(r'\\label{[\w\d:\s]+}', '', sline)
    sline = re.sub(r'\\todo{[\w\d:\s]+}', '', sline)

    sline = re.sub(r'\\textit{', r'\\protect\\textit{', sline)
    return sline

def find_fig_str(text_file_path, fig_ref):
    """ Finds the figure floating environment based on label
    """
    with open(text_file_path, 'r') as tfile:
        envstr = ''
        in_fig = False
        for line in tfile:

            if line.startswith(r"\begin{figure}"):
                in_fig = True

            if in_fig:
                to_write = line
                # Remove placement specifications:
                if line.startswith(r"\begin{figure}"):
                    to_write = re.sub(r'\[([\w]+)\]', '', line)
                # ignore label
                if line.startswith(r'\label{'):
                    to_write = ''
                # replace figure width to make it fill the whole slide
                if re.match(r'[\t\s]?\\includegraphics',line):
                    to_write = re.sub(r'\[([\w=\d]+)\]', r'[width =\\textwidth, height = 0.6\\textheight, keepaspectratio]', line)

                envstr += to_write

            labelmatch = re.match(r'[\t\s]?\\label{fig:([\w\d -_]+)}',line)
            if labelmatch:
                if labelmatch.group(1) == fig_ref:
                    right_fig = True
                else:
                    right_fig = False

            if line.startswith(r"\end{figure}"):
                in_fig = False
                if right_fig: # Stop searching
                    break
                else:
                    envstr = ''

    # Make figure be in its own frame:
    if envstr:
        envstr = r'\begin{frame}{\subsecname}' + '\n' + envstr + r'\end{frame}' + '\n'

    return envstr

main()

答案2

就我个人而言,我更喜欢pdfscreen和。 texpowerbeamer

\documentclass{article}
\usepackage[affil-it]{authblk}
\usepackage{xspace,xcolor}
\usepackage{graphics}
\usepackage[screen,nopanel,orange]{pdfscreen}
\hypersetup{pdftoolbar=true}
\usepackage[display]{texpower}

\margins{0.25in}{0.25in}{0.25in}{0.25in}
\screensize{4.5in}{6in}% 4.5in by 8in for widescreen
\backgroundcolor{blue}
\color{white}

\author[1]{Phill}
\author[1]{Bill}
\affil[1]{Instutite of LaTeX}
\title{Paper 1}
\begin{document}
\maketitle
\section{sec1}
\subsection{sec1.1}

See the data in \ref{fig:1}.

\section*{Figures}
\begin{figure}
    \includegraphics[scale = 0.5]{example-image}
    \caption{No caption, No figure number, No label}
    \label{fig:1}
\end{figure}
\end{document}

相关内容