# How does \everyhbox work?

Knuth in the TexBook Chapter 24, gives a short list of the every commands. The \everyhbox command inserts a token list - as its name implies- before a horizontal box.

Here is a short example:

\documentclass[11pt]{article}
\begin{document}
\begingroup
\everyhbox{a} % change this to $a$ to see the issue!
\def\oneLineBox#1#2%
{%
\hfuzz=0pt
\overfullrule=0.25pt
\setbox2=\hbox to 5cm{\box0\hfil\box1}%
\box2
}
\endgroup

\end{document}

The oneLineBox macro above set ups some boxes and using \everybox we add the letter a in each horizontal box. But when we change the letter to a math letter say $a$, the badness is shown as zero, why? Surely the badness of the line did not disappear? Similar results can be obtained by changing \badness to \hbadness, this time TeX displays 1000 for all the lines. The overfullrule also seems to go a bit awry!

-
@TH Sorry, I edited the code and wrote \badness. I was playing with the code and copied wrongly. Will you please give it another go? –  Yiannis Lazarides Dec 29 '10 at 20:39
I moved my comment into my answer. –  TH. Dec 29 '10 at 20:51

This doesn't really have anything to do with \everyhbox. The real issue is that constructing a math list involves constructing hboxes and the \badness reflects that.

For example,

$a$\the\badness
\bye

The first box is infinitely bad because the glue shrunk beyond its limit. In the second case, the box is again infinitely bad, but the \badness reflects that of the a and its italic correction, I believe.

Okay, I finally tracked down where this happens in tex.web. mlist_to_hlist converts a math list to a horizontal list as per the rules in Appendix G. In particular, it calls hpack which contains

I was just about to post more-or-less the same thing :-) The easiest way to see what is going on is to use \everyhbox{\showthe\badness$a$\showthe\badness}. –  Joseph Wright Dec 29 '10 at 20:52
@TH You are right it has nothing to do with everyhbox. I changed the code to that suggested by Joseph and removed the\badness in box1 and it displays two sets of badness. One for the $a$ and one for the text line, both correct now. Thanks! –  Yiannis Lazarides Dec 29 '10 at 21:16
I was looking through tex.web and the woven version to find exactly where this gets set and ran across this great line: "The simplest math formula is, of course '', when no noads are generated. The next simplest cases involve a single character, e.g., '$x$'. Even though such cases may not seem to be very interesting, the reader can perhaps understand how happy the author was when '$x$' was first properly typeset by TeX." Yes, yes I can. –  TH. Dec 29 '10 at 21:41