【问题标题】:How to change the font used inside the chunks when knitting as tuffte_handout pdf?编织为 tuffte_handout pdf 时如何更改块内使用的字体?
【发布时间】:2019-10-08 09:52:31
【问题描述】:

我正在写一本书,使用 R 和 Tufte 库。 我为每个 .Rmd 章节使用以下 YAML:

---
title: "Chapter 2: A First Linear Program"
header-includes:
 \usepackage{longtable}
 \usepackage{caption}
output:
  tufte::tufte_handout:
    citation_package: natbib
    latex_engine: xelatex
  highlight: monochrome
  tufte::tufte_html: default
  tufte::tufte_book:
    citation_package: natbib
    latex_engine: xelatex
---

\pagestyle{headings}


```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```

以下是索引.Rmd文件的YAML:

---
title: "Operations Research Using R<br />"
author: "Timothy R. Anderson"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: ["Master4Bookdowns.bib"]
---

# Preface {-}

我尝试了不同的选项,例如在 YAML 中添加 sansfont 和 mainfont:

output:
  tufte::tufte_handout:
    citation_package: natbib
    latex_engine: xelatex
  highlight: monochrome
  sansfont: Calibri Light
  mainfont: Calibri Light

但它并没有改变块内的字体,只改变了外面的文本。


这是一个更简单的示例文档,显示了我尝试按照 cmets 中的建议更改 monofont

---
title: "Chapter 2: A First Linear Program"
header-includes:
\usepackage{longtable}
 \usepackage{caption}
output:
  tufte::tufte_handout:
    citation_package: natbib
    latex_engine: xelatex
    toc: TRUE
    number_sections: true
  highlight: monochrome
  monofont: Times New Roman
  tufte::tufte_html: default
  tufte::tufte_book:
    citation_package: natbib
    latex_engine: xelatex
---

\pagestyle{headings}


```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```

This is an example, where we can see that text in the book is affected by the use of command
monofont: Times New Roman in the YAML. However, we can see that next chunk, the oucome when generating the tuffte_handout pdf is using a different font and also using multiple colors (no only black as in the main body).


```{r base_case_no_pipes_step_4}

model0d <- c("example", 6 + 2 <= 2000) 
#fabrication
model0e <- c("example", 8 + 6 <= 2000)  
#assembly    
```

【问题讨论】:

  • 请适当压缩并重新格式化您的问题。
  • 我减少了一些多余的段落。它可能看起来仍然有点长,但我想展示我已经完成的工作。如果它看起来仍然太长,请告诉我。谢谢
  • 您可以尝试更改monofont
  • 我也尝试过使用monofont,但它也只影响书中的主要字体,而不影响块内的字体。
  • @monzarrate 我对monofont 更改文档的主字体感到非常惊讶。如果没有真正的minimal reproducible example,我就不可能得到进一步的帮助。

标签: r latex r-markdown tufte


【解决方案1】:

您必须将 YAML 标头带到正确的级别:

  • monofont 等。是顶级标题
  • highlight 低于输出格式,而不低于output

以下示例使用“Times New Roman”作为代码块,使用 XeLaTeX 的默认字体(Latin Modern)作为正文。您可以使用mainfont 更改它。另外,没有语法高亮:

---
title: "Chapter 2: A First Linear Program"
header-includes:
- \usepackage{longtable}
- \usepackage{caption}
monofont: Times New Roman
output:
  tufte::tufte_handout:
    citation_package: natbib
    latex_engine: xelatex
    toc: TRUE
    number_sections: true
    highlight: monochrome
  tufte::tufte_html: default
  tufte::tufte_book:
    citation_package: natbib
    latex_engine: xelatex
---

\pagestyle{headings}


```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```

This is an example, where we can see that text in the book is affected by the use of command
monofont: Times New Roman in the YAML. However, we can see that next chunk, the oucome when generating the tuffte_handout pdf is using a different font and also using multiple colors (no only black as in the main body).


```{r base_case_no_pipes_step_4}

model0d <- c("example", 6 + 2 <= 2000) 
#fabrication
model0e <- c("example", 8 + 6 <= 2000)  
#assembly

```

结果:

【讨论】:

  • 谢谢!在遵循您的建议后,当我在单个章节中使用 tufte_handout 输出时效果很好。另一方面,我想我应该提出一个新问题,因为无论出于何种原因,当我结合整本书(它有 9 章)并使用 bookdown::bookdown_tuftebook2 编织它时,颜色仍然存在。
  • @monzarrate 你在output.bookdown::bookdown_tuftebook2 部分有highlight: monochrome 吗?顺便说一句,在 SO 上说“谢谢”的传统方式是接受/赞成答案。
  • 谢谢,@Ralf Stubner。我会在编写本书时为新的疑问开一个新帖子。
  • 如果你有机会,可以看看更新的帖子stackoverflow.com/questions/56297474/…,如果你有任何建议,请告诉我,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 2012-09-01
  • 2020-02-23
  • 2021-12-27
  • 1970-01-01
  • 2016-11-19
  • 2019-10-11
  • 1970-01-01
  • 2021-03-15
相关资源
最近更新 更多