Beamer中的代码块字体大小与knitr和latex

gja*_*bel 11 latex r beamer knitr

我正在尝试获取一些R代码以适应我的beamer幻灯片.似乎不可能通过size代码块的参数更改字体大小,就像对其他knitr类型文档一样.唯一的方法似乎是\footnotesize在每个代码块之前.这是令人沮丧的,因为我有很多代码块,在许多情况下,我必须使用\normalsize之后我的LaTeX项目符号点.

---
title: "Untitled"
output:
 beamer_presentation:
  includes:
   in_header: header.txt
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, size = "footnotesize")
```

## R Markdown

```{r}
summary(cars)
```

\footnotesize
```{r}
summary(cars)
```
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在我header.txt(下面)中,我已经尝试了一些来自http://yihui.name/knitr/demo/beamer/的代码,但没有运气.

\ifdefined\knitrout
\renewenvironment{knitrout}{\begin{footnotesize}}{\end{footnotesize}}
\else
\fi

\makeatletter
\let\oldalltt\alltt
\def\alltt{\@ifnextchar[\alltt@i \alltt@ii}
\def\alltt@i[#1]{\oldalltt[#1]\footnotesize}
\def\alltt@ii{\oldalltt\footnotesize}
\makeatother
Run Code Online (Sandbox Code Playgroud)

......但真的超出我的深度\def.

sco*_*coa 6

借鉴这个 tex.SE 答案,我们可以重新定义Shaded围绕R代码的环境以使其具有脚注大小(以及verbatim输出环境)。将此添加到您的 header.txt:

%% change fontsize of R code
\let\oldShaded\Shaded
\let\endoldShaded\endShaded
\renewenvironment{Shaded}{\footnotesize\oldShaded}{\endoldShaded}

%% change fontsize of output
\let\oldverbatim\verbatim
\let\endoldverbatim\endverbatim
\renewenvironment{verbatim}{\footnotesize\oldverbatim}{\endoldverbatim}
Run Code Online (Sandbox Code Playgroud)