[comp.text.tex] Need non-fixed-width parbox in LaTeX

garyb@abekrd.UUCP (Gary Bartlett) (12/11/90)

I need to format a piece of text in a parbox (because I need to specify
where the line breaks are to occur) but I want the final box to be only as
wide as the largest line of text.  I cannot figure out how to achieve this.

eg - the text to be formatted:

	this is line 1 \\
	this is a longer line 2 \\
	the end

I will always specify where the line breaks are to occur so I don't need
LaTeX to split the lines for me. Theoretically therefore, I don't need to
specify a width. Unfortunately, \parbox demands a width. I don't know the
width of the longest line before hand either. Can anyone see a way around
this?

Ultimately, I would like to produce the following:

	+---------------------------------------------+
	|                                             |
	|                                             |
	|                                             |
	|                                             |
	|           this is line 1                    |
	|           this is a longer line 2           |
	|           the end                           |
	|                                             |
	|                                             |
	|                                             |
	|                                             |
	+---------------------------------------------+

where my specified text is CENTERED in a surrounding box of a given size
(and position).  Any ideas?

Thanks in advance,
Gary
-- 
---------------------------------------------------------------------------
Gary C. Bartlett               NET: garyb@abekrd.co.uk
Abekas Video Systems Ltd.     UUCP: ...!uunet!mcsun!ukc!pyrltd!abekrd!garyb
12 Portman Rd,   Reading,    PHONE: +44 734 585421
Berkshire.       RG3 1EA.      FAX: +44 734 567904
United Kingdom.              TELEX: 847579

dhosek@mimir.claremont.edu (Don Hosek) (12/12/90)

In article <1568@abekrd.UUCP>, garyb@abekrd.UUCP (Gary Bartlett) writes:
> I need to format a piece of text in a parbox (because I need to specify
> where the line breaks are to occur) but I want the final box to be only as
> wide as the largest line of text.  I cannot figure out how to achieve this.
 
> eg - the text to be formatted:
 
> 	this is line 1 \\
> 	this is a longer line 2 \\
> 	the end
 
> I will always specify where the line breaks are to occur so I don't need
> LaTeX to split the lines for me. Theoretically therefore, I don't need to
> specify a width. Unfortunately, \parbox demands a width. I don't know the
> width of the longest line before hand either. Can anyone see a way around
> this?

\newenvironment{centerlongestline}{\begin{center}\begin{minipage}{\linewidth}
   \begin{tabbing}}{\end{tabbing}\end{minipage}\end{center}}

See the LaTeX manual under the tabbing environment for an
explanation of why this works.

Another alternative would be to use \begin{tabular}{l} ...
\end{tabular} rather than a tabbing environment, in which case
the minipage environment could be omitted entirely.

-dh

sommer@BINAH.CC.BRANDEIS.EDU (12/12/90)

In article <1568@abekrd.UUCP>, garyb@abekrd.UUCP (Gary Bartlett) writes:
>I need to format a piece of text in a parbox (because I need to specify
>where the line breaks are to occur) but I want the final box to be only as
>wide as the largest line of text.  I cannot figure out how to achieve this.
>
>eg - the text to be formatted:
>
>	this is line 1 \\
>	this is a longer line 2 \\
>	the end
>
>I will always specify where the line breaks are to occur so I don't need
>LaTeX to split the lines for me. Theoretically therefore, I don't need to
>specify a width. Unfortunately, \parbox demands a width. I don't know the
>width of the longest line before hand either. Can anyone see a way around
>this?

Don Hosek posted two different solutions. I'm only a LaTeX amateur, but I
think this will work, too (see LaTeX manual, p. 95):

\newlength{\boxwidth} % or whatever you want to call it

\settowidth{\boxwidth}{this is a longer line 2} % your longest line

\parbox{\boxwidth}{this is line 1 \\ this is a longer line 2 \\ the end}

I think it should work with centering, etc., thrown in, too. 

klute@tommy.informatik.uni-dortmund.de (Rainer Klute) (12/12/90)

In article <1568@abekrd.UUCP>, garyb@abekrd.UUCP (Gary Bartlett) writes:
|> I need to format a piece of text in a parbox (because I need to specify
|> where the line breaks are to occur) but I want the final box to be only as
|> wide as the largest line of text.  I cannot figure out how to achieve this.
|> 
|> eg - the text to be formatted:
|> 
|> 	this is line 1 \\
|> 	this is a longer line 2 \\
|> 	the end

Not really a solution, but an ugly hack that works:

\newlength{\longestline}
\settowidth{\longestline}{this is a longer line 2}
\parbox{\longestline}{
this is line 1 \\
this is a longer line 2 \\
the end
}

The problem is that you have to find out which of your line is the longest
one and then paste it into the \settowidth command. This task must be
carried out by running latex at least once over your document.

If someone has a better idea please let me know!

-- 
  Dipl.-Inform. Rainer Klute      klute@irb.informatik.uni-dortmund.de
  Univ. Dortmund, IRB             klute@unido.uucp, klute@unido.bitnet
  Postfach 500500         |)|/    Tel.: +49 231 755-4663
D-4600 Dortmund 50        |\|\    Fax : +49 231 755-2386