2

Anyway to include a language besides English in Rmarkdown pdf_document?

Consider using this reprex to reproduce the problem.

---
title: ""
author: ""
date: ""
output:
  pdf_document:
    # latex_engine: xelatex
    keep_md: true
    keep_tex: true
header-includes: 
   # - \usepackage[arabic]{babel}
   - \usepackage[arabic,english]{babel}

   # - \inputencoding{utf8}
   # - \usepackage[utf8]{inputenc}
---
<!-- from https://rstudio.cloud/project/923892 -->

Uncomment the following code if your tinytex is not well configurated. Advisable to use in an Rstudio cloud project.

```{r}
# library(tinytex)
# options(tinytex.verbose = TRUE)
# options(tinytex.clean = FALSE)
# if (!dir.exists('~/.TinyTeX')) {
#   install_tinytex()
# }
# 
# Sys.getenv("PATH")

```

h

ل

dtthf

dgfافق

I wish to knit a document that includes English and Arabic text. The Arabic text is considered Unicode characters and execution halts. Using either of latex engines xelatex or lualatex renders the document successfully, but the Arabic/unicode characters are not rendered; they are simply ignored.

From this Rstudio community topic( Latex language package installation not working - RStudio Cloud - RStudio Community), it seems that there is a problem with defining languages in tinytex. A solution offered was to include:

header-includes:
     \usepackage[icelandic]{babel}

in the YAML header, to use the Icelandic locale provided by the CTAN: Package babel project for multilingual LaTeX support. Using:

header-includes:
     \usepackage[arabic]{babel}

renders Arabic characters perfectly, but renders only Arabic. English letters are converted to Arabic ones, even the R code itself. From the babel support, you can use this:

header-includes:

     \usepackage[arabic, english]{babel}

to declare you are using both English and Arabic, with the last one called (in this case, English) being the default language. However, I get this error: ! LaTeX Error: Command \lam unavailable in encoding T1. The \lam command corresponds to the Arabic letter, lam ( ل ), it just happens to be the first Arabic letter in the text. I tried to use xelatex, I tried to change the encoding into utf8, but none of these solved the solution.

I have no prior LaTeX knowledge so these encoding and multilingual topics seem to difficult for me. The main question is: how to successfully knit a bilingual rmarkdown document, especially English-Arabic document? Your help is most appreciated.

  • Look there: https://tex.stackexchange.com/questions/496941/how-to-write-in-english-in-an-arabic-text – manro Dec 10 '21 at 21:03
  • Dear @manro , sorry for the delay! I tried what the link you mentioned suggests (look below) but still the same problem. --- header-includes: - \usepackage[utf8]{inputenc} - \usepackage[LAE,T1]{fontenc} - \usepackage[arabic,english]{babel} - \newcommand{\ab}{\selectlanguage{arabic}} - \newcommand{\en}{\selectlanguage{english}} ``` – Hossam Ghorab Dec 12 '21 at 21:40
  • From (https://stackoverflow.com/questions/53419058/writing-greek-text-in-rmarkdown) and (https://stackoverflow.com/questions/50539913/fonts-for-rmarkdown-document), it seems like I have to change the font to a font that actually includes Arabic characters. Using `[LAE, T1]{fontenc}` doesn't solve anything. I tried the `fontspec` package but I can't find a OpenType font that provides Arabic and English characters. You have any experience with that? – Hossam Ghorab Dec 12 '21 at 21:45
  • Hm, try to read about package ```polyglossia``` https://www.overleaf.com/learn/latex/Multilingual_typesetting_on_Overleaf_using_polyglossia_and_fontspec – manro Dec 12 '21 at 23:31
  • 1
    You could try to set `lang: ar` in the metadata and use `[some English text]{lang=en}` to mark other text. You may also have manually add `[text]{dir="rtl"}` or `dir="ltr"` here and there to adjust the text direction. – tarleb Dec 13 '21 at 13:25
  • @manro, I tried the following under the `xelatex` engine: ``` header-includes: - \usepackage{polyglossia} - \setdefaultlanguage{english} - \setotherlanguage{arabic} - \newfontfamily\arabicfont[Script=Arabic]{Arial} ```` I get the same output as when nothing is put in the header of the tex: English is rendered, Arabic is ignored. – Hossam Ghorab Dec 14 '21 at 18:13
  • @manro If you **set the main language to be Arabic** and the other language to be English, you get no output at all! You get `! Package polyglossia Error: The current latin font latinmodern-math.otf(3) does not contain the "Arabic" script! (polyglossia) Please define \arabicfont with \newfontfamily command.` Although I defined the Arabic font for Arabic script properly! There seems to be a problem with setting the main language to Arabic. – Hossam Ghorab Dec 14 '21 at 18:13
  • @tarleb I get Arabic characters rendered when I use xelatex engine with a mainfont that understands Arabic scripts. However, the rendered text is in wrong direction, RTL, and the letters within each word aren't witten as they are a part of the same word, they are just written side-by-side. Unfortunately, using `[text](lang=ar)` and `[text](dir="rtl")` doesn't help with it. This reprex in reproduces the problem on my Windows OS: ```` --- title: "" author: "" date: "" output: pdf_document: latex_engine: xelatex mainfont: Arial --- بسم الله الرحمن الرحيم ```` – Hossam Ghorab Dec 14 '21 at 18:34
  • 1
    Editing last comment: `[text]{dir="rtl"}` works! Words are aligned as they should. However, How do I overcome the problem of bad letter-to-letter junctions? To make it clear to you, instead of getting the sentence _بسم الله الرحمن الرحيم_و I get [this](https://drive.google.com/file/d/1w1zuFTRZv3639ytJa1MDitz58kftXseB/view?usp=sharing). Any help? – Hossam Ghorab Dec 14 '21 at 18:52
  • Solved! To render a bilingual Arabic-English Rmarkdown -> LaTeX document smoothly, you need `xelatex`, `polyglossia`, and a `mainfont`! No LaTeX lines needed in the preamable needed, Pandoc YAML variables will do it all. Check the code in [my other question](https://stackoverflow.com/questions/70397830/auto-align-text-for-a-bilingual-rmarkdown-latex-document). Thanks a lot @manro and @tarleb . – Hossam Ghorab Dec 17 '21 at 19:12
  • 2
    I gave you "+1" for the nice effort. Working with another from English locale is always torment. – manro Dec 17 '21 at 20:01
  • 1
    @HossamGhorab you could write an answer to your own question, that would make it easier for others to find the answer (and would probably get you some points, too). – tarleb Dec 18 '21 at 19:54
  • Thanks @tarleb ! I shall post a the solution and maybe add it to the post itself once the mis-alignment problem is solved. – Hossam Ghorab Dec 19 '21 at 21:36

0 Answers0