How to render PDF from R Markdown with Chinese characters

I am unable to render PDF files from R Markdown properly due to the Chinese characters in an otherwise mostly English document, from a Windows 11 machine, using tinytex. I'm receiving the following error:

! Package fontspec Error: The font "SimHei" cannot be found.

MRE of the Rmd file:

---
header-includes:
- \usepackage{ctex}
output:
  pdf_document:
    latex_engine: xelatex
---

位置適中,環境優美。                                                                                                                                                              
Central location, beautiful environment

I do have the SimHei font installed on my machine, which is causing me some confusion as to how to make tinytex be aware of it.

That says it all. This post explains how to fix it in regular TeX, how that plays with tinytex is something I'm unsure of, so use this at your own risk

`tinytex::tlmgr_install("SimHei")`

Thanks for your reply @technocrat - running tinytex::tlmgr_install("SimHei") returns the following:

tlmgr.pl install: package SimHei not present in repository.
tlmgr.pl: action install returned an error; continuing.
tlmgr.pl: package repository https://ctan.mirror.rafal.ca/systems/texlive/tlnet (not verified: pubkey missing)
tlmgr.pl: An error has occurred. See above messages. Exiting.

Yeah, I get the same on my setup with a different repo

tlmgr.pl: package repository https://ctan.mirror.rafal.ca/systems/texlive/tlnet (not verified: pubkey missing)

This post discusses installing from the local machine after downloading the font.

I'm fairly certain that tlmgr_install, and the post you are linking, is specifically for latex packages, not for fonts. Looking on CTAN, there does not appear to be a package named "SimHei".

Anyway. I'm calling the ctex package here because my browsing around has pointed me to that for the use of Chinese characters, but perhaps I'm going down the wrong path and should be using another package instead.

1 Like

I may be further muddying the waters, but FWIW, I have this running after

sudo apt-get update
sudo apt install texlive-full

and knitting your sample document downloaded a lot of files. However, I don't know now whether RStudio is still rendering from tinytex or is using the system tex

I had to install the fonts needed using the "Install for all users" admin option - initially I was only providing the font for my user privilege.

1 Like

Yes a font is missing. It is not a CTAN package. Fonts are hard to handle with LaTeX especially on Windows.

You usually need to have the font available on your Windows system. Do you have it ?

I would suggest looking online about Windows + TexLive + Chinese fonts to see the setup you can found.

You can change the name of the font also through YAML

For reference, this has been posted on SO too: How to render PDF from R Markdown with Chinese characters - Stack Overflow

I believe SimHei is not installed by default on Windows
https://github.com/MicrosoftDocs/windows-itpro-docs/blob/public/windows/deployment/windows-10-missing-fonts.md

You need the chinese option

Please do at least cross reference (FAQ: Is it OK if I cross-post?)

Hi @cderv yes I was inadvertently installing the font to my own user privilege instead of a system wide install.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.