排版交替文本从左到右和从右到左

排版交替文本从左到右和从右到左

出于好奇:我可以从左到右和从右到左交替排版吗?

Hello, I actually
txet elttil a ma
with no meaning

答案1

这是使用 bidi 功能的解决方案etex。请注意,它不需要手动换行。我们首先将文本收集到一个框中,然后逐行拆分,交替从左到右和从右到左设置它们,直到什么都不剩(但输出是正确的)。

在此处输入图片描述

\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}

答案2

stringstrings包中有一个\reversestring宏。

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

这样,我们可以让它自动切换交替线,如下所示:

\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}

在此处输入图片描述

相关内容