使用与Rmarkdown中文本的默认字体相同的字体输出代码

时间:2018-05-27 07:26:45

标签: r pdf yaml r-markdown

这是一个代表:

---
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

1 个答案:

答案 0 :(得分: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}
标题中的