Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to detect if \vbox is empty

54 views
Skip to first unread message

Agent Smith

unread,
Feb 4, 2011, 2:01:25 PM2/4/11
to
How can you detect if a \vbox is empty?

Ex.:

\showboxdepth=100
\newbox\somebox
\setbox\somebox\vbox{}
\showbox\somebox
% \somebox=\box16
% > \box16=
% \vbox(0.0+0.0)x0.0
\message{\string\somebox\space
\ifvbox\somebox is \else is not \fi a \string\vbox}
\bye

You can use \ifvbox for detecting that \somebox is a \vbox.
How to find out if that \vbox is empty?
(You can check width/height/depth of \somebox but these
lengths could be 0 while the \vbox is not empty.)

Thanks.

Heiko Oberdiek

unread,
Feb 4, 2011, 2:30:49 PM2/4/11
to
Agent Smith <Agent...@suremail.info> wrote:

Perhaps:
Open a new \vbox, add a \kern with length a,
then \unvbox your box and then check via
\lastkern if the length is a. Then do the
same with a \kern of different length b.
Both length a and b must not be equal
to 0pt. (Skips, penalties and even
other boxes also could be used for
this test.)

--
Heiko Oberdiek

GL

unread,
Feb 4, 2011, 2:54:49 PM2/4/11
to

Yes, "theoretically" an empty box is a void box, so:

box 0 is \ifvoid0 \else not\fi void

;-)

Heiko Oberdiek

unread,
Feb 4, 2011, 3:10:31 PM2/4/11
to
GL <goua...@gmail.com> wrote:

> Yes, "theoretically" an empty box is a void box, so:

In TeX "void" is different from "empty" regarding boxes.

--
Heiko Oberdiek

zappathustra

unread,
Feb 4, 2011, 3:10:24 PM2/4/11
to
Le 04/02/2011 20:01, Agent Smith a �crit :

\newbox\somebox

\directlua{%

local box = tex.box[\the\mybox]
if box then
if box.head then
% Box not empty.
else
% Box empty.
end
else
% Box not assigned.
end

}


Ok, you need LuaTeX to do that, but you'll come to it, don't you worry :)
(Until version I-can't-remember-which, 0.65 I think, replace "head" with
"list".)

Best,
Paul

Agent Smith

unread,
Feb 4, 2011, 4:04:22 PM2/4/11
to
On 4 Feb., 20:30, Heiko Oberdiek <heiko.oberd...@googlemail.com>
wrote:
> Agent Smith <AgentSm...@suremail.info> wrote:

> > How can you detect if a \vbox is empty?

> Perhaps:


> Open a new \vbox, add a \kern with length a,
> then \unvbox your box and then check via
> \lastkern if the length is a. Then do the
> same with a \kern of different length b.
> Both length a and b must not be equal
> to 0pt. (Skips, penalties and even
> other boxes also could be used for
> this test.)

Thanks for the reply.

Will this be reliable in case my \vbox contains
only other boxes whose width, height and depth
are set to 0pt ?
\unvboxing / \unvcopying the \vbox returns these
boxes but if I got it right, that won't affect \lastkern.

Heiko Oberdiek

unread,
Feb 4, 2011, 4:11:39 PM2/4/11
to
Agent Smith <Agent...@suremail.info> wrote:

> On 4 Feb., 20:30, Heiko Oberdiek <heiko.oberd...@googlemail.com>
> wrote:
> > Agent Smith <AgentSm...@suremail.info> wrote:
>
> > > How can you detect if a \vbox is empty?
>
> > Perhaps:
> > Open a new \vbox, add a \kern with length a,
> > then \unvbox your box and then check via
> > \lastkern if the length is a. Then do the
> > same with a \kern of different length b.
> > Both length a and b must not be equal
> > to 0pt. (Skips, penalties and even
> > other boxes also could be used for
> > this test.)
>

> Will this be reliable in case my \vbox contains
> only other boxes whose width, height and depth
> are set to 0pt ?
> \unvboxing / \unvcopying the \vbox returns these
> boxes but if I got it right, that won't affect \lastkern.

\setbox0=\vbox{\hbox{}}
\setbox2=\vbox{\kern123pt\unvcopy0 \showthe\lastkern}

\lastkern does not see the initial \kern, because of the box
in box 0. Thus \showthe\lastkern reports 0pt.
Therefore the initial \kern must not be 0pt.
And for the case, the first box contains \kern123pt
as last element, the same should be done
with a different value.

--
Heiko Oberdiek

Agent Smith

unread,
Feb 4, 2011, 4:17:47 PM2/4/11
to
On 4 Feb., 20:54, GL <gouail...@gmail.com> wrote:
> Le 04/02/2011 20:01, Agent Smith a écrit :

> > How can you detect if a \vbox is empty?

> >    \newbox\somebox
> >    \setbox\somebox\vbox{}

> Yes, "theoretically" an empty box is a void box, so:


>
>         box 0 is \ifvoid0 \else not\fi void

Distinguish "box-register" from "box".

\somebox above is a register which is not void/
not empty as it contains an empty \vbox.

I re-read large portions of the TeXbook last night
and although headache now bothers hell out of me
I know that "voidness of a box-_register_" can be
detected by \ifvoid.

But I don't want to know if a register is void.

I want to know how to reliably crank out the case
of a \vbox held as the only item in a - thus non-void -
box-register being empty.

David Kastrup

unread,
Feb 4, 2011, 4:20:30 PM2/4/11
to
Agent Smith <Agent...@suremail.info> writes:

\setbox0\vbox{\unvcopy\somebox\expandafter}\ifnum\lastnodetype<0 ...

I have a bad memory, maybe it is called something else but
\lastnodetype. You'll find it in the eTeX documentation.

--
David Kastrup
UKTUG FAQ: <URL:http://www.tex.ac.uk/cgi-bin/texfaq2html>

Donald Arseneau

unread,
Feb 4, 2011, 5:29:18 PM2/4/11
to
Agent Smith <Agent...@suremail.info> writes:

> How can you detect if a \vbox is empty?

As Heiko says, by checking for a signature \lastkern
(or \lastpenalty or \lastskip). Or, as David points out,
by using eTeX feature to test last node type in a similar
way.

I just want to add that there is a signature \prevdepth
at the beginning of a vertical list, which could also
be tested for...

\setbox\junkbox\vbox{%
\unvcopy\questionbox
\expandafter}%% no eol token
\ifdim\prevdepth=-1000pt questionbox is empty\fi

It is possible to trick this method, so it isn't totally
reliable, but a material depth of -1000pt won't happen
accidentally in natural circumstances.


--
Donald Arseneau as...@triumf.ca

Agent Smith

unread,
Feb 4, 2011, 5:35:42 PM2/4/11
to
On 4 Feb., 21:10, zappathustra <zappathus...@free.fr> wrote:

> Ok, you need LuaTeX to do that, but you'll come to it, don't you worry :)

Eh - You really think so?

I was born in 1917. I ceased using TeX in 1992.

As some people passed away unexpectedly, the
need arose to revise my will which is the reason
why I came back to using TeX.

Getting me nowadays a computer which I can
still operate, was already a task of an unusually
difficult order. ;->>

The question about detecting if a \vbox is empty
actually is sort of a moot point.
I'm asking because I fear that otherwise I might
die of curiosity. ;->>

Looks like the code provided by you has nothing
to do with that newfangled Lamport-thingie dating
back to the early eighties but needs to be processed
by a program entirely different from TeX.

I suppose, this is what the "Lua" in the name
"LuaTeX" is about. Is there a book or a
PostScript file or the like available where I can
learn more about this "LuaTeX"?

zappathustra

unread,
Feb 4, 2011, 6:15:25 PM2/4/11
to
Le 04/02/2011 23:35, Agent Smith a �crit :

> On 4 Feb., 21:10, zappathustra<zappathus...@free.fr> wrote:
>
>> Ok, you need LuaTeX to do that, but you'll come to it, don't you worry :)
> Eh - You really think so?

Since the first sentence below is ``I was born in 1917,'' and the last
is ``Is there a book...?'', and in the meanwhile you mention ``dying of
curiosity,'' yes, I really do.

> I was born in 1917. I ceased using TeX in 1992.
>
> As some people passed away unexpectedly, the
> need arose to revise my will which is the reason
> why I came back to using TeX.

At 93, after an eighteen year gap, he looks at his computer screen and
says: ``Now, TeX, you might have beaten me in the past, but now I'll
impose you my will.'' After all, he was already a man when TeX's creator
was born. ``Watch your vboxes, kiddie.''

> Getting me nowadays a computer which I can
> still operate, was already a task of an unusually
> difficult order. ;->>
>
> The question about detecting if a \vbox is empty
> actually is sort of a moot point.
> I'm asking because I fear that otherwise I might
> die of curiosity. ;->>

Which, if you allow me some black humor (simply echoing your own, as far
as I can say), would be somewhat ironical, given the circumstances.

> Looks like the code provided by you has nothing
> to do with that newfangled Lamport-thingie dating
> back to the early eighties but needs to be processed
> by a program entirely different from TeX.
>
> I suppose, this is what the "Lua" in the name
> "LuaTeX" is about. Is there a book or a
> PostScript file or the like available where I can
> learn more about this "LuaTeX"?

Indeed, LuaTeX is Lua + TeX (and many other fascinating features), Lua
being the programming language in which the snippet I gave was written.
It is different in that respect, but otherwise it's TeX all the way down.
As for learning about it, unfortunately it's being developed, so
documentation isn't exactly its forte. You can find interesting
documents here, including the rather technical reference:
http://www.luatex.org/documentation.html

(By the way, the Lamport thing was LaTeX, not TeX; and judging from your
code, you're not using LaTeX, but plain TeX.)

Best,
Paul

GL

unread,
Feb 4, 2011, 6:49:32 PM2/4/11
to
Le 04/02/2011 21:10, Heiko Oberdiek a �crit :

> GL<goua...@gmail.com> wrote:
>
>> Yes, "theoretically" an empty box is a void box, so:
>
> In TeX "void" is different from "empty" regarding boxes.

Does it make sense ? I mean, what is an "empty box" ?

\null is an empty box ? and \hbox to0pt{\hss} is empty ?

Is \unhbox\null exactly the same as \leavevmode ?

Many questions...

Heiko Oberdiek

unread,
Feb 4, 2011, 8:03:42 PM2/4/11
to
David Kastrup <d...@gnu.org> wrote:

> Agent Smith <Agent...@suremail.info> writes:
>
> > How can you detect if a \vbox is empty?

> \setbox0\vbox{\unvcopy\somebox\expandafter}\ifnum\lastnodetype<0 ...


>
> I have a bad memory, maybe it is called something else but
> \lastnodetype. You'll find it in the eTeX documentation.

Good memory.

Now I have implemented the test in package ltxcmds 2011/02/04 v1.16:
http://www.informatik.uni-freiburg.de/~oberdiek/tmp/ltxcmds.pdf
(CTAN release of my bundle this month hopefully.)

Makros: \ltx@IfBoxEmpty{<box number>}{<yes>}{<no>}
and \ltx@IfBoxVoidOrEmpty{<box number>}{<yes>}{<no>}

First I test the obvious (\ifvoid, \ifdim\wd0, ...),
then the expensive tests follows. In case of e-TeX,
Davids' method via \lastnodetype is used, otherwise
the signature method using \penalty.

--
Heiko Oberdiek

Heiko Oberdiek

unread,
Feb 4, 2011, 8:03:41 PM2/4/11
to
GL <goua...@gmail.com> wrote:

> Le 04/02/2011 21:10, Heiko Oberdiek a écrit :
> > GL<goua...@gmail.com> wrote:
> >
> >> Yes, "theoretically" an empty box is a void box, so:
> >
> > In TeX "void" is different from "empty" regarding boxes.
>
> Does it make sense ? I mean, what is an "empty box" ?

A box without contents.

In case of \ifvoid there isn't even a box.

> \null is an empty box ?

Yes, it's defined as \hbox{}.

> and \hbox to0pt{\hss} is empty ?

No, the box contains glue:

\catcode`\{=1
\catcode`\}=2
\showboxdepth=10000 %
\showboxbreadth=10000 %
\tracingonline=1 %

\setbox0=\hbox to 0pt{\hss}
\showbox0

\csname@end\endcsname

> Is \unhbox\null exactly the same as \leavevmode ?

I thing you mean something like
\setbox0=\null
\unhbox0 %

Yes.

--
Heiko Oberdiek

GL

unread,
Feb 5, 2011, 4:19:31 AM2/5/11
to
Le 05/02/2011 02:03, Heiko Oberdiek a écrit :
> David Kastrup<d...@gnu.org> wrote:
>
>> Agent Smith<Agent...@suremail.info> writes:
>>
>>> How can you detect if a \vbox is empty?
>
>> \setbox0\vbox{\unvcopy\somebox\expandafter}\ifnum\lastnodetype<0 ...
>>
>> I have a bad memory, maybe it is called something else but
>> \lastnodetype. You'll find it in the eTeX documentation.
>
> Good memory.
>
> Now I have implemented the test in package ltxcmds 2011/02/04 v1.16:
> http://www.informatik.uni-freiburg.de/~oberdiek/tmp/ltxcmds.pdf
> (CTAN release of my bundle this month hopefully.)

Great ! Thanks ! I wonder if \lastnodetype was of any use, and now
am convinced.

Yours sincerely.

> Makros: \ltx@IfBoxEmpty{<box number>}{<yes>}{<no>}
> and \ltx@IfBoxVoidOrEmpty{<box number>}{<yes>}{<no>}
>
> First I test the obvious (\ifvoid, \ifdim\wd0, ...),

Just a point: \setbox0=\null
\setbox1=\null \wd1 = 3 in
\setbox2=\null \ht2 = -3 in

box 0 is empty, 1 is not, but \unhbox0 \unhbox 1 and
\unhbox 2 are the same... while \box0 \box1 and \box2 not.

I would say -- naturally speaking -- that box 1 and 2 are empty too...
For they don't have any content.

Minimal example:

%-----------------------------------------
\documentclass{article}
\begin{document}

\setbox0=\null
\setbox1=\null \wd1 = 1 in
\setbox2=\null \wd2 = -1 in


{\parindent=2in
the following \copy0 follows \qquad the following \unhcopy0 follows

the following \copy1 follows \qquad the following \unhcopy1 follows

the following \copy2 follows \qquad the following \unhcopy2 follows

}

\end{document}\endinput
% -------------------------------------------------------

Yours sincerely.

Heiko Oberdiek

unread,
Feb 5, 2011, 5:39:37 AM2/5/11
to
GL <goua...@gmail.com> wrote:

> Le 05/02/2011 02:03, Heiko Oberdiek a écrit :

> > Makros: \ltx@IfBoxEmpty{<box number>}{<yes>}{<no>}
> > and \ltx@IfBoxVoidOrEmpty{<box number>}{<yes>}{<no>}
> >
> > First I test the obvious (\ifvoid, \ifdim\wd0, ...),
>
> Just a point: \setbox0=\null
> \setbox1=\null \wd1 = 3 in
> \setbox2=\null \ht2 = -3 in
>
> box 0 is empty, 1 is not, but \unhbox0 \unhbox 1 and
> \unhbox 2 are the same... while \box0 \box1 and \box2 not.
>
> I would say -- naturally speaking -- that box 1 and 2 are empty too...
> For they don't have any content.

Good point, I will remove the tests for \wd/\ht/dp.

--
Heiko Oberdiek

David Kastrup

unread,
Feb 5, 2011, 5:51:28 AM2/5/11
to
Donald Arseneau <as...@triumf.ca> writes:

> Agent Smith <Agent...@suremail.info> writes:
>
>> How can you detect if a \vbox is empty?
>
> As Heiko says, by checking for a signature \lastkern
> (or \lastpenalty or \lastskip). Or, as David points out,
> by using eTeX feature to test last node type in a similar
> way.
>
> I just want to add that there is a signature \prevdepth
> at the beginning of a vertical list, which could also
> be tested for...
>
> \setbox\junkbox\vbox{%
> \unvcopy\questionbox
> \expandafter}%% no eol token
> \ifdim\prevdepth=-1000pt questionbox is empty\fi
>
> It is possible to trick this method, so it isn't totally
> reliable,

In fact, it is 100% unreliable since \unvcopy does not change
\prevdepth.

> but a material depth of -1000pt won't happen accidentally in natural
> circumstances.

A \hrule is not natural?

Donald Arseneau

unread,
Feb 5, 2011, 6:20:48 PM2/5/11
to
David Kastrup <d...@gnu.org> writes:

> Donald Arseneau <as...@triumf.ca> writes:
> > \ifdim\prevdepth=-1000pt questionbox is empty\fi

> In fact, it is 100% unreliable since \unvcopy does not change
> \prevdepth.


Oops! of course.


--
Donald Arseneau as...@triumf.ca

0 new messages