在knitr中,size选项在.Rnw
文件中正常工作,以下代码生成:
\documentclass{article}
\begin{document}
<<chunk1, size="huge">>=
summary(mtcars)
@
\end{document}
但是,我不能让它在Rmarkdown中工作。以下代码不会像.rnw
文件中那样更改字体大小。尝试使用opts_chunk$set(size="huge")
设置选项时会发生同样的事情。
这是预期的行为吗?如何更改块代码字体大小? (我的意思是使用knitr选项,而不是在代码之前添加\huge
)
---
title: "Untitled"
output: pdf_document
---
```{r, size="huge"}
summary(mtcars)
```
我使用的是RStudio版本0.98.987,knitr 1.6和rmarkdown 0.2.68。
答案 0 :(得分:20)
提出改变编织钩子的想法,我们可以做到以下几点:
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)
})
此代码段修改默认的块状挂钩。它只是检查块选项大小是否不等于其默认值(normalsize
),如果是,则将options$size
的值预先添加到代码块的输出(包括源!)并附加{ {1}}以便切换回来。
因此,如果你要将\\normalsize
添加到一个块,那么这个块生成的所有输出都将以这种方式打印。
您所要做的就是将此代码段包含在文档的开头。
答案 1 :(得分:11)
每this Gist,您必须使用css定义字体大小:
<style type="text/css">
body, td {
font-size: 14px;
}
code.r{
font-size: 20px;
}
pre {
font-size: 20px
}
</style>
code.r
将控制从代码块回显的R代码的字体大小,而pre
将应用于代码的任何R结果输出。
完整的.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)
```
生成的html呈现为:
答案 2 :(得分:6)
您可以通过从包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)
}
这将定义一个knitr chunk hook size
,它将在块之前放置相应的latex命令,并在块之后放置\normalsize
。
无论如何,通过以下R降价,您可以检查它是否正常工作:
---
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)
```
我从`markdown :: render()得到以下结果:
另请参阅我在github上打开的问题:
答案 3 :(得分:0)
\tiny
```{r}
summary(mtcars)
```
\normalsize
可用的大小按降序排列的选项是:
Huge
,huge
,LARGE
,Large
,large
,normalsize
,small
,footnotesize
,{{ 1}},scriptsize