# How to set paragraph width as the width of the longest line

I have a paragraph with several lines ended using \break. I would like to set the width of the whole paragraph to the width of the longest line. How to achieve this?

I have a macro like this where I early changed \\ into \break, I was recommended to use \haling and \cr but I can't see how to integrate it.

\everypar={\setbox0=\lastbox\endgraf\vbox\bgroup\everypar={\vrule height12pt width0pt}}
\def\par{\ifhmode\endgraf\egroup\fi}
\def\\{\unskip\break}
\rightskip=0pt plus 1fil\parindent=0pt #2\par
\everypar={}


This does not work:

\everypar={\setbox0=\lastbox\endgraf\vbox\bgroup\everypar={\vrule height12pt width0pt}}
\def\par{\ifhmode\endgraf\egroup\fi}
\def\\{\cr}
\rightskip=0pt plus 1fil\parindent=0pt \haling{#2}\par
\everypar={}

• Can you post a full example, including the paragraph you're trying to typeset? In your code sample you have "haling" instead of "halign". – ShreevatsaR Jan 27 at 3:29

TeX has the \halign primitive for this (this question is tagged “plain-tex”, isn’t it?). Instead of \break you have to use \cr; a little drawback is that you have to request the paragraph indentation explicitly (with \indent) if you want it:

\halign{#\cr
\indent Lorem ipsum dolor sit amet, consectetur adipiscing elit.\cr
Curabitur massa turpis, semper quis fringilla ut, viverra nec risus.\cr
Pellentesque habitant morbi tristique\cr
senectus et netus et malesuada fames ac turpis egestas.\cr
}

More text that is set at the normal text width, with line breaks chosen by
\TeX's line breaking algorithm.  For example, a line break should have occurred
at the beginning of this sentence.

\bye


Output:

Of course, it is easy to define “syntactic sugar” to make this more user-friendly. Let me repeat that \halign is a primitive command of TeX, so it is much more efficient than any macro-based solution. Moreover, it is worth remarking that \halign contributes the individual lines of the “‘Lorem ipsum…’ paragraph” to the main vertical list (as it happens with normal paragraphs), entailing that page breaks are allowed in the usual way; also the interline glue is administered as it normally is.

• This seems that it could solve all of my problems, I am just not sure how to integrate it with my complicated macro. Basically, I have a macro in which I was replacing \\ with \break, edited the question. – Alexander Weps Jan 26 at 20:51
• I am getting: Incompatible list can't be unboxed after I encapsulated my text in \halign and replaced \break with \cr. – Alexander Weps Jan 26 at 21:56
• @AlexanderWeps: I cannot offer any suggestion on how to integrate \halign into your “complicated macro” unless you show us this macro and you intend to use it. More generally, it is not at all clear, from your question, what you are trying to achieve: could you explain it in detail, maybe with the help of a hand-made drawing? – GuM Jan 27 at 12:02

You could split everything at \break, typeset each line at natural width to determine the longest and then against typeset at maximum width.

\newdimen\maxwidth
\newbox\linebox

\foreachline\measure#1\break\break\endforeachline
\ifdim\maxwidth>\hsize
\vbox{#1}
\else
\fi
}

\def\measure#1{%
\setbox\linebox=\hbox{#1}
\ifdim\wd\linebox>\maxwidth
\maxwidth=\wd\linebox
\fi}

\def\foreachline#1#2\break#3\endforeachline{%
#1{#2}%
\ifx#3\break\else
\foreachline#1#3\endforeachline
\fi
}

\def\endforeachline{}

Lorem ipsum dolor sit amet, consectetur adipiscing elit.\break
Curabitur massa turpis, semper quis fringilla ut, viverra nec risus.\break
Pellentesque habitant morbi tristique\break
senectus et netus et malesuada fames ac turpis egestas.\break
}

\bye


If the longest line is longer than \hsize

• This work best so far, but does not handle paragraphs. – Alexander Weps Jan 30 at 2:38
• @AlexanderWeps Can you be a bit more specific? What do you mean with “it does not handle paragraphs”? – Henri Menke Jan 30 at 2:51
• Just put \par or two new lines inside \adjustlines. – Alexander Weps Feb 6 at 13:29