# 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,

\setbox0\hbox spread -1em{X}

$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

last_badness := badness(...)


and this global variable is exactly the badness reported by \badness.

I'm not sure if this is ever actually mentioned in The TeXbook—I looked through texbook.tex (my copy of the book is in my office) and didn't see any mention of it. It doesn't appear to be in TeX by Topic either.

\hbadness and \vbadness are parameters that control the "amount of tolerance before TeX reports and underfull or overfull horizontal/vertical box" (TeX by Topic). That it doesn't change is not surprising.

-
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
@TH it is a great comment by Knuth! Is it still possible to get the original pascal compiler and code and run it on a windows machine? –  Yiannis Lazarides Dec 29 '10 at 22:13
@Yiannis: I'm not sure. I don't know anything about Windows. That said, looking through texbook.tex, I saw this comment, "Also, TeX was first implemented on a DEC-10. The \catcode for <space> is 10. My birthday is January 10. Can this all be just a coincidence?" I guess he means a PDP-10. So maybe you should track one of those down. =) –  TH. Dec 29 '10 at 22:36