# bidi, RTL and \write

In order to solve one issue with reledmac in RTL, I made some test with \write and bidi.

In the following MWE, only the first \write works. It I delete the \RTL, all the \write work.

\documentclass{article}
\usepackage{bidi}
\begin{document}
\RTL
\newwrite\toto
\openout\toto=\jobname.sty
\write\toto{1}ssss
\write\toto{2}ssss
\write\toto{3}ssss
\closeout\toto

s
\end{document}


You were lucky that 1 got output, if you use

\documentclass{article}
\usepackage{bidi}
\begin{document}
\showoutput
\RTL
\leavevmode\newwrite\toto
\openout\toto=\jobname.sty
\write\toto{1}ssss
\write\toto{2}ssss
\write\toto{3}ssss
\closeout\toto

s
\end{document}


then nothing will be written to the file,

The problem is the tex--xet bidirectional model that xetex inherited from etex. right to left directionality isn't built in to the system at a low level the way it is in the model luatex inherited from omega, it is essentially a simple pass that reverses nodes (just) in horizontal boxes.

So in vertical mode (the file open and the first \write in your example, it's OK but the line of the paragraph with the later writes and the file close, the nodes are reversed, and so the file close happens first, which means the \write are writing to a closed file so just appear on the terminal and log.

One possible scheme would be

\documentclass{article}
\usepackage{bidi}
\begin{document}
\showoutput
\RTL
\foo{\newwrite\toto}
\openout\toto=\jobname.sty
\foo{\write\toto{1}}ssss
\foo{\write\toto{2}}ssss
\foo{\write\toto{3}}ssss
\foo{\closeout\toto}

s
\end{document}

• so we need to be in a vertical mode to use \write in RTL ? – Maïeul Oct 9 '16 at 14:34
• @Maïeul not necessarily, especially for \write if you know that the entries are written in the wrong order you can usually fix it up when the file is used, but if you do \openout and \closeout in the wrong order, that's bad. – David Carlisle Oct 9 '16 at 14:40
• @Maïeul updated answer – David Carlisle Oct 9 '16 at 15:05
• thanks for your answer. I will look on it with a fresh brain, so not tonight ;-) – Maïeul Oct 9 '16 at 18:05
• Unfortunatly, this solution made problem when implementing in reledmac : it add spurious lines. I have found an other solution, anyway (github.com/maieul/ledmac/commit/…) – Maïeul Feb 23 '17 at 18:02