0

Here is a reprex :

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

I want only ONE font in the document, ie. the default font for the text. To do this I have added the line monofont: "Computer Modern" (by this I am trying to tell the software to create the output of code in the same font as the text). I get the following error when I try to create a PDF from the above Rmarkdown file. I have a Ubuntu system. How can I fix this ?

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

This is a follow up query to the original query posted here: [Original Query] Fonts for Rmarkdown document

user2338823
  • 501
  • 1
  • 3
  • 16
  • Why are you trying to do this? I find it highly unusual to use a variable width font for source code. – Ralf Stubner May 27 '18 at 08:42
  • I have been asked to create a document which looks like a SINGLE document. One of my specific instructions was to make the output of code look like the text in the document. – user2338823 May 27 '18 at 08:46

1 Answers1

2

Actaully it is not Computer Modern but its close Relative Latin Modern that is used as default. So you could try:

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

Since you have an Ubuntu system, you can use fc-list to see all the fonts installed on your system, that are available for XeLaTeX.

Alternatively, if you do not want to use XeLaTeX, you can use

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

in the header.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • This is great. I noticed that the output of table loses its alignment when we change it's font. The table made with the kable incantation does NOT lose it's alignment. Many thanks. One more query, why does table respond to monofont and kable to mainfont ? They are both output of code. In the future how will I come to know if a particular command needs mono or mainfont ? – user2338823 May 27 '18 at 09:51
  • @user2338823 `table` produces output on the R console, which is rendered using `monofont` but looses its alignment when a variable width font is used for code (output). `kable` on the other hand is specifically meant for producing good looking tables that are rendered as normal text but have suitable markup to be properly aligned. See https://rmarkdown.rstudio.com/pdf_document_format.html#data_frame_printing how to make this the default. Typically one would use `kable` in a separate block with `echo = FALSE`. – Ralf Stubner May 27 '18 at 10:00
  • I could do with only the kable output. In that case we do NOT need monofont OR mainfont, since kable will be controlled by the mainfont whose default value is Latin Modern Roman. Do I get it right? In that case my kable output and text output would be in the same font (Latin Modern Roman). – user2338823 May 27 '18 at 10:07
  • 1
    @user2338823 If you do not need the code itself but only the (tabular) output, then using `kable` together with `echo = FALSE` is probably the best solution. – Ralf Stubner May 27 '18 at 10:11