1

I don't have a good use case for this. I'm wondering if it's possible to set up LaTeX to switch between left align and right align for each line in a paragraph. For example, lines 1, 3, 5, 7, 9, etc. are left aligned, while lines 2, 4, 6, 8, etc. are right aligned. Obviously this could be done by hand, but I'm wondering if there is a way to set a document up so that it does it automatically without having to slap line breaks everywhere? Note, this is in the context of normal text, not equations. Again, I don't have any real use case for this, just wondering if it's possible and if so how to go about it.

Example:

  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.

2 Answers 2

2

If you first set in a box you can (as long as there are no color whatsits or other non removeable items) deconstruct the list and rebuild with re-aligned line boxes

enter image description here

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

A typical use case I can imagine for aligning lines in this way would involve prior knowledge of where in the text the line breaks will be, for example, because they make up a poem.

If that is the case, you can define an end-of-line macro like the following:

\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,\\
...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .