Search code examples
latexmarkdownr-markdownpdflatexxelatex

How to change font style font with Rmarkdown-PDF , already sent error : " Failed to find a package that contains Times New Roman"


It's my first Markdown report, and i try to write it directly on a PDF. I need to use Times New Roman font but the export fail:

Package fontspec Error: The font "Times New Roman" cannot be found.

I've already tried these code

font-family: Times New Roman

or

mainfont: Times New Roman

or


header-includes:

- \usepackage{fontspec}

- \setmainfont{Times New Roman}

but none of these codes work.

Thanks !


Solution

  • As @RalfStubner has already mentioned, you can use TeX Gyre Termes to embed a font like Times New Roman.

    Install tex-gyre

    In order to use TeX Gyre Termes, you need to install tex-gyre package from CTAN. If you have installed @Yihui's TinyTeX, type the following code to R console.

    tinytex::tlmgr_install("tex-gyre")
    

    The code above is equivalent to executing the code below in your bash.

    tlmgr install tex-gyre
    

    Provide .tex file to specify font(s) you want

    Setting mainfont: in the YAML header should also work. However, here I show how to set fonts by including a .tex file, say font-config.tex for instance, to the YAML section. By the font-config.tex file, you can configure more detailed options, like Scale=MatchUppercase.

    In the following font-config.tex, TeX Gyre Termes, or Times New Roman is set as the main font (\rmfamily) and TeX Gyre Heros, so-called Helvetica or Arial is specified as the san serif font (\sffamily). Save the file in your current directory.

    font-config.tex

    \usepackage{fontspec}
    
    %rmfamily
    \setmainfont[Scale=MatchUppercase]{TeX Gyre Termes} %Times New Roman
    
    %sffamily
    \setsansfont[Scale=1]{TeX Gyre Heros} %So called Helvetica, Arial
    

    Include font-config.tex into the YAML header

    You can call the font-config.tex from the YAML header, as shown below.

    ---
    title: "The definitive MWE"
    subtitle: "Oh, yeah"
    author: "CLR"
    output:
      bookdown::pdf_document2:
        number_sections: true
        latex_engine: xelatex #or lualatex
        keep_tex: true
        includes:
          in_header: 
            - font-config.tex
    ---
    
    *Hellow world!* in Times New Roman.
    
    \textsf{Hellow world!} in Helvetica.