[texhax] indent for last line of a paragraph

Oleg Katsitadze olegkat at gmail.com
Sat Oct 7 21:44:21 CEST 2006


On Sat, Oct 07, 2006 at 06:04:19PM +0200, martin böhnert wrote:
> i would like to have a paragraph where the last line has an indentation. 
> this should only apply if the paragraph is longer than one line.

One solution is to read in the paragraph from \everypar,
test-typeset it to count how many lines it has, and then set
up \hangindent and \hangafter accordingly.  Here is a quick
implementation (in plain TeX):

------------------------------%<------------------------------
\newcount\linesnoindent
\newcount\linesindent

\def\lastparindent{4em\relax}

\def\readpar#1\par{%
  \begingroup
    \everypar={}% Reset to avoid recursion.
    % Count number of lines with the last line unindented.
    \setbox0=\vbox{%
      #1\par
      \global\linesnoindent=\prevgraf
    }%
    % Only do tricks if there're more than one line.
    \ifnum \linesnoindent > 1
      % Count number of lines with the last line indented.
      \setbox0=\vbox{%
        \hangindent=\lastparindent
        \hangafter=\linesnoindent \advance\hangafter by -1
        #1\par
        \global\linesindent=\prevgraf
      }%
      % If indenting the last line increases number of lines, we try
      % to squeeze the paragraph into one line less.  But this only
      % works when the paragraph is long enough, or the split-off part
      % is short enough.
      \ifnum \linesnoindent < \linesindent
        \looseness = -1
      \fi
      % Set up the last line indentation.
      \hangindent=\lastparindent
      \hangafter=\linesnoindent \advance\hangafter by -1
    \fi
    % Typeset the paragraph.
    #1\par
  \endgroup
}


\everypar={\readpar}
\parindent=0pt

\hsize=4in

i would like to have a paragraph where the last line has an

\bigskip

i would like to have a paragraph where the last line has an
indentation.  this should only apply if the paragraph is longer than
one line.  the result should look like this:

\bigskip

i would like to have a paragraph where the last line has an
indentation.  this should only apply if the paragraph is longer than
one line.  the result should look like this: adf af as df afa fad a a

\bye
------------------------------%<------------------------------

The problem with such indentation is that there are
situations when indenting the last line makes the last line
too wide, so that it splits into two lines.  What are we
supposed to do then?

The above code tries to get rid of the extra line by setting
\looseness=-1 -- try commenting out the \looseness setting
and see what happens to the third paragraph.  But this is
not guaranteed to always work.  When it doesn't, there's
probably nothing you can do but rewrite the paragraph to
avoid such an unfortunate line break.

Hope this helps,
Oleg

P.S.  A simpler approach might be ripping out \lastbox of
the paragraph and putting it back indented, but here you
won't be able to take advantage of \looseness.



More information about the texhax mailing list