# Minimal bidi for typesetting RL text

I need to typeset RTL text (Arabic, Persian) for linguistics books. I know of the bidi package. But bidiis way to powerful for our purposes and it has many side effects (it basically affects everything: title, footnotes, margin notes, ...). This results in problems with hyperref as described here: bidi and hyperref change hyphenation

What I need is just the support for using the script in the main text. So the question is: What do I need for this functionality? Can I use code from the bidi package? Could there be a package that users like me and bidiloads? This would be most modular, I guess ...

\documentclass{scrbook}

\usepackage{fontspec}

\usepackage{bidi}

\newfontfamily\Parsifont[Script=Arabic]{XB Niloofar}
\usepackage{bidi}
\newcommand{\PRL}[1]{\RL{\Parsifont #1}}

\begin{document}

Some Persian example: \PRL{او مرد را دوست نخواهد داشت.}

\end{document}


Edit:

OK. I now have the following. This works nicely for one line, but breaks for longer text. The problem is that after a linebreak text should start at the right hand site and not in the middle of the page as if it was left to right text ending in the middle of the page.

\documentclass{scrbook}

\usepackage{fontspec}

\newfontfamily\Parsifont[Script=Arabic]{XB Niloofar}

\newcommand{\TeXXeTOn}{\TeXXeTstate=1 }
\newcommand{\TeXXeTOff}{\TeXXeTstate=0 }
\TeXXeTOn

\newcommand{\RL}[1]{\beginR #1\endR}

\newcommand{\PRL}[1]{\RL{\Parsifont #1}}

\begin{document}

Some Persian example: \PRL{او مرد را دوست نخواهد داشت.}

\PRL{او مرد را دوست نخواهد داشت. و مرد را دوست نخواهد داشت. و مرد را دوست نخواهد داشت. و مرد را دوست نخواهد داشت.}

\end{document}

• It depends on how much you want to achieve. You can easily switch to RTL using TeX--XeT (pdfTeX, XeTeX) or Omega-like primitives (LuaTeX), but a simple-minded approach will not necessary give you all the bells and whistles you need. – Joseph Wright Nov 2 '13 at 10:08
• Could you please tell me how this is done? I looked at the manual and found \beginR and \endR, but xelatex complained: ! Improper \beginR. – Stefan Müller Nov 2 '13 at 10:38
• Minimal (plain) pdfTeX/XeTeX example: \TeXXeTstate = 1 \newbox\mybox \everypar{\setbox\mybox\lastbox\beginR\box\mybox} will make all paragraphs RTL and move the indent correctly. – Joseph Wright Nov 2 '13 at 10:41
• Well, if it were Hebrew, I'd suggest the minimalistic cjhebrew package. It doesn't look like there's an Arab equivalent though. – einpoklum Nov 2 '13 at 22:26
• Loading bidi with no options means you want to typeset an LR document with some RL text. In LR mode, bidi does not change document behaviour (e.g. margins, footnotes, etc), but of course it gives you the ability to do so. Every change bidi does to internal macros of LaTeX and different packages, has a test to check whether you are typesetting in LR or RL mode. For LR, the macro will behave exactly as defined before loading bidi. You can also temporarily switch off TeX--XeT feature by TeXXeTOff (provude by bidi) and switch it back on by TeXXetOn in case you have a package clash. – Hasan Zakeri Nov 3 '13 at 20:08

You will be needing two different commands/environments to typeset Persian (or any other RTL language alongside Latin texts. One for small RTL texts along in an LTR paragraph, and one for RTL paragraph.

\documentclass{scrbook}

\usepackage{fontspec}
\input{bidi.tex}
\newfontfamily\Parsifont[Script=Arabic]{XB Niloofar}
\newcommand{\PRL}[1]{\RL{\Parsifont #1}}
\newenvironment{PRTL}{\RTL\Parsifont}{\endRTL}

\begin{document}

Some Persian example: \PRL{او مرد را دوست نخواهد داشت.}

\begin{PRTL}
او مرد را دوست نخواهد داشت. و مرد را دوست نخواهد داشت. و مرد را دوست نخواهد داشت. و مرد را دوست نخواهد داشت.
\end{PRTL}

\end{document}


bidi.tex is part of bidi package for bidirectional typesetting in plain XeTeX, so it is pretty much minimal. It redefines only a handful of commands, and they are essential for correct bidirectional typesetting.