0

I have a shiny application that produces an R Markdown report in pdf format, so I used also LaTeX to customise it. In my YAML I use the fontspec package to specify a custom font for the whole document (Times), with the following code:

---
output:
  pdf_document:
    latex_engine: xelatex
header-includes:
  - \usepackage{fontspec}
  - \setmainfont{Times}
---

But if I have the .otf file of my fonts, I can also set the main font with the commands specified in this question, i. e. by specifying an absolute path.

My problem is that, once the shiny app is deployed on shinyapps.io, the fonts, in both cases, are not found and the R Markdown report is not downloaded.

Do you have any suggestion on how to specify the fonts online? Thanks!

4
  • You should contact shinyapps.io customer service. – DG' Nov 12 '19 at 17:36
  • Isn't there another package to specify the fonts or a way to do that directly inside the .rmd file and not in the YAML section? – nd091680 Nov 13 '19 at 9:52
  • Do you want to use the "Times" font or is that just used as an example? Do you have a requirement for using XeLaTeX? – Ralf Stubner Nov 13 '19 at 13:02
  • Times will be the main font of my "one-page" report. I need "xelatex" because if I compile my .rmd report without it the fontspec function to set Times as main font doesn't work. The error message suggests also to use xelatex or lualatex. I really don't know how to proceed. – nd091680 Nov 13 '19 at 13:13
1

I cannot tell why you have to use XeLaTeX or LuaLaTeX without seeing more of your code and the actual error messages. However, using these engines should still be possible. My suggestion would be to use

---
output:
  pdf_document:
    latex_engine: xelatex
mainfont: TeX Gyre Termes
---

as YAML header. This brings two changes. First, a special YAML header is used to set the font name instead of using raw LaTeX code. Second, a Times-clone is used instead of the Times font. This font should be included in the TeX installation on shinyapps.io. If it is not, this would be a good reason to contact their support staff.

I have tested this on Linux and it should also work on Windows. However, Mac OS does not allow loading fonts by name from the TEXMF tree with XeLaTeX. There you either have to switch to LuaLaTeX or load the fonts by file name:

---
output:
  pdf_document:
    latex_engine: xelatex
header-includes:
- |
  ```{=latex}  
  \usepackage{fontspec}
  \setmainfont{texgyretermes-regular.otf}[
        BoldFont = texgyretermes-bold.otf,
        ItalicFont = texgyretermes-italic.otf,
        BoldItalicFont = texgyretermes-bolditalic.otf]
  ```
---

This also works on Linux (and probably Windows), but is rather ugly with all the necessary quoting.

Now for the more general question of how to use a font where no clone is available, I would try the following:

  • Include the font as TTF or OTF file in you Shiny app along with the Rmd file.
  • Also copy the font file when copying the Rmd file to a temp directory before processing.
  • In the Rmd refer to the font by file name. Since the font is in the same directory, no path should be necessary.
7
  • Hi Ralf and thanks for your answer. As regard you're first solution, I started testing it on a very simple .rmd file locally (and then on my application), but in both cases when I knit my pdf I get the following error message, which is the same that I get on shinyapps.io servers when the font "Times" is not found. Do you know what could be the problem? I will now test your second possible solution. Thanks a lot! – nd091680 Nov 14 '19 at 10:39
  • @nd091680 Do you have the tex-gyre package installed? sudo tlmgr install tex-gyre should help. – Ralf Stubner Nov 14 '19 at 10:42
  • I have opened my terminal, used your command and that's the messages I got: tlmgr: package repository http://mirror.kumi.systems/ctan/systems/texlive/tlnet (not verified: gpg unavailable) tlmgr install: package already present: tex-gyre But unfortunately it's still not working. – nd091680 Nov 14 '19 at 10:54
  • @nd091680 Which OS are you using? – Ralf Stubner Nov 14 '19 at 11:10
  • macOS Mojave. If I manage to fix this, I am wondering if for windows and linux users there would be no problems in downloading my report. – nd091680 Nov 14 '19 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.