4

This is somewhat similar to Reading the contents of a box, but with a specifically plain Latex example; I somewhat solved the question while writing the post, so I'll formulate the question like this: is the below all there is to it?

Basically, I have a situation, where I'd like to concatenate/append input text as box material; the below MWE demonstrates this concatenation "manually":

\documentclass{article}

\loggingoutput\scrollmode
\tracingonline=1 % shows just \box42=[] in terminal, unless \loggingoutput\scrollmode above is enabled!

% https://tex.stackexchange.com/questions/46480/setbox-vs-sbox-and-savebox-what-are-the-differences-i-need-to-know-about
\newsavebox{\mainbox}
\newsavebox{\firstchildbox}
\newsavebox{\secondchildbox}

\begin{document}

\savebox\mainbox{inner2 inner1 main}
\typeout{Mainbox with inline contents:}
\showbox\mainbox

% "Manual" concatenation/appending of box material
\setbox\secondchildbox\hbox{inner2}
% \box will clear the box register (\usebox or \copy will not erase it)
\savebox\firstchildbox{\box\secondchildbox inner1}
\setbox\mainbox\hbox{\usebox\firstchildbox main}
\typeout{Mainbox with nested contents:}
\showbox\mainbox

% unhbox via https://tex.stackexchange.com/questions/6183/reading-the-contents-of-a-box
\setbox\secondchildbox\hbox{inner2}
% \box will clear the box register (\usebox or \copy will not erase it)
\savebox\firstchildbox{\unhbox\secondchildbox\space inner1}
\setbox\mainbox\hbox{\unhbox\firstchildbox\space main}
\typeout{Mainbox with unnested contents:}
\showbox\mainbox

\end{document}

The MWE does not produce PDF output, all output is to the terminal/logfile. Basically, if I put a text like "inner2 inner1 main" directly into a \hbox inline, the box looks like this in the terminal output:

Mainbox with inline contents:
> \box26=
\hbox(6.67859+0.0)x82.83351
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 2
.\glue 3.33333 plus 1.66666 minus 1.11111
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 1
.\glue 3.33333 plus 1.66666 minus 1.11111
.\OT1/cmr/m/n/10 m
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 n

If I do the "manual" concatenation/appending, then I have:

Mainbox with nested contents:
> \box26=
\hbox(6.67859+0.0)x76.16685
.\hbox(6.67859+0.0)x54.50012
..\hbox(6.67859+0.0)x27.25006
...\OT1/cmr/m/n/10 i
...\OT1/cmr/m/n/10 n
...\OT1/cmr/m/n/10 n
...\OT1/cmr/m/n/10 e
...\OT1/cmr/m/n/10 r
...\OT1/cmr/m/n/10 2
..\OT1/cmr/m/n/10 i
..\OT1/cmr/m/n/10 n
..\OT1/cmr/m/n/10 n
..\OT1/cmr/m/n/10 e
..\OT1/cmr/m/n/10 r
..\OT1/cmr/m/n/10 1
.\OT1/cmr/m/n/10 m
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 n

So, the "concatenation" resulted with a nested hierarchy of boxes, with a parent/child relationship.

My original question was, how could I flatten the hierarchy and obtain spaces like in the "inline" version; then I saw Reading the contents of a box, and realized I could use \unhbox to "unpack" and thereby use the "first child" contents (immediate contents) of a given box; and adding \space ensures the same \glues are present in the box contents as in the "inline" case:

Mainbox with unnested contents:
> \box26=
\hbox(6.67859+0.0)x82.83351
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 2
.\glue 3.33333 plus 1.66666 minus 1.11111
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 n
.\OT1/cmr/m/n/10 e
.\OT1/cmr/m/n/10 r
.\OT1/cmr/m/n/10 1
.\glue 3.33333 plus 1.66666 minus 1.11111
.\OT1/cmr/m/n/10 m
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 n

This looks good enough to me - but is this all that there is to it?

1

1 Answer 1

4

You can not in general do this in TeX (luatex is different).

If your first box ends in (say) . then adding a \space after the \unhbox will not add extra space due the the space factor stretching, it will just add a normal space. If the first box ends with a glue you can see how much glue was there with \lastskip but if it is a character node such as . it can not be accessed via TeX.

\documentclass{article}
\begin{document}
\showoutput

\setbox0\hbox{This.}
\setbox0\hbox{\unhbox0 \space That.}
\showbox0
\setbox0\hbox{This. That.}
\showbox0
\stop

Produces

\hbox(6.94444+0.0)x50.05565
.\OT1/cmr/m/n/10 T
.\OT1/cmr/m/n/10 h
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 s
.\OT1/cmr/m/n/10 .
.\glue 3.33333 plus 1.66666 minus 1.11111
.\OT1/cmr/m/n/10 T
.\OT1/cmr/m/n/10 h
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 t
.\OT1/cmr/m/n/10 .

In the first case, a box with a 3.33pt space.

and in the second

\hbox(6.94444+0.0)x51.16676
.\OT1/cmr/m/n/10 T
.\OT1/cmr/m/n/10 h
.\OT1/cmr/m/n/10 i
.\OT1/cmr/m/n/10 s
.\OT1/cmr/m/n/10 .
.\glue 4.44444 plus 4.99997 minus 0.37036
.\OT1/cmr/m/n/10 T
.\OT1/cmr/m/n/10 h
.\OT1/cmr/m/n/10 a
.\OT1/cmr/m/n/10 t
.\OT1/cmr/m/n/10 .

The space here is 4.44pt at the end of the sentence.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .