# LTR sequences within RTL text - alternative to cumbersome markup?

I am writing a book in Persian that is full of text in Latin scripts scattered all over book.

I have to wrap every Latin script with \lt{} to guide xetex to align words from left to right. It's really cumbersome.

If I do not use \lr{} output of One Two Three will be Three Two One in Persian document and output of یک دو سه will be سه دو یک‍‍ in English documents.

This is a very basic requirement and I wonder why xetex can not do it without extra markup. Is there any way for not using \lr{}

-

To start it, this is not as basic as you claim it to be. You may be able to do it with using \XeTeXinterchartoks primitive of XeTeX. Aan example:

The following was a response of Jonathan Kew (the author of XeTeX) to me a while ago, I just modified his example to work with XePersian:

\documentclass{article}
\usepackage{xepersian}
\makeatletter
% classes 1-3 are used in unicode-letters.tex, so we'll put the Latin  letters in 4
\newcount\xp@n
\xp@n=\A \loop \XeTeXcharclass \xp@n=4 \ifnum\xp@n<\Z \advance\xp@n by 1 \repeat
\xp@n=\a \loop \XeTeXcharclass \xp@n=4 \ifnum\xp@n<\z \advance\xp@n by 1 \repeat
% when we encounter class 4, we'll do \startlatin
\XeTeXinterchartoks 0 4 {\startlatin}
\XeTeXinterchartoks 255 4 {\startlatin}
% and when we encounter class 0, we'll do \finishlatin
\XeTeXinterchartoks 255 0 {\finishlatin}
\XeTeXinterchartoks 4 0 {\finishlatin}
\newcommand{\startlatin}{\if@Latin\else\bgroup\beginL\latinfont\@Latintrue\fi}
\newcommand{\finishlatin}{\if@Latin\unskip\endL\egroup{ }\fi}
\makeatother
\XeTeXinterchartokenstate=1
\begin{document}
این یک آزمایش است
One Two Three
و ادامه آن
\end{document}


Note that it both changes font (to latin font) and direction (to LTR).

However, I suspect you're not really going to be able to do this on a large scale, because it will be too difficult to handle things like
punctuation and spacing at direction changes. In unidirectional text, it may not matter whether the "language switch" happens before or
after the space (or punctuation mark), but with bidi it does matter. I think in the end you're still going to need markup if you want to
reliably mix LR and RL scripts.

In Addition, LR and RL scripts share some characters. So for example, how would you be able to decide if ) or ( is a RL chracter or an LR one?

Alternatively, you may be able to implement a preprocessor (written in C or any other language) that converts say, test.tex to test1.tex and places all LR words inside \lr. Actually BiDiTeX exists so you may be able to get its sources and modify it a bit to work with bidi/XePersian packages.

-
@Thank a lot. There is a lot of standard and technical documents to handling this issue in web and other desktop apps. (consider w3c and Unicode consumerism documents). Currently most of windows applications (if not all) handle mixing RTL and LTR scripts correctly.(consider notepad as a simple app). I will investigate current algorithms to find out whether they are applicable in TeX. I think the Unicode facilities right now have provided required tokens to alleviate the issue. –  PHPst May 4 '12 at 6:09
Good Luck then! –  Simurgh12 May 4 '12 at 6:45
Do you need help with the code in general or the `\XeTeXinterchartoks' primitive? –  Simurgh12 May 4 '12 at 7:10
line 6 is similar to line 7. For example what we do in line 6, is just giving class 4 to capital letters A to Z. In line 7, we give class 4 to letters a to z. –  Simurgh12 May 4 '12 at 7:15
replacing body with \begin{document} \section{x} \end{document} and the code generate an error. –  PHPst Jun 24 '12 at 18:34