So I've created an Rmarkdown file that generates a pdf output. The main language is English but I have some arabic words here and there in my resulting dataframes that I then push out as tables using kableExtra.
The problem is that my arabic texts do not join together as they should and are rendered as individual letters (i.e this word "الكل" looks like this in the output "ال ك ل" where letters do not join together).
When I set the default language to arabic the words look perfect, however the whole document including tables and english words go from right to left which makes it look all out of whack. I can't really apply these solutions mentioned here and here because I cannot apply them to my dataframe/table outputs. is there a way to fix this?
my YAML code at the begining of my .rmd document is below
---
output:
pdf_document:
latex_engine: xelatex
geometry: margin=1.7cm
params:
branch: ""
month: "March"
year: "2024"
header-includes:
- \usepackage{fontspec}
- \setmainfont{Amiri}
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---
# Quantity Sold
``{r}
library(tidyverse)
library(readxl)
library(openxlsx)
library(tinytex)
mt <- data.frame(Product = c("متين GERMA-C2TE","لاصق GERMA-Glue","لاصق GERMA-C1","Alumacer/ 000CRU3 - CRUCETAS PVC 3 mm (300/كيس)"),
mpg = c(1,5,14,2),
cyl = c(20,54,67,83),
hp = c(130,200,201,145))
final_kable <- mt %>% knitr::kable("latex", booktabs = T, longtable = T, align = c("l","r","r","r"), col.names = c("Cars","mpg","cyl","hp")) %>%
kableExtra::kable_styling(latex_options = c("scale_down"),position = "left", full_width = F) %>%
kableExtra::kable_styling(font_size = 9) %>%
kableExtra::pack_rows(group_label = "الكل / الستيلات", start_row = 1, end_row = 2) %>%
kableExtra::pack_rows(group_label = "الكل / تيكا", start_row = 3, end_row = 4)
final_kable
``
I have also tried adding the polyglossia package but to no avail as the word that should look like this "الكل" looks like this in the output "ل ك ل ا" where letters are going from left to right and are not joining together.
---
output:
pdf_document:
latex_engine: xelatex
geometry: margin=1.7cm
params:
branch: ""
month: "March"
year: "2024"
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
- \usepackage{longtable}
- \usepackage{hyperref}
- \usepackage{wrapfig}
- \usepackage{float}
- \usepackage{colortbl}
- \usepackage{fontspec}
- \setmainfont{Amiri}
- \usepackage{polyglossia}
- \setdefaultlanguage{english}
- \setotherlanguages{arabic}
---
# Quantity Sold
``{r}
library(tidyverse)
library(readxl)
library(openxlsx)
library(tinytex)
mt <- data.frame(Product = c("متين GERMA-C2TE","لاصق GERMA-Glue","لاصق GERMA-C1","Alumacer/ 000CRU3 - CRUCETAS PVC 3 mm (300/كيس)"),
mpg = c(1,5,14,2),
cyl = c(20,54,67,83),
hp = c(130,200,201,145))
final_kable <- mt %>% knitr::kable("latex", booktabs = T, longtable = T, align = c("l","r","r","r"), col.names = c("Cars","mpg","cyl","hp")) %>%
kableExtra::kable_styling(latex_options = c("scale_down"),position = "left", full_width = F) %>%
kableExtra::kable_styling(font_size = 9) %>%
kableExtra::pack_rows(group_label = "الكل / الستيلات", start_row = 1, end_row = 2) %>%
kableExtra::pack_rows(group_label = "الكل / تيكا", start_row = 3, end_row = 4)
final_kable
``
You could use the LaTeX package ucharclasses
to automatically switch between different fonts (or in this case the same font but with different scripts activated) based on Unicode code block:
---
output:
pdf_document:
latex_engine: xelatex
header-includes:
- \usepackage{longtable}
- \usepackage{booktabs}
- \usepackage{hyperref}
- \usepackage{fontspec}
- \newfontfamily{\defaultfont}{Amiri}
- \newfontfamily{\latinfont}{Amiri}
- \newfontfamily{\arabicfont}[Script=Arabic]{Amiri}
- \usepackage[Latin, Arabic]{ucharclasses}
- \setDefaultTransitions{\defaultfont}{}
- \setTransitionsForLatin{\latinfont}{}
- \setTransitionsForArabics{\arabicfont}{}
---
# Quantity Sold
```{r}
library(tidyverse)
mt <- data.frame(Product = c("متين GERMA-C2TE","لاصق GERMA-Glue","لاصق GERMA-C1","Alumacer/ 000CRU3 - CRUCETAS PVC 3 mm (300/كيس )"),
mpg = c(1,5,14,2),
cyl = c(20,54,67,83),
hp = c(130,200,201,145))
final_kable <- mt %>% knitr::kable("latex", booktabs = T, longtable = T, align = c("l","r","r","r"), col.names = c("Cars","mpg","cyl","hp")) %>%
kableExtra::kable_styling(latex_options = c("scale_down"),position = "left", full_width = F) %>%
kableExtra::kable_styling(font_size = 9) %>%
kableExtra::pack_rows(group_label = "الكل / الستيلات", start_row = 1, end_row = 2) %>%
kableExtra::pack_rows(group_label = "الكل / تيكا", start_row = 3, end_row = 4)
final_kable
```
Result:
Note that I have introduced a space before the closing parenthesis. Otherwise it would have been treated as port of the Arabic RTL text. One can see the same effect with the quotation marks in the included source code. It probably would be better to insert something like U+200B ZERO WIDTH SPACE instead.