Reliably trapping end of paragraph / return to vertical mode

David Carlisle d.p.carlisle at gmail.com
Wed Oct 2 22:29:13 CEST 2019


basically Phil you are doomed (unless you are tempted to try luatex which
has more possibilities)

you mention paragraphs ending with \par, \endgraf, a blank line or \vskip.

blank lines and vskip are Ok really as they cause the \par token to be
inserted, so if you have hooked some code into \par that will run.

\endgraf or other commands let to the primitive are Ok if you give yourself
access to the format early enough that you know that no one has taken
control.

But there are two cases that you really can't easily  trap.

In classic tex you can end a paragraph by ending a vbox, that does not
insert a \par token: it just directly ends the paragraph with no
macro-layer hook at all.

In pdftex (and xetex) a paragraph can be ended with \primitive\par which is
again pretty tricky to catch without breaking other uses of \primitive.

partial paragraphs ended with $$ also make things interesting.



On Wed, 2 Oct 2019 at 08:10, Taylor, P <P.Taylor at rhul.ac.uk> wrote:

> While working on the most recent iteration of my code for floating image
> insertion, I encountered a situation in which I was unable to determine the
> correct value of \prevgraf.  It transpired that this was because the
> previous paragraph had been terminated (via a macro) by \endgraf rather
> than with a blank line, explicit \par or \vskip.  Although Knuth provides
> \everypar, which can be reliably used to trap *entry *to a paragraph,
> there does not appear (to me, at least) to be any 100% reliable method of
> trapping exit therefrom, and of course once the next paragraph has been
> entered, the previous (and desired) value of \prevgraf has been lost.  I
> wonder if anyone has successfully addressed this problem, and found a
> reliable way of trapping paragraph exits, or equivalently, returns to
> vertical mode.  Re-defining \endgraf solves the trivial case, but of course
> one can never know what other synonyms for the primitive \par might have
> been created before one's own code is entered.
>
> *Philip Taylor*
> --------
> \count 0 = 0
>
> \everypar = {\advance \count 0 by 1 }
>
> \def \par {\primitive \par \message {para.\string ~\the \count 0: \string
> \prevgraf = \the \prevgraf;}}
>
> \leavevmode \par
>
> \leavevmode \vskip 0 pt
>
> \leavevmode \endgraf
>
> \leavevmode % only this line is followed by a blank line
>
> \end
>
>
> ->
>
> ( para.~1: \prevgraf= 1; para.~2: \prevgraf= 1; para.~4: \prevgraf= 1; [4]
> )
>
> i.e., no \prevgraf report for para.~3, the paragraph terminated by
> \endgraf.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://tug.org/pipermail/texhax/attachments/20191002/8ae68141/attachment.html>


More information about the texhax mailing list