【问题标题】:Remove additional blank slides after ToC and insert black slides in rmarkdown::beamer_presentation with custom beamer theme在 ToC 之后删除额外的空白幻灯片,并在 rmarkdown::beamer_presentation 中插入带有自定义投影仪主题的黑色幻灯片
【发布时间】:2021-05-21 07:49:50
【问题描述】:

我在rmarkdown::beamer_presentation 中使用自定义LaTex beamer theme。 根据这些 SO 答案(LaTex theme、colon、theme path),我使用了 YAML 标头和 beamerthemeTHEMENAME.sty 的一些修改。 这些 LaTex hack 是在 rmarkdown::beamer_presentation 中顺利应用 LaTex Beamer theme 所必需的。

然而,目前,LaTex 会在目录 (ToC) 幻灯片之前和之后插入一张空白幻灯片(参见下面的图 1)。

  • 如何摆脱这些幻灯片?
  • 相反,我最好在 ToC 之前和演示期间的其他情况下使用全黑幻灯片(即,在其间短暂中断和“关闭”演示)

注意:如果我使用选项 toc: true 生成 ToC(而不是使用 beamerouterthemeTHEMENAME.sty 中定义的 \tocframe),markdown 会打乱 ToC 的垂直对齐方式,并且没有任何标题(见下图 2)。

图 1:使用 LaTex 模板中的 Tocframe 生成 ToC(YAML 标头中的toc:false)

图2:在YAML header中使用toc:true生成ToC

MWE.Rmd

---
# COMMENT out "title" in YAML header: else markdown generates a second title slide
# ==> if title contains no special characters: feed it straight into LaTex at the end of the YAML header
# ==> if title contains special characters, like ":", feed it in "preamble.tex" sourced in "LaTex Hacks"
subtitle: "Beamer presentation with R-markdown"
institute: "some place"
date: "`r format(Sys.time(), '%B %d, %Y')`"
output:
  # beamer_presentation: default
  bookdown::pdf_book:
    base_format: rmarkdown::beamer_presentation
    # Source below in "LaTex Hacks", if want theme to be stored in subfolder (else rmarkdown does not find it)
    # theme: "THEMENAME"
    # Source "includes" below in "LaTex Hacks" if using custom theme with custom title page
    # => else, markdown places the includes at inadequate position in LaTex file, which then either does not show the title or throws an error
    # includes: ...
    latex_engine: xelatex
    toc: false # use LaTex TOC (else vertical alignment is messed up and not headline)
    slide_level: 2
classoption: aspectratio=169 # fix aspect ratio of presentation (169 => 16:9, 149 => 14:9, default: 4:3)
#
# LaTex Hacks
# --------------------------
compact-title: false # to remove markdown generated title frame
header-includes:
  # - \title{Title if no special characters} 
  - \input{beamer_files/beamerthemeTHEMENAME.sty}
  - \input{beamer_files/preamble}       # feed title to LaTex in preamble.tex due to ":"
  - \def\titlefigure{img/my_bg}
  - \AtBeginDocument{\titleframe} # add title frame defined in beamerouterthemeTHEMENAME
  - \makeatletter\beamer@ignorenonframefalse\makeatother
  ---


​```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
​```

<!-- TOC - Table of Contents -->
<!-- ======================================================== -->

``` {=latex}
\end{frame}
\tocframe
\begin{frame}
```

## Slide with Bullets
<!-- ======================================================== -->

- Bullet 1
- Bullet 2
- Bullet 3

<!-- Appendix -->
<!-- ======================================================== -->
``` {=latex}
\end{frame}
\appendix
\begin{frame}
```

beamerthemeTHEMENAME.sty

\mode<presentation>

% Tizk, textpos, graphics
\RequirePackage{tikz}
\RequirePackage{textpos}
\RequirePackage{booktabs}

% Beamer settings
\input{beamer_files/beamercolorthemeTHEMENAME.sty}
\input{beamer_files/beamerouterthemeTHEMENAME.sty}

\setbeamertemplate{navigation symbols}{}

\mode<all>

beamerouterthemeTHEMENAME.sty

\mode<presentation>

% Redefine \insertshortinstitute to allow multiple lines
\makeatletter
\renewcommand{\insertshortinstitute}{\beamer@shortinstitute}
\makeatother

% Title frame
\def\titlefigure{img/my_bg}
\newlength\titleboxwidth
\setlength{\titleboxwidth}{0.43\textwidth}
\newcommand{\titleframe}{%
    {
        \setbeamertemplate{background}{
            \begin{tikzpicture}
            \useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
            \ifx\titlefigure\empty
                \fill[titlebgcolor] (6.3mm,6.4mm) rectangle (\the\paperwidth-8.3mm,\the\paperheight-13mm);
            \else
                \node at (current page.center) [anchor=center,yshift=-3.5mm] {\includegraphics[width=\the\paperwidth-19mm]{\titlefigure}};
            \fi
            \node at (current page.north east) [anchor=base east, xshift=-8.3mm, yshift=-6.3mm, align=left, inner sep=0mm, font=\fontsize{5.5}{6.6}\selectfont] {
                \insertshortinstitute
            }; 
            \node at (current page.north west) [anchor=south west, inner sep=0mm, xshift=8.3mm, yshift=-8.6mm] {
                \includegraphics[height=3.8mm]{img/my_logo}
            }; 
            \end{tikzpicture}
        }
        \setbeamertemplate{footline}{}
        \begin{frame}[noframenumbering]
            \begin{tikzpicture}
                \useasboundingbox (0,0) rectangle(\the\paperwidth,\the\paperheight);
                \node [
                    shift={(-11.5mm,-1.8mm)},
                    fill=titlefgcolor!50!white,
                    minimum width=0.46\paperwidth,
                    minimum height=18mm,
                    anchor=west,
                    inner sep=4mm
                    ] at (current page.west) {
                        \hspace*{9mm}
                        \begin{minipage}{\titleboxwidth}
                            \raggedright
                            \usebeamerfont{title}\usebeamercolor[fg]{title}\inserttitle\par
                            \usebeamerfont{author}\usebeamercolor[fg]{author}\insertauthor\par
                            \usebeamerfont{date}\usebeamercolor[fg]{date}\insertdate
                        \end{minipage}          
                    };
            \end{tikzpicture}
        \end{frame}
    }
}

% TOC frame
\newcommand{\tocframe}{%
    \begin{frame}
      \frametitle{Outline}
      \tableofcontents
    \end{frame}
}

% Section title frame
\AtBeginSection[]
{
    \begin{frame}
      \frametitle{Outline}
      \large
      \tableofcontents[currentsection] 
    \end{frame}
}

% Frame title
\setbeamertemplate{frametitle}{
    \vspace{2mm}
    \insertframetitle \\
    \usebeamerfont{framesubtitle}\insertframesubtitle
    \vspace{2.5mm}
}

% Footline
\setbeamertemplate{footline}{
    \leavevmode%
    \hyperlink{toc---table-of-contents}{\includegraphics[width=12mm,trim=0mm 0.4mm 0mm 0mm]{img/my_logo.png}}   
    \hfill
    \hyperlinkappendixstart{\insertframenumber/\inserttotalframenumber}
    \vspace{3mm}
}

\mode<all>

preamble.tex

% "title" is commented out in YAML header: else markdown generates a second title slide
% if title contains no special characters: feed it straight into LaTex at the end of the YAML header
% if title contains special characters, like ":", feed it to LaTex here:
\title[short version]{First line of the title:\\ second line of the title}

【问题讨论】:

    标签: r latex r-markdown markdown beamer


    【解决方案1】:
    • 额外的框架是由 Markdown 插入的,因为它会自动启动不必要的框架,即使实际上没有任何内容打印到框架。您可以通过在 \titleframe 之后放置 \tocframe 来避免标题和目录框架之间的空框架。

    • 对于黑框,你可以定义一个新的宏,例如

      \newcommand{\blackframe}{{\setbeamercolor{background canvas}{bg=black}\begin{frame}[plain]\end{frame}}}
      

    ---
    subtitle: "Beamer presnetation with R-markdown"
    institute: "some place"
    date: "`r format(Sys.time(), '%B %d, %Y')`"
    author: "Donald Duck"
    output:
      # beamer_presentation: default
      bookdown::pdf_book:
        base_format: rmarkdown::beamer_presentation
        # includes:
        #   in_header: preamble.tex
        theme: "THEMENAME"
        latex_engine: xelatex
        toc: false
        slide_level: 2
        keep_tex: true 
    header-includes:
      - \newcommand{\blackframe}{{\setbeamercolor{background canvas}{bg=black}\begin{frame}[plain]\end{frame}}}
      - \AtBeginDocument{\title{MWE}\titleframe\blackframe\tocframe}    
      - \AtEndDocument{\begin{closingframe}lalala\end{closingframe}}
      - \makeatletter\beamer@ignorenonframefalse\makeatother
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    ```
    
    # section
    
    ## Slide with Bullets
    <!-- ======================================================== -->
    
    - Bullet 1
    - Bullet 2
    - Bullet 3
    
    ``` {=latex}
    \end{frame}
    \blackframe
    \begin{frame}
    ```
    
    <!-- Appendix -->
    <!-- ======================================================== -->
    ``` {=latex}
    \end{frame}
    \appendix
    \begin{frame}
    ```
    

    【讨论】:

    • 非常感谢您的意见!如果我复制您的代码,我会遇到几个困难: 1. ToC 幻灯片上的数字显示的是 1/1 而不是 1/N; 2. ToC 幻灯片上页脚和标题的垂直对齐方式与后续幻灯片上的不同(如果我使用 ```` {=latex} \end{frame} \tocframe \begin{frame} ` ``` 包括 ToC); 3.黑框出乎意料地变白了——别问我为什么…… (另外,如果我尝试在 theme/preamble.tex 中定义宏,LaTex 会引发错误。)
    • 对齐已关闭,因为 beamer 仅在文档​​开头计算所需的头部和脚部高度,但此时 rmarkdown 烦人地已经开始了一个框架,并且为时已晚注入您的自定义标题框架在那里。
    • 感谢您的意见!如果我使用## Outline {.unnumbered} \tableofcontents,对齐效果很好,正如您建议的here,所以我坚持这种方法并放弃\tocframe。检查了整个主题。不知道为什么页面是白色的。至少它是一个空白页......
    • 使用如下定义的- \AtBeginDocument{\titleframe\begin{blackframe}\end{blackframe}}会创建一个黑框,但它还没有覆盖整个页面:\newenvironment{blackframe}{ \setbeamertemplate{background}{ \begin{tikzpicture} \fill[black] (0mm,0mm) rectangle (\the\paperwidth,\the\paperheight); \end{tikzpicture} } \setbeamertemplate{footline}{} \begin{frame}[noframenumbering] }% { \end{frame} }
    • @mavericks (current page.south west) rectangle (current page.north east),但如果看到一个不会产生黑框的可重现代码示例会更有趣
    猜你喜欢
    • 2021-05-21
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    相关资源
    最近更新 更多