# Usage of \lastbox

As part of our LaTeX3 work, we provide a consistent interface to TeX programming through the expl3 package. We've recently been discussing the TeX primitive \lastbox, which is used to inspect or unpack whatever just came before in the typesetting.

From a scan over various packages that use \lastbox, its use appears to be restricted (entirely, it seems) to constructions like

\setbox\foo=\lastbox
... % do something with box \foo


That is, there's no need to provide access to the read-only \lastbox variable itself, only a method by which the last box can be assigned to a TeX box for further processing; something like

\box_set_to_last:N \foo
... % do something with box \foo
% no such thing as \lastbox or equivalent


We'd propose to only provide the expl3 programmer with this method and promote that any \lastbox related functionality use \box_set_to_last:N instead. Does this seem sensible? Contrariwise, do you know of an actual use for \lastbox that doesn't involve a \setbox first?

-
I'm rather inclined to say that no other use makes much sense. If you say \lastbox (not in outer vertical mode or in math mode where it's disallowed), all you get is nothing, if the last item is not a box; otherwise you get exactly that box, so nothing different from what you'd get without \lastbox. –  egreg Sep 13 '11 at 12:01
@egreg: I'd be inclined to follow you on that, but plain.tex uses \lastbox in \m@ketabbox without \setbox. (I don't understand what DEK is doing in those macros.) –  Bruno Le Floch Sep 13 '11 at 17:17
@Bruno: I've always wanted to study those macros; but they are rather frightening. :) –  egreg Sep 13 '11 at 17:54
@egreg: they are, but they are also the only case of usage of \lastbox without \setbox in my texlive install (modulo some copies of the same macros elsewhere, and some clearly erroneous \box\lastbox at various places). –  Bruno Le Floch Sep 14 '11 at 0:33

Investigating about the use of \lastbox it seems that it is always used as

\setbox\someboxregister=\lastbox


with one important exception: line 619 of plain.tex contains

\ifus@\unvbox\z@\lastbox\fi


as part of the magic macros for \tabalign (aka \+).

I'd say that providing a LaTeX3 interface for the most common use is a good thing to do. One can always rely on the (renamed) primitive, for uses such as Knuth's.

-

I found three aspects of \lastbox not covered by the \setbox\foo=\lastbox.

Firstly, I used \shipout\lastbox repeatedly after packing all pages of a document in one big \vbox{<firstpage><secondpage>...}, to reverse pages from now onwards

Secondly, \lastbox used in vertical mode will take the last box from the current list, and put it back, inserting the \baselineskip or \lineskip as relevant given the \prevdepth that was active above that box. This is perhaps what \lastbox is used for within Knuth's \tabalign macro. For instance, the following puts several \baselineskip glues between the two boxes.

\vbox{\hbox{}\hbox{}\lastbox\lastbox\lastbox\showlists}


In horizontal mode, something similar happens with the space factor: it is reset to 1000, as it should following a box. I don't see a use for that, but it is consistent.

\vbox{\hbox{}\spacefactor=999\lastbox\showthe\spacefactor}


I think we should remain away from those exotic uses of \lastbox in LaTeX3, at least until we find real uses. In all cases, \lastbox can be replaced by {\setbox0\lastbox\box0}.

-