1

I am generating a bunch of documents with centered titles of varying lengths. Sometimes the titles run onto a second line. When they do so, I would rather have the line break occur somewhere in the middle so that the two lines have approximately equal lengths. But, as far as I can tell, LaTeX does not allow this because it results in underfull hboxes.

Is there a way to dynamically change the width of the hbox to be as narrow as possible for a given number of lines (e.g. once there is a second line, reduce the hbox as much as possible without creating a third line)?

Note: This is a different problem than the one discussed in "How can I make (La)TeX prefer to fill centered lines" because this is about changing the width of the lines to be filled rather than having those widths decrease from line to line. I also tried the varwidth package without success.

\documentclass{article}

\begin{document} \begin{center} \LARGE \bfseries This is a short title.\ ~\ This is a longer one-line title.\ ~\ This is a longer title that runs onto two lines.\ ~\ % I would rather it look like this \begin{minipage}{3in} \centering This is a longer title that runs onto two lines.\ \end{minipage} \end{center} \end{document}

1 Answers1

0

I came up with a way of doing this. The following custom function determines how many lines the text will be spread across, divides the width of the text by that number of lines (to get even width lines), and puts the text in an appropriate width parbox.


\usepackage{xparse}
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\floor}{m}
 {
  \fp_eval:n { floor ( #1 ) }
 }
\ExplSyntaxOff

\newcommand{\compactcentering}[1]{ \newlength{\inputlength} \settowidth{\inputlength}{#1} \newcounter{breakerlines} \setcounter{breakerlines}{\floor{\inputlength / \textwidth}} \stepcounter{breakerlines} \parbox{{\inputlength / \value{breakerlines}}+ 2\fboxsep}{\center \noindent #1} }

  • There is a much better was of doing this at https://tex.stackexchange.com/questions/375963/automatically-minimizing-the-area-of-a-box – bbernicker Feb 04 '23 at 03:15