# 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}.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8865438103675842, "perplexity": 2582.7422207197524}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2016-22/segments/1464049277091.36/warc/CC-MAIN-20160524002117-00168-ip-10-185-217-139.ec2.internal.warc.gz"}