如何在每行文本上交替左对齐和右对齐?

如何在每行文本上交替左对齐和右对齐?

我没有很好的用例。我想知道是否可以设置 LaTeX 以在段落的每一行之间切换左对齐和右对齐。例如,第 1、3、5、7、9 行等为左对齐,而第 2、4、6、8 行等为右对齐。显然这可以手动完成,但我想知道是否有办法设置文档以便它自动执行此操作而不必在各处打换行符?请注意,这是在普通文本的上下文中,而不是方程式的上下文中。同样,我没有实际的用例,只是想知道这是否可行,如果可行,如何去做。

例子:

  This is a line which is indented and left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
                       all in one giant useless paragraph.

答案1

如果你首先设置一个框,你可以(只要没有颜色或其他不可移除的项目)解构列表并使用重新对齐的线框重建

在此处输入图片描述

\documentclass{article}

\newif\ifpushright
\def\repack{%
\par
\global\pushrightfalse
\global\setbox1\vbox{}%
\loop
\count0=\lastnodetype
\ifnum\count0=1
\setbox0\lastbox
\global\setbox1\vbox{%
\ifpushright
\global\pushrightfalse
\hbox to \hsize{\hfill\unhbox0\unskip\unskip}%
\else
\global\pushrighttrue
\box0
\fi
\unvbox1
}%
\else\ifnum\count0=11
\skip0=\lastskip
\unskip
\global\setbox1\vbox{%
\vskip\skip0
\unvbox1
}%
\else\ifnum\count0=13
\count2=\lastpenalty
\unpenalty
\global\setbox1\vbox{%
\penalty\count2
\unvbox1
}%
\fi\fi\fi
\ifnum\count0>-1
\repeat
}
\begin{document}


{\raggedright\parindent=15pt
 This is a line which is indented and left aligned,
       which is followed by a line which is right aligned,
which issss followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
                       all in one giant useless paragraph.

}

\bigskip


\setbox0\vbox{\raggedright\parindent=15pt
 This is a line which is indented and left aligned,
       which is followed by a line which is right aligned,
which issss followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
       which is followed by a line which is right aligned,
which is followed by a line which is left aligned,
                       all in one giant useless paragraph.
\repack}
\unvbox1


\end{document}

答案2

我可以想象的以这种方式对齐行的一个典型用例将涉及对文本中换行符位置的先验知识,例如,因为它们构成了一首诗。

如果是这种情况,您可以定义一个行尾宏,如下所示:

\newcount\linecount
\everypar={\count\linecount=1 }
\def\\{\ifodd\count\linecount\hfil\break\vadjust{}\hfil
  \else\break
  \fi
  \advance\count\linecount by1 }
  This is a line which is indented and left aligned,\\
       which is followed by a line which is right aligned,\\
...

相关内容