Typesetting alternating text left to right and right to left

Here is a solution using etex's bidi feature. Note that it does not require breaking the lines manually. We first collect the text in a box, from which we then split off line by line, alternately setting them left-to-right and right-to-left, until nothing is left (but the output is right).

enter image description here

\documentclass{article}
\TeXXeTstate=1
\newbox\textbox
\newbox\linebox
\newif\ifrtl
\def\ltrrtl#1{%
  \setbox\textbox=\vbox{#1}%
  \rtlfalse
  \loop\ifvbox\textbox
    \vbadness=10000
    \splittopskip0pt
    \setbox\linebox\vsplit\textbox to\baselineskip
    \unvbox\linebox\setbox\linebox=\lastbox
    \hbox to \hsize{\ifrtl\beginR\fi\unhbox\linebox\ifrtl\endR\fi}%
    \ifrtl\rtlfalse\else\rtltrue\fi
  \repeat
}
\begin{document}
\parbox{2.5cm}{\ltrrtl{Hello, I actually am a little text with no meaning}}
\end{document}

The stringstrings package has a \reversestring macro.

\documentclass{article}
\usepackage{stringstrings}
\begin{document}
\noindent
Hello, I actually\\
\reversestring{am a little text}\\
with no meaning
\end{document}

And with that, we can make it automatically switch alternating lines as such:

\documentclass{article}
\usepackage{stringstrings}
\usepackage{readarray}
\usepackage{ifthen}
\usepackage{calc}
\newcounter{index}
\newlength\linelength
\newcommand\altsense[1]{%
  \def\linesense{N}%
  \def\thisline{}%
  \getargsC{#1}%
  \setcounter{index}{0}%
  \whiledo{\theindex < \narg}{%
    \stepcounter{index}%
    \let\savedline\thisline%
    \edef\thisline{\thisline\csname arg\roman{index}\endcsname\ }%
    \setlength{\linelength}{\widthof{\thisline}}%
    \ifthenelse{\lengthtest{\linelength < \textwidth}}%
      {}%
      {\if N\linesense\savedline\def\linesense{R}%
       \else\reversestring{\savedline}\def\linesense{N}\fi%
       \addtocounter{index}{-1}%
       \def\thisline{}\\}%
  }%
  \if N\linesense\thisline\else%
  \reversestring{\thisline}\fi\\%    }
}
\begin{document}
\noindent
\altsense{this is a test this is a test this is a test this is a test
and here we have some other text material to see what happens. 
this is a test this is a test this is a test this is a test}
\end{document}

enter image description here

Tags:

Bidi