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

时间:2022-02-20 18:59:44

I am trying get some R code to fit on my beamer slides. It does not seem possible to change the font size via the size argument for the code chunk as you might do for other knitr type documents. The only way seems to be with \footnotesize before every code chunk. This is gets frustrating, as I have lots of code chunks and in many cases I then have to use \normalsize after for my LaTeX bullet points.

我正在尝试获取一些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)
```

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

In my header.txt (below) I have experimented with a couple of bits of code from http://yihui.name/knitr/demo/beamer/ but with no luck.

在我的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

... but really out my depth with \def.

......但是真的超出了我的深度\ def。

1 个解决方案

#1


4  

Drawing on this tex.SE answer, we could redefine the Shaded environment that surrounds R code to make it footnotesize (and the verbatim environment for output). Add this to your header.txt:

借助这个tex.SE答案,我们可以重新定义围绕R代码的着色环境,使其成为脚注(以及输出的逐字环境)。将其添加到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}

#1


4  

Drawing on this tex.SE answer, we could redefine the Shaded environment that surrounds R code to make it footnotesize (and the verbatim environment for output). Add this to your header.txt:

借助这个tex.SE答案,我们可以重新定义围绕R代码的着色环境,使其成为脚注(以及输出的逐字环境)。将其添加到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}