\documentclass[a4paper]{article}


\title{How to make a~box disappear at a~line break}
\author{Marcin Woli\'nski}

\begin{document}
\maketitle
\thispagestyle{empty}

Let us consider the problem of marking spaces in a paragraph with some
symbol, like in the following example:

\begin{center}
  \begin{minipage}{9.5cm}
    \language\csname l@polish\endcsname
    \obeyspaces
\def {%
\setbox0\hbox{\textvisiblespace}%
\dimen0=\wd0\relax
\hskip0ptplus.3\dimen0%
\cleaders\box0\hskip\dimen0%
\hskip0ptplus.3\dimen0%
}
\def {%
\setbox0\hbox{$\cdot$}%
\dimen0=\wd0\relax
\hskip1ptplus2pt%
\cleaders\box0\hskip\dimen0%
\hskip1ptplus2pt%
}

Ten typowy testowy akapit tekstu daje przy okazji rodzaj filigranowego %
wysypu hodowli pieczarek w zielonym kasztanie regloryfikacji %
stanowisk ministerialnych i podsypanych minimalistom jako fetysz %
zaduchu studziennych barykad.%  aglomeracji fosforescencji luminazy %
% atraktywno-bajerywnej z dodatkiem glukozy i mineralnych bakterii %
% finansowych oraz gromadzenia idei atrakcyjnych pomp prasowych z okazji %
% rozpoczynania wegetacji takich istot jak wiolonczele, napoje bazaltowe %
% i gramatyka z okresu mezozoicznego z jej typowym sposobem oznajmiania %
% zachwytu nad bytem poprzez wycie i popiskiwanie o charakterystycznej %
% modulacji toniczno-barycznej z wyskokami w kierunku reglamentacji %
% zawartej immanentnie w bagnie.
\end{minipage}
\end{center}

The hard part is to make the symbol disappear when
such a ``space'' occurs at a line break.  We cannot use
\verb|\discretionary| for that purpose since we need the ``space''
to be stretchable to make justification possible.  Moreover we want to
be able to associate some penalty (e.g., 0) with our breakpoints other
than \verb|\(ex)hyphenpenalty|.

As it turns out any box can be made discardable by putting it into
\verb|\cleaders| to the exact width of the box in question.  According
to his rules \TeX\ will put exactly one copy of the box in the text.
So the construct will look exactly like the box itself but will behave
like a glob of glue.  In particular it will disappear at a line break.

Here are the macros used in the preceding passage:
\begin{verbatim}
\obeyspaces
\def {%
\setbox0\hbox{$\cdot$}%
\dimen0=\wd0\relax%
\hskip1ptplus2pt%
\cleaders\box0\hskip\dimen0%
\hskip1ptplus2pt%
}
\end{verbatim}
Stretchability is achieved with separate globs of glue not to allow
\TeX\ to insert more than one copy of the box in case of an
overstretched space.

Note that this trick can be used in vertical mode as well (e.g., to
separate paragraphs with some graphical element except the case when a
paragraph boundary occurs at a page break).  A discardable box can
have arbitrary complexity, it can include colour, EPS graphics, and so
on.

\end{document}

%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 
