How to write Chinese report using R Markdown

本文介绍如何在R Markdown文件中正确使用LaTeX引擎XeLaTeX来显示中文字符,避免出现乱码错误。通过设置合适的字体及配置文件,确保中文内容能够正常呈现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    • If you directly implement with Chinese, it will throw error:
      ! Package inputenc Error: Unicode char \u8: not set up for use with LaTeX. Try running pandoc with –latex-engine=xelatex.

    • Then you should put the following code in the head of the R markdown file:

    title: Your File Title
    author: Your Name
    output:  
      pdf_document:
        latex_engine: xelatex
    • Though now it can be implemented, all Chinese disappear. So you should add a new latex file header to the R markdown file using the following code.
    title: Your File Title
    author: Your Name
    outputs:  
      pdf_document:
        includes:
          in_header: header.tex
        latex_engine: xelatex
    • Now I will give you the content of header.tex file, which should be put in the same folder of your R markdown file.
    \usepackage{xeCJK}
    \setCJKmainfont{HanziPen SC}  % Chinses Font
    \setmainfont{Georgia} 
    \setromanfont{Georgia} 
    \setmonofont{Courier New}
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值