Unexpected newline (catcode magic)

boxesline-breakingmacros

I am playing with Tex catcodes and hboxes. The following code should produce three lines of output:

A...1...
B...2...
C...3...

However, for some reason it makes a line break between A and ...1..., and I have no idea why.

Here is the code:

\def\changeCatcodes{
    \catcode`A=13
    \catcode`B=13
    \catcode`C=13
}

\begingroup
\changeCatcodes
\gdefA{\argtohbox{\string A}}
\gdefB{\argtohbox{\string B}}
\gdefC{\argtohbox{\string C}}
\endgroup

\gdef\argtohbox#1{\hbox to 20pt{#1}}

\gdef\newstring{\penalty-10000}

\catcode`\^^M=13 % one for reading macro definition
\def\specialenv{%
    \begingroup%
    \parindent0pt%
    \catcode`\^^M=13 % ... and one for actually changing it when it is executed
    \changeCatcodes%
    \let^^M\newstring%
}
\catcode`\^^M=5 % return catcode back

\def\endspecialenv{\endgroup}

\specialenv
A...1...
B...2...
C...3...
\endspecialenv
\end

Best Answer

A simpler version is

enter image description here

from


\hbox{A}1
\hbox{B}2
\hbox{C}3

\bye

as you have no \leavevmode before the \hbox the first hbox stays in vertical mode, but then the paragraph is started (in your case by ...) so the following hboxes are found in hmode.

Related Question