Rmarkdown中带有Knitr和Latex的代码块字体大小 [英] Code chunk font size in Rmarkdown with knitr and latex

查看:201
本文介绍了Rmarkdown中带有Knitr和Latex的代码块字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在knitr中,size选项在.Rnw文件中可以正常工作,将生成以下代码:

In knitr, the size option works fine in a .Rnw file, the following code generates:

\documentclass{article}

\begin{document}

<<chunk1, size="huge">>=
summary(mtcars)
@


\end{document}

但是,我无法在Rmarkdown中使用它.下面的代码不会更改字体大小,就像在.rnw文件中一样.尝试使用opts_chunk$set(size="huge")设置选项时会发生同样的事情.

However, I can't get it to work in Rmarkdown. The following code does not change the font size, as it did in .rnw file. The same thing happens when trying to set options with opts_chunk$set(size="huge").

这是预期的行为吗?如何更改组块代码的字体大小? (我的意思是使用knitr选项,而不是在代码前添加\huge)

Is this the expected behavior? How does one change the chunk code font size? (I mean using knitr options, not by adding \huge before the code)

---
title: "Untitled"
output: pdf_document
---

```{r, size="huge"}
summary(mtcars)
```

我正在使用RStudio版本0.98.987,knitr 1.6和rmarkdown 0.2.68.

I am using RStudio Version 0.98.987, knitr 1.6 and rmarkdown 0.2.68.

推荐答案

提出改变编织钩的想法,我们可以执行以下操作:

Picking up the idea to alter a knitr hook we can do the following:

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

此代码段修改了默认的块挂钩.它只是检查块选项的大小是否不等于其默认值(normalsize),如果是,则将options$size的值添加到代码块的输出(包括源代码!)中,并按顺序附加\\normalsize切换回去.

This snippet modifies the default chunk hook. It simply checks if the chunk option size is not equal to its default (normalsize) and if so, prepends the value of options$size to the output of the code chunk (including the source!) and appends \\normalsize in order to switch back.

因此,如果您将size="tiny"添加到块中,则该块所生成的所有输出都将以这种方式打印.

So if you would add size="tiny" to a chunk, then all the output generated by this chunk will be printed that way.

您要做的就是在文档的开头添加此代码段.

All you have to do is to include this snippet at the beginning of your document.

这篇关于Rmarkdown中带有Knitr和Latex的代码块字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆