Updated. See below.
I'm working on a bilingual report. Namely using Arabic and English languages. Using xelatex
engine, mainfont: Arial
and lang: ar
YAML metadata, the document smoothly renders both Arabic and English languages (After a hustle).
How to auto-align text in Rmarkdown -> LaTeX document?
The problem is: the language chosen in the lang
variable is aligned right-to-left, so the whole document follows this alignment. Whenever I want to insert a paragraph in English, I have to use [text]{dir="ltr"}
. Is there a way to automatically align the paragraphs based on the language used? Any LaTeX package or Pandoc / Rmarkdown trick to do so? Pure LaTeX in the preamable?
Appendix - reprex (old)
If you need it, the following code is what you need to reproduce the problem.
---
output:
pdf_document:
latex_engine: xelatex
mainfont: Arial
lang: ar
---
بسم الله الرحمن الرحيم
This text is mis-aligned in rendered document.
[This text is well-aligned in rendered document.]{dir="ltr"}
Update
The following update is to incorporate the Lua filter kindly offered by @tarleb .
The bottom line is:
- Before using the Lua filter, Arabic text was in the right direction and alignment, English text was in the wrong text direction(rtl) and alignment(rigth-aligned). See the rendered PDF without the filter here
- The filter proposed by @tarleb aimed to detect the English text paragraphs and automatically set their direction to left-to-right.
- The resulting document was that all text, regardless Arabic or English, was in the left-to-right direction and aligned to the left border of the page. See the resulting PDF here
I believe this inconvenience is because the Lua filter doesn't detect Latin/English characters only, it doesn't distinguish between Arabic VS English characters, aka. Latin VS non-Latin characters, so the filter just sets the direction of each paragraph in the document to be left-to-right.
So what happened is that the effects of lang: ar
attribute is totally reversed by the Lua filter, and we have the same problem but now with the Arabic language instead of English.
Additionally, it appears that the alignment of the paragraphs follows the direction of tis text; if the document text direction is ltr, all paragraphs are aligned to the left border, and vice versa. I'm not sure this is true. My question here is how do we set the text direction and alignment of the document for each paragraph separately? Can we use a Lua filter that detects if the first character in a paragraph is Latin VS non-Latin and sets the text direction and alignment of this very paragraph accordingly, e.g ltr direction and left-aligned if Latin, rtl direction and right-aligned if non-Latin?
Many thanks in advance.
Updated reprex:
---
output:
pdf_document:
latex_engine: xelatex
pandoc_args: '--lua-filter=ltr-paras.lua'
mainfont: Arial
lang: ar
---
بسم الله الرحمن الرحيم
Thanks to the Lua filter from **@tarleb**, the English text is well-aligned in rendered document without having to wrap it in {dir=ltr}. The text direction is left-to-right and the paragraph itself is aligned to the left border of the page.
To get the Arabic text direction right, I have to wrap it inside {dir=rtl}:
[بسم الله الرحمن الرحيم]{dir="rtl"}
However, the Arabic paragraph is still aligned wrongfully to the left border of the page.