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{}
.
Supporting other types of wrapping
I realized the same code works perfectly for $
wrap! It is really hard to implement such thing for
English-only script, yet in Persian-English script you just need the snippet I wrote above. I modify
it to allow different wrapping options.
|
|
I have not changed the functions my-blank-p
and my-char-type
.