10

If I center the first sentence of lorem, traditional use of the center environment will result in text like so: lorem centered incorrectly

However, suppose I want to center the text such that the length of each line is equal.

lorem centered correctly

It is rather easy to include manual line breaks, but this is rather unsafe. If I update the font-size or other document parameters, the text is likely to not be split appropriately and need manual fixing. Is it possible to do this automatically?

2

2 Answers 2

12

The following centereq takes as an optional parameter the required fraction of the text width to fill (default 0.8) and makes lines that differ as much as 2em (one on either side) from the target width.

For a long enough paragraphs there should be no real problem in using whatever fraction you like. Short paragraph will need an educated guess.

\documentclass{article}
\usepackage{kantlipsum}

\newenvironment{centereq}[1][0.8]
 {%
  \trivlist
  \centering
  \setlength{\leftskip}{\dimexpr(\columnwidth-#1\columnwidth)/2\relax plus 1em}%
  \setlength{\rightskip}{\leftskip}%
  \item\relax
 }
 {\endtrivlist}

\begin{document}

\begin{centereq}
\kant[2]
\end{centereq}

\begin{centereq}[0.6]
\kant[2]
\end{centereq}

\begin{centereq}[1]
\kant[2]
\end{centereq}

\end{document}

enter image description here

8

Here I provide \centerlines[<delta length>]{<n>}{<content>} to assist in the process of setting content over n lines. It sets the content in an hbox, and divides the content width by n. It then creates a parbox of that width + delta length (default 5pt) and resets the content in that \parbox, which has been centered.

\documentclass{article}
\usepackage[showframe,pass]{geometry}
\usepackage{fp}
\makeatletter
\newcommand\centerlines[3][5pt]{%
  \sbox0{#3}%
  \FPdiv\result{\strip@pt\wd0}{#2}
  {\noindent\hfill
  \parbox{\dimexpr #1+\result pt}{\centering\strut#3\strut}%
  \hfill\mbox{}\par}
}
\makeatother
\begin{document}
\centerlines{2}{To be or not to be, that is the question}

\centerlines[9pt]{8}{When in the Course of human events, it becomes necessary
 for one people to dissolve the political bands which have connected them 
with another, and to assume among the powers of the earth, the separate 
and equal station to which the Laws of Nature and of Nature's God entitle 
them, a decent respect to the opinions of mankind requires that they 
should declare the causes which impel them to the separation.}
\end{document}

enter image description here


Here is a slightly different take on it. With this verion, a \parbox is not used. Rather, \leftskip nad \rightskip are employed, and so the result can break across pages. However, each line of the \centerlines output will be forced to the same width, and hyphenation is now active.

\documentclass{article}
\usepackage[showframe,pass]{geometry}
\usepackage{fp}
\makeatletter
\newcommand\centerlines[3][5pt]{%
  \sbox0{#3}%
  \FPdiv\result{\strip@pt\wd0}{#2}
  \FPdiv\theskip{\strip@pt\dimexpr\linewidth-\result pt-#1\relax}{2}%
  {\centering\leftskip\theskip pt\rightskip\theskip pt#3\par}
}
\makeatother
\begin{document}
\centerlines{2}{To be or not to be, that is the question}

\centerlines[1pt]{8}{When in the Course of human events, it becomes necessary
 for one people to dissolve the political bands which have connected them 
with another, and to assume among the powers of the earth, the separate 
and equal station to which the Laws of Nature and of Nature's God entitle 
them, a decent respect to the opinions of mankind requires that they 
should declare the causes which impel them to the separation.}

Back to normal
\end{document}

enter image description here

You must log in to answer this question.

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