Writing $\LaTeX$ in two different languages, where one of them is Left-to-Right (English)
and the other is Right-to-Left (Persian) is cumbersome. I found emacs to have a better
support for bidirectional text (VSCode is awful for this purpose, and TeXStudio was not
ideal). Changing editor solves the issue of rendering source files, yet the tedious task
is to surround every Latin script with \lr{}
macro (from xepersian package). This macro
commands $\LaTeX$ to use the correct font (Latin Modern Roman in my case) and change the
directionality of text in the output file.
To automate this task (wrapping \lr{}
macro around every English text), I wrote this
series of functions in elisp.
|
|
The usage is pretty simple. In a .tex
file, select a region of text and press C-c r
.
This is one of my first attempts to write something in elisp; So I won’t be surprised if there was a noticeable error in this code. The logic of the code might seem a little weird. There are four main principles I wanted to consider:
- Every Latin character needs to be wrapped.
- Every Persian character should not be wrapped.
- For every two Latin character, if there are no Persian character between them, they
should be wrapped in a single
\lr{}
. - For a Latin character and a Neutral character, if there are no Persian or Space
characters between them, they should be wrapped in a single
\lr{}
.
References
- The snippet as a gist: https://gist.github.com/ParsaAlizadeh/37a79eb1e86269d6251528418e354aee
- Xepersian user documentation: http://mirrors.ctan.org/macros/xetex/latex/xepersian/xepersian-doc.pdf
- Emacs bidirectional text: https://www.gnu.org/software/emacs/manual/html_node/elisp/Bidirectional-Display.html
- Emacs character classes: https://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Markers.html
- Emacs markers: https://www.gnu.org/software/emacs/manual/html_node/elisp/Overview-of-Markers.html