5

Given some text, I want to centrally align this text on a given amount of lines.

For example,

\documentclass{article}
\begin{document}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna. 
\end{document}

gives enter image description here However, I would like to advise LaTeX to distributed this text among 12 lines, whereby all lines (including the last one) have roughly the same width. Additionally, the output should be centered.

10

enter image description here

\documentclass{article}
\usepackage[latin]{babel}
\begin{document}
\sbox0{\indent Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna.}
\begin{center}
\dimen0=\dimexpr\wd0/12\relax
\loop
\sbox2{\parbox[b]{\dimen0}{\parfillskip=0pt plus .1\hsize\unhcopy0}}
\ifdim\ht2>12\baselineskip
\advance\dimen0 1em
\repeat
\usebox2
\end{center}
\end{document}

If you change to

\documentclass{article}
\usepackage[latin]{babel}
\begin{document}
\sbox0{Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna.}
\begin{center}
\dimen0=\dimexpr\wd0/12\relax
\loop
\sbox2{\parbox[b]{\dimen0}{\centering\parfillskip=0pt \unhcopy0}}
\ifdim\ht2>12\baselineskip
\advance\dimen0 .5em
\repeat
\usebox2
\end{center}
\end{document}

Then you get

enter image description here

  • lines don't have the same with – touhami Mar 4 '15 at 15:17
  • 1
    @touhami you mean the last line? I gave a bit of flexibility there, delete plus .1\hsize if that is not wanted. – David Carlisle Mar 4 '15 at 15:19
  • Sorry if this was not clear. Is it possible to not only center the box, but also the individual lines? Further, how can I get rid of the indentation on the first line? – Florian Mar 4 '15 at 15:19
  • Very nice. The use of iteration to achieve the text-fit reminds me of the related problem: tex.stackexchange.com/questions/123614/… – Steven B. Segletes Mar 4 '15 at 15:19
  • @Florian Just add \centering before \parfillskip. – Steven B. Segletes Mar 4 '15 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.