I have written a .Rmd file which includes a unicode character (\u2264, <=) in some figure headings. When I render the code within the .Rmd document, it renders correctly. But when I compile the document using the knit button, the character is rendered incorrectly as "=". This is demonstrated with the following TestCase:
author: "Hunsicker LG"
This is an R Markdown test document.
library(knitr) library(survminer) library(survival) df1 <- data.frame(time = rnorm(100, 100, 20), event1 = sample(0:1, 100, replace = T), covar1 = sample(0:1, 100, replace = T)) model1 = survfit(Surv(time, event1) ~ covar1, data = df1) ggsurvplot(fit = model1, risk.table = F, xlab = 'Years', pval = F, title = "Junk \u2264 Stuff")
When rendered within the Rmd document in the R-Studio editor, it renders correctly.
But when printed out using the knit button the "<=" character (\u2264) prints out incorrectly as '='.
I suspect that this is some sort of font issue. The Latex is evidently being rendered correctly, but the default Latex to pdf program apparently doesn't interpret the unicode correctly. But I haven't been able to get the document to print correctly. What's the required magic?
One of the nice things about RStudio is that most of the time things just work. One doesn't have to understand how the system does things. But when things don't work, one then has to figure out what is actually happening. After some noodling around, it seems to me that when one pushes the "Knit" button, the following sequence happens:
- the "Knit" button invokes the render function, which sends the .Rmd file file to knitr, which
- executes the R code, and then sends the file to markdown, which
- formats the combined files using pandoc to some sort of "document-p-code," and then
- passes the document to tinytex/TinyTex to compile the .tex document, which then
- in the case of a pdf-document, uses either pdfLatex or xeLatex to render the doc to pdf.
Now when I run my TestCase above, if I look at the ouput within the Rmd file in the RStudio Source window, the \u2264 unicode <= character gets rendered correctly. Similarly, if I render the file as a MS Word .docx file, it gets rendered correctly. But when I render the file as a pdf document, it gets mistranslated as "=". For what it's worth, I am using xelatex rather than the default pdflatex, as I am told that xelatex handles unicode better than pdflatex, but this didn't fix my problem.
So it seems likely that the problem is at the tinytex/TinyTex stage, and probably results from the default font file lacking the \u2264 character.
My somewhat refined questions, then, are:
- Do I understand the above sequence correctly?
- If so, how do I determine what the default Latex font(s) are?
- What font(s) would have the \u2254 character (and more generally, math characters)?
- And finally, how would I install a font that DOES have the \u2254 character without messing up the rest of the formatting? Can I do this by including something in the .Rmd file header?
Thanks in advance for the help.