I could not find references to this with a cursory search, so I thought I'd post a question; I'm aware this is probably discussed at length in the Tex book or similar, but unfortunately I don't have the time to delve into those details now.
Basically, I'm trying to understand the code in the answer putting a box around each line of text in a paragraph, in particular this macro:
\def\hmmx{%
\@tempcnta\z@
\loop
\advance\@tempcnta\@ne
\setbox\z@\lastbox
\global\dimen@i\ht\z@
\skip@\lastskip\unskip
\count@\lastpenalty\unpenalty
\ifdim 5sp=\wd\z@
\else
\global\setbox\@ne\vbox{%
\penalty\count@
\vskip\skip@
\ifodd\@tempcnta
\hbox{\reflectbox{\box\z@}}%
\else
\box\z@
\fi
\unvbox\@ne
}%
\repeat
}%
I can see that it loops, and then it basically sets up the initial boxes for a line, which makes sense - but I don't understand, where are the characters read from the source file afterwards? E.g., if there was something like:
\ifodd\@tempcnta
\hbox{\reflectbox{\box\z@}}\PROCEEDwithRESTofLINE%
\else
\box\z@\PROCEEDwithRESTofLINE
\fi
... it would have made more sense to me - but obviously, there is no \PROCEEDwithRESTofLINE
there. I would have assumed that something like a \PROCEEDwithRESTofLINE
would read next character from input (source file), and decide if it belongs on the line: if it belongs, then a glyph is written to the PDF output; if not, then a line break would be output (except there are no "line breaks" in PDF literals, if I remember correctly - just positioning text glyphs at X/Y); and that would be the signal to "return" to the \hmmx
function, so that \repeat
could execute again. But again - this is just my speculation; I have no idea how this actually works.
So, could someone provide a breakdown what happens in terms of line processing in (La)Tex: considering tex input character by character (or token by token), when does the engine output glyphs to PDF; how does the engine know when to break the line; and how is that (line break or a new line) signaled to the \loop
/\repeat
above - so that code can intervene only on "opening" boxes, so to speak? We might as well speak about the simplest example, something like:
\documentclass{article}
\begin{document}
Just a long, or somewhat long line here; long enough to cause a line break within a paragraph in Latex ...
\end{document}
I'm aware this may not be the simplest thing to answer - so just some direct links to docs where this would be explained, would be much appreciated...