在Rmarkdown使用knitr和latex代码块字体大小。

时间:2021-09-30 06:14:26

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

在knitr中,size选项在.Rnw文件中运行良好,以下代码生成:

\documentclass{article}

\begin{document}

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


\end{document}

在Rmarkdown使用knitr和latex代码块字体大小。

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").

但是,我不能让它在Rmarkdown工作。下面的代码不会改变字体大小,就像在.rnw文件中那样。当尝试使用opts_chunk$set(size="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)

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

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

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

在Rmarkdown使用knitr和latex代码块字体大小。

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

我使用的是RStudio版本0.98.987,knitr 1.6和rmarkdown 0.2.68。

3 个解决方案

#1


11  

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

我们可以采用以下方法来改变knitr钩子的想法:

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("\\", options$size,"\n\n", x, "\n\n \\normalsize"), x)
})

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.

这段代码修改了默认的chunk钩子。它只是检查块选项大小是否不等于它的默认值(normalsize),如果是这样,则将选项$size的值预先设置为代码块的输出(包括源代码!),并将其添加到normalsize以切换回调。

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

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

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

您所要做的就是在文档的开头添加这段代码。

#2


10  

Per this Gist, you have to define the font size using css:

根据这个要点,您必须使用css定义字体大小:

<style type="text/css">
body, td {
   font-size: 14px;
}
code.r{
  font-size: 20px;
}
pre {
  font-size: 20px
}
</style>

code.r will control the font size for R code echoed from the code chunk, while pre will apply to any R results output from the code.

代码。r将控制从代码块发出的r代码的字体大小,而pre将应用于代码的任何r结果输出。

A complete working .Rmd file might look like:

一个完整的工作。rmd文件可能是:

---
title: "FontTest"
author: "Thomas Hopper"
date: "January 13,2016"
output: html_document
---

<style type="text/css">

body, td {
   font-size: 14px;
}
code.r{
  font-size: 20px;
}
pre {
  font-size: 20px
}
</style>

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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

The resulting html renders as:

生成的html显示为:

在Rmarkdown使用knitr和latex代码块字体大小。

#3


5  

You can define you own document format by exporting something based on the following function from your package my_package:

您可以通过从包my_package导出以下函数来定义自己的文档格式:

my_report <- function(...) {

  fmt <- rmarkdown::pdf_document(...)

  fmt$knitr$knit_hooks$size = function(before, options, envir) {
    if (before) return(paste0("\n \\", options$size, "\n\n"))
    else return("\n\n \\normalsize \n")
  }

  return(fmt)
}

This will define a knitr chunk hook size that will put the appropriate latex command before the chunk, and \normalsize after the chunk.

这将定义一个knitr块钩子大小,它将在数据块之前放置适当的latex命令,并且在数据块后将其正常大小。

Anyway, with the following R markdown you can check if it's working:

不管怎样,用下面的R markdown,你可以检查它是否工作:

---
output: my_package::my_report
---

Test text for comparison

```{r}
print(1)
```
The next code chunk has `size = 'tiny'` in the chunk options.

```{r, size = 'tiny'}
print(1)
```

I get the following result from `markdown::render():

我从“markdown::render()”中得到以下结果:

在Rmarkdown使用knitr和latex代码块字体大小。

See also the issue I opened on github:

还有我在github上打开的问题:

https://github.com/yihui/knitr/issues/1296

https://github.com/yihui/knitr/issues/1296

#1


11  

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

我们可以采用以下方法来改变knitr钩子的想法:

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("\\", options$size,"\n\n", x, "\n\n \\normalsize"), x)
})

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.

这段代码修改了默认的chunk钩子。它只是检查块选项大小是否不等于它的默认值(normalsize),如果是这样,则将选项$size的值预先设置为代码块的输出(包括源代码!),并将其添加到normalsize以切换回调。

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

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

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

您所要做的就是在文档的开头添加这段代码。

#2


10  

Per this Gist, you have to define the font size using css:

根据这个要点,您必须使用css定义字体大小:

<style type="text/css">
body, td {
   font-size: 14px;
}
code.r{
  font-size: 20px;
}
pre {
  font-size: 20px
}
</style>

code.r will control the font size for R code echoed from the code chunk, while pre will apply to any R results output from the code.

代码。r将控制从代码块发出的r代码的字体大小,而pre将应用于代码的任何r结果输出。

A complete working .Rmd file might look like:

一个完整的工作。rmd文件可能是:

---
title: "FontTest"
author: "Thomas Hopper"
date: "January 13,2016"
output: html_document
---

<style type="text/css">

body, td {
   font-size: 14px;
}
code.r{
  font-size: 20px;
}
pre {
  font-size: 20px
}
</style>

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

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

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

The resulting html renders as:

生成的html显示为:

在Rmarkdown使用knitr和latex代码块字体大小。

#3


5  

You can define you own document format by exporting something based on the following function from your package my_package:

您可以通过从包my_package导出以下函数来定义自己的文档格式:

my_report <- function(...) {

  fmt <- rmarkdown::pdf_document(...)

  fmt$knitr$knit_hooks$size = function(before, options, envir) {
    if (before) return(paste0("\n \\", options$size, "\n\n"))
    else return("\n\n \\normalsize \n")
  }

  return(fmt)
}

This will define a knitr chunk hook size that will put the appropriate latex command before the chunk, and \normalsize after the chunk.

这将定义一个knitr块钩子大小,它将在数据块之前放置适当的latex命令,并且在数据块后将其正常大小。

Anyway, with the following R markdown you can check if it's working:

不管怎样,用下面的R markdown,你可以检查它是否工作:

---
output: my_package::my_report
---

Test text for comparison

```{r}
print(1)
```
The next code chunk has `size = 'tiny'` in the chunk options.

```{r, size = 'tiny'}
print(1)
```

I get the following result from `markdown::render():

我从“markdown::render()”中得到以下结果:

在Rmarkdown使用knitr和latex代码块字体大小。

See also the issue I opened on github:

还有我在github上打开的问题:

https://github.com/yihui/knitr/issues/1296

https://github.com/yihui/knitr/issues/1296