[Tex/LaTex] Text justification within multicols environment

horizontal alignmentmulticol

I'm using this 3-column layout.

\begin{multicols}{3}
[.. a lot of text ..]
\end{multicols}

I want to left-align the text in the left column, center the text in the middle column, and right-align the text in the right column. (Before you ask why anyone would want that: it's for a rather long list of names)

Of course, I can check which part of the text goes into which column, and justify the parts accordingly. But that's ugly. Can I tell LaTeX to do this for me?

Best Answer

enter image description here

The following code repackages the columns as set by multicols, centre aligning the middle columns and aligning the right hand column flush right. It works for paragraphs as well as single lines, but would fail if there is any extra inter-line material (such as floats or specials).

\documentclass{article}

\textheight.3\textheight
\pdfpageheight2\textheight

\usepackage{multicol}


\makeatletter

\def\maybe@reverse@columns{%
\count@\mult@rightbox
\rejustifybox{\hskip 0pt \@plus 1filll\unhbox\z@\unskip}%
\advance\count@4
\rejustifybox{\hskip 0pt \@plus 1filll\unhbox\z@\unskip\hskip 0pt \@plus 1filll}%
}

\def\rejustifybox#1{%
\@tempdimb\ht\count@
\global\setbox\@ne\vbox{}%
\setbox\z@\vbox{\hbox{\vrule depth 123sp} \unvbox\count@
\loop
\setbox\z@\lastbox
\@tempskipa\lastskip\unskip
\@tempcnta\lastpenalty\unpenalty
\ifdim\dp\z@=123sp
\else
\global\setbox\@ne\vbox{%
   \penalty\@tempcnta
   \vskip\@tempskipa
   \ifvoid\z@\else
   \hbox to \wd\z@{#1}%
    \fi
   \unvbox\@ne}%
\repeat
\global\setbox\count@\box\@ne
\global\ht\count@\@tempdimb
}}

\makeatother

\def\a{One two three four five six seven. }
\def\b{\a Red green blue yellow. }
\def\c{\a\b\a\b}
\def\x{Oxford\par London\par Manchester\par Birmingham\par Nottingham\par Liverpool\par Cambridge\par}

\begin{document}

\begin{multicols}{3}\raggedright
1\c\c\par
2 Text goes here. \c\a\c\par
3 \x\x\x\x\x\x\x\x\x
\end{multicols}

\end{document}
Related Question