% Copyright (c) 2005 Jonathan Fine <jfine@pytex.org>
% License: GPL version 2 or (at your option) any later version.
% $Source: /cvsroot/pytex/pytex/texmacros/outerhbox.sty,v $

% $Log: outerhbox.sty,v $
% Revision 1.2  2005/10/06 07:00:19  jfine
% Added description, as provided to CTAN
%
% Revision 1.1  2005/10/06 06:32:18  jfine
% As posted to comp.text.tex, but \outerhbox instead of \outerbox

% Collects horizontal mode material into an \hbox, suitable for later
% \unhbox'ing into a paragraph.  For use with plain, LaTeX, ConTeXt, etc.

% Provides \outerhbox, which is roughly similar to \hbox, except that
% material is set in outer horizontal mode.  This prevents TeX from
% optimising away math penalties and the like, that are needed when the
% material is \unhbox'ed.

% Based on code I posted to comp.text.tex 

% Message-ID: <43399D5A.50803@pytex.org>
% Date: Tue, 27 Sep 2005 20:28:26 +0100
% Newsgroups: comp.text.tex
% Subject: Re: overlong lines in List of Figures
%  <http://groups.google.com/group/comp.text.tex/msg/56ea490ad6d13dae>

\ifx\undefined\outerhbox
\else\endinput\fi

\ifx \documentstyle \undefined \else
  \ifx \documentclass \undefined
  \else \ProvidesPackage{outerhbox}[2005/09/29] \fi
\fi


\long\def\outerhbox #1%
% like \hbox, but sets material in outer horizontal mode
% #1 - horizontal material, to be set in outer horizontal mode
% Note: inappropriate commands in #1 can cause an \outerhboxerror
{%
   \hbox{%
     \setbox0 \vbox{%
       \hsize\maxdimen
       \pretolerance -1
       \rightskip 0pt
       \parfillskip 0pt plus 1fil
       \noindent #1\endgraf
       \global\setbox1 \lastbox
     }%
     % check that size of \box 0 is 0.0+0.0x0.0
     \ifcase
       % skip to \else if any dimension is non-zero
       \ifcase\wd0 \else 1 \fi
       \ifcase\ht0 \else 1 \fi
       \ifcase\dp0 \else 1 \fi
       0 % satisfy the \ifcase
       %
       % place material into \hbox
       \unhbox 1
       \unskip % remove \rightskip glue
       \unskip % remove \parfillskip glue
       \unpenalty % remove paragraph ending \penalty 10000
     \else
       % there's something left in \box 0
       \outerhboxerror % default handler is to be \undefined
     \fi
   }%
}

% \endinut % some test code follows

\tracingall
\tracingonline = 1
% \nonstopmode

\setbox 0 \hbox{$2+2=2$} \showbox 0
\setbox 0 \outerhbox{$2+2=2$} \showbox 0
{ \leftskip 1in
  \setbox 0 \outerhbox{$2+2=2$} \showbox 0
}

\setbox 0 \outerhbox{a\par b} \showbox 0
\setbox 0 \outerhbox{} \showbox 0
\setbox 0 \outerhbox{\ } \showbox 0
\end
