【问题标题】:Making the output of code in the same font as that of the default font for the text in Rmarkdown以与 Rmarkdown 中文本的默认字体相同的字体输出代码
【发布时间】:2018-11-06 01:53:29
【问题描述】:

这是一个代表:

---
date : 2018-May-27
output:
    pdf_document:
        latex_engine: xelatex
monofont: "Computer Modern"
title: "Testing Rmarkdown"
---

```{r,comment = NA}

Gender <- gl(2,1000,labels = c("Men","Women"))
SmokerM <- sample(c("Y","N"),1000,replace = T , prob = c(.3,.7))
SmokerW <- sample(c("Y","N"),1000,replace = T , prob = c(.5,.5))
Smoker <- c(SmokerM,SmokerW)

mydata  <- data.frame(Gender,Smoker)
table(mydata$Gender,mydata$Smoker)
```
This is some running text(in the Computer Modern font).

我只想要文档中的一种字体,即。文本的默认字体。为此,我添加了 monofont: "Computer Modern" 行(通过此我试图告诉软件以与文本相同的字体创建代码输出)。当我尝试从上述 Rmarkdown 文件创建 PDF 时出现以下错误。我有一个 Ubuntu 系统。 我怎样才能解决这个问题 ?

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! fontspec error: "font-not-found"
! 
! The font "Computer Modern" cannot be found.
! 
! See the fontspec documentation for further information.
! 
! For immediate help type H <return>.
!...............................................  

这是对此处发布的原始查询的后续查询:[原始查询]Fonts for Rmarkdown document

【问题讨论】:

  • 你为什么要这样做?我发现在源代码中使用可变宽度字体非常不寻常。
  • 我被要求创建一个看起来像 SINGLE 文档的文档。我的具体指示之一是使代码的输出看起来像文档中的文本。

标签: r pdf yaml r-markdown


【解决方案1】:

实际上,默认使用的不是计算机现代,而是其接近的相对拉丁现代。所以你可以试试:

---
date : 2018-May-26
output:
    pdf_document:
        latex_engine: xelatex
mainfont: Latin Modern Roman
monofont: Latin Modern Roman
title: "Testing Rmarkdown"
---

```{r,comment = NA}

Gender <- gl(2,1000,labels = c("Men","Women"))
SmokerM <- sample(c("Y","N"),1000,replace = T , prob = c(.3,.7))
SmokerW <- sample(c("Y","N"),1000,replace = T , prob = c(.5,.5))
Smoker <- c(SmokerM,SmokerW)

mydata  <- data.frame(Gender,Smoker)
table(mydata$Gender,mydata$Smoker)
knitr::kable(table(mydata$Gender,mydata$Smoker))
```

This is a text in the body of the document.What font is this ? What is
font for the output of table ? How can we change these 2 fonts ? What 
other categories of items are there in an Rmarkdown which have different
fonts ?   

由于您有一个 Ubuntu 系统,您可以使用fc-list 查看系统上安装的所有字体,这些字体可用于 XeLaTeX。

或者,如果你不想使用 XeLaTeX,你可以使用

output:
    pdf_document
header-includes:
    - \renewcommand*{\ttdefault}{lmr}

在标题中。

【讨论】:

  • 这很棒。我注意到当我们改变它的字体时,表格的输出失去了对齐。用 kable 咒语制作的桌子不会失去它的对齐方式。非常感谢。再问一个问题,为什么 table 响应 monofont 而 kable 响应 mainfont ?它们都是代码的输出。将来我将如何知道特定命令是否需要 mono 或 mainfont ?
  • @user2338823 table 在 R 控制台上生成输出,使用 monofont 渲染,但在将可变宽度字体用于代码(输出)时会失去对齐。另一方面,kable 专门用于生成美观的表格,这些表格呈现为普通文本,但具有适当的标记以正确对齐。请参阅rmarkdown.rstudio.com/… 如何将其设为默认值。通常,人们会在带有echo = FALSE 的单独块中使用kable
  • 我只能使用 kable 输出。在这种情况下,我们不需要 monofont 或 mainfont,因为 kable 将由默认值为 Latin Modern Roman 的 mainfont 控制。我做对了吗?在这种情况下,我的 kable 输出和文本输出将使用相同的字体(拉丁现代罗马)。
  • @user2338823 如果您不需要代码本身而只需要(表格)输出,那么使用kableecho = FALSE 可能是最好的解决方案。
猜你喜欢
  • 1970-01-01
  • 2013-02-18
  • 2018-04-19
  • 1970-01-01
  • 2021-02-03
  • 2018-11-05
  • 2010-11-17
  • 1970-01-01
  • 2013-06-08
相关资源
最近更新 更多