Is it possible to test whether a token list register is empty without expanding it?
At time of writing the other TeX based answers on this page are flawed in that they hide a conditional
The LaTeX3 programming language expl3 contains a module for doing stuff with token registers:
It essentially does internally what the other answers here are suggesting, but it uses expansion to grab its arguments so the branching is robust (and you don't have
Update: So what does this approach do that is superior to other methods? Consider the style of solution first offered in answer to this question:
This doesn't behave nicely when nested, because TeX scans ahead when discarding unfollowed branches of a conditional. Consider
and hence the cause of the ‘Extra
So this form for conditionals doesn't work so well. Next try. You can also write this style of code like this:
This avoids the problems of nesting as in the previous trial solution, but it's prone to another problem:
so overcome this problem. This is how expl3 conditionals work, and it's why we're writing
Incidentally, there are expandable tests for checking for emptiness, which is why I suggest using the expl3 approach for this test. Expandability is not always required, of course, but code that is fully expandable tends to be more reliable and it's always nice to have for cases such as
With LuaTeX you can do this:
But since I never use token list: don't use this code to build nuclear plants.
Ulrich Diez regularly posts on
Or, without using e-TeX,
Would something like this work?
You could then use it like this:
Similar to Jan Hlavacek's answer, but expandable:
This requires that
Edit: Taking Will's (absolutely correct) comment into consideration, change the definition of
Then you can use
A rather retro way of avoiding the problems Will talks about is to use flags. Define:
I tend to avoid any kind of overlap between