2

I'm trying to make a box where the line breaks are such that each line of text in the box is roughly equal length.

For example:

\documentclass[11pt]{article}
\begin{document}
words words words words words words words words words words words words words words 
\end{document}

Would print "words" 11 times on the first line, but 3 times on the second line.

I would like it so that instead it printed "words" 7 times on each line without having to manually add line breaks.

The objective would be to make the amount of wasted space in the box as little as possible.

The application I need this for is that I have a 2 column tabularx and I want the second column to look as nice as possible.

\documentclass[11pt]{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\textwidth}{ l X }
\bf Word & \bf Definition\\
\hline
\bf Apple & The usually round, red or yellow, edible fruit of a small tree, Malus sylvestris, of the rose family.\\
\hline
\bf Orange & A globose, reddish-yellow, bitter or sweet, edible citrus fruit.\\
\hline
\bf Banana & A tropical plant of the genus Musa, certain species of which are cultivated for their nutritious fruit.
\end{tabularx}
\end{document}

Here is how I would like it to look using manual line breaks:

\documentclass[11pt]{article}
\usepackage{tabularx}
\begin{document}
\begin{tabularx}{\textwidth}{ l X }
\bf Word & \bf Definition\\
\hline
\bf Apple & The usually round, red or yellow, edible fruit of\\& a small tree, Malus sylvestris, of the rose family.\\
\hline
\bf Orange & A globose, reddish-yellow, bitter or sweet, edible citrus fruit.\\
\hline
\bf Banana & A tropical plant of the genus Musa, certain species\\& of which are cultivated for their nutritious fruit.
\end{tabularx}
\end{document}

My current solution is to manually add line breaks, but its tedious and I feel that there is a way to do this automatically.

So does anyone have a good way to do this?

4
  • 1
    You should be more specific about "[minimising the] amount of wasted space in the box". For example, a line of 14 words could be made into a 5-5-4 breakout.
    – Werner
    Jun 20, 2017 at 18:20
  • I believe this question has been asked and answered before on this site. I'm thinking David had the award-winning answer, but I could be wrong on that. Jun 20, 2017 at 18:24
  • 1
    If I took your original example and coded it to break into two equal lines they would both be a lot less than \textwidth but such a linebreaking is no use for your tabularx example as there the column width is pre-determined as text width minus width of first column, so if the line breaking uses short lines it will look very odd, especially if that length was different in each cell. Jun 20, 2017 at 19:01
  • unrelated but do not use \bf in latex. Jun 20, 2017 at 19:02

1 Answer 1

5

This tries to find the smallest width that gives the same number of lines as the full width paragraph.

enter image description here

\documentclass[11pt]{article}
\usepackage{tabularx}
\begin{document}

% a couple of counters used in the tests below.
\newcount\zzc
\newcount\zzzc

%\zz{some text} is the main (only) macro defined.

\def\zz#1{{%
% the macro will set the text several times, 
% will be very poor typographically
% so turn off all over/under full box warnings during tests
\hbadness\maxdimen\hfuzz\maxdimen
%
% first set the box at natural width with `\parfillskip` 0pt
% so the last line is full width, use \sloppy so the white space
% stretches. this box may look horrible (if you looked) eg 
% \zz{hello world}
% would have hello at the left and world at the far right and a big
% gap in the middle.
% however save the number of lines (\prevgraf) that the paragraph took.
\setbox0\vbox{%
\sloppy \parfillskip 0pt
#1\par
\global\zzc\prevgraf}%
%
% Now set the same text in smaller and smaller widths 
% reducing in steps of 5pt
\dimen0\hsize
\loop
\advance\dimen0 -5pt
\setbox0\vbox{%
\hsize\dimen0
\sloppy \parfillskip 0pt
#1\par
\global\zzzc\prevgraf}%
%
% The number of lines of this narrower paragraph is in zzzc
% if that is same as the original, we have less white space
% which is good. If if it is more than the original we have
% over-squeezed the paragraph so it is too narrow so taking more 
% lines.
\ifnum\zzzc>\zzc\else
\repeat
%
% now the loop stops so \dimen0 is the first "bad" width
% so do the final setting with 5pt more than that which is
% the last good width with the tightest setting found
\noindent\strut\vtop{\hsize=\dimexpr\dimen0+5pt\relax
\sloppy \parfillskip 0pt
#1}}}


% example use in a table, as requested.
\noindent
\begin{tabularx}{\textwidth}{ lX }
\bfseries Word & \bfseries Definition\\
\hline
\bfseries Apple & \zz{The usually round, red or yellow, edible fruit of
 a small tree, Malus sylvestris, of the rose family.}\\
\hline
\bfseries Orange & \zz{A globose, reddish-yellow, bitter or sweet, edible citrus fruit.}\\
\hline
\bfseries Banana &\zz{A tropical plant of the genus Musa, certain species
 of which are cultivated for their nutritious fruit.}
\end{tabularx}
\end{document}
4
  • I knew when I heard the bu\zzz of the macro-gnats that your answer would not be far behind. Jun 21, 2017 at 10:57
  • Would you mind explaining what is going on in the macro?
    – edggy
    Jun 21, 2017 at 20:10
  • @edggy I added some comments Jun 21, 2017 at 20:22
  • @DavidCarlisle Thanks, it's easier to learn what's going on than seeing a big blob of LaTeX magic
    – edggy
    Jun 21, 2017 at 20:26

You must log in to answer this question.

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