简体   繁体   English

使用 knitr 和 latex 在 Beamer 中编码块字体大小

[英]Code chunk font size in Beamer with knitr and latex

I am trying get some R code to fit on my beamer slides.我正在尝试获取一些 R 代码以适合我的投影仪幻灯片。 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.似乎不可能通过代码块的size参数更改字体大小,就像您对其他 knitr 类型文档所做的那样。 The only way seems to be with \\footnotesize before every code chunk.唯一的方法似乎是在每个代码块之前使用\\footnotesize 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.这令人沮丧,因为我有很多代码块,并且在许多情况下,我必须在 LaTeX 要点之后使用\\normalsize

---
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)
```

在此处输入图片说明

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深度了解。

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). 借助这个tex.SE答案 ,我们可以重新定义围绕R代码的Shaded环境,使其成为脚注(以及输出的verbatim环境)。 Add this to your header.txt: 将其添加到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}

Following @Martin Schmelzer's output , you can change code font size and text font size independently and for the whole document by adding this to you rmarkdown file:按照@Martin Schmelzer 的输出,您可以通过将其添加到您的 rmarkdown 文件来独立更改代码字体大小和文本字体大小并更改整个文档

def.chunk.hook  <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
  x <- def.chunk.hook(x, options)
  paste0("\n \\", "footnotesize","\n\n", x, "\n\n \\normalsize")
})

Out of this code snippet, you only have to change the "footnotesize" and "normalsize" parameters to whatever font size you want;在这段代码片段中,您只需将"footnotesize""normalsize"参数更改为您想要的任何字体大小; the first being the code and output font size and the second being the text font size.第一个是代码和输出字体大小,第二个是文本字体大小。

For instance, with code as "tiny" and text as "normalsize":例如,代码为“tiny”,文本为“normalsize”:

---
output: beamer_presentation
---

```{r setup, include=FALSE}
def.chunk.hook  <- knitr::knit_hooks$get("chunk")
knitr::knit_hooks$set(chunk = function(x, options) {
  x <- def.chunk.hook(x, options)
  paste0("\n \\", "tiny","\n\n", x, "\n\n \\normalsize")
})
```

# Section 1
```{r}
summary(cars)
```
Text.

# Section 2
```{r}
summary(cars)
```
This works for every chunks.

Gives this:给出了这个: 例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM