10

In chapter 23 "Output routines" there is a warning:

It's best not to monkey with \hsize and \vsize except at the very beginning of a job, or after you have ejected all pages from TeX's memory.

But in TeXbook itself there are several examples where \hsize is changed:

1) In the exercise with story.tex \hsize is set several times in one tex job.

\hsize=4in \input story
\hsize=3in \input story
...

I suppose, this case is "legal", because \eject is called at the end of story.tex

Some more examples:

2) On page 115 \hsize is set inside \topinsert.

3) On page 223 \hsize is set inside \vbox.

In which contexts is it "legal" to modify \hsize and why there is this warning in TeXbook? Is there an example which shows what can go wrong if we change \hsize in a "bad" place?

5

What is \hsize?

The primitive parameter \hsize is the length that TeX uses to split paragraphs into lines (taking also into account \leftskip, \rightskip and the paragraph shape instructions coming from \hangindent and \hangafter.

When a paragraph has been split into lines, TeX will contribute a stack of horizontal boxes of width \hsize to the current vertical list. The width will be different if \hangindent and \hangafter are being used and will reflect the actual width of each line.

When \parshape is being used, it's like a different \hsize is specified for each line.

Consider the code

\input plipsum

\hsize=300pt

\showboxbreadth=1000 \showboxdepth=0 

\lipsum{1}

\showlists

\hsize=200pt

\lipsum{2}

\showlists

\bye

The first \showlists command outputs

### vertical mode entered at line 0
### current page:
\glue(\topskip) 3.05556
\hbox(6.94444+1.94444)x300.0, glue set - 0.62798 []
\penalty 150
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 0.38911 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set - 0.17653 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 0.24695 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 0.36536 []
\penalty 150
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 227.0554fil []
total height 70.0
 goal height 643.20255
prevdepth 1.94444, prevgraf 6 lines

so we see a paragraph consisting of six lines, corresponding to six boxes 300pt wide.

The second \showlists instruction outputs

### vertical mode entered at line 0
### current page:
\glue(\topskip) 3.05556
\hbox(6.94444+1.94444)x300.0, glue set - 0.62798 []
\penalty 150
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 0.38911 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set - 0.17653 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 0.24695 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 0.36536 []
\penalty 150
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x300.0, glue set 227.0554fil []
\glue(\parskip) 0.0 plus 1.0
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x200.0, glue set 0.68265 []
\penalty 250
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x200.0, glue set - 0.56822 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x200.0, glue set 0.02776 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x200.0, glue set 0.76903 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x200.0, glue set 1.07141 []
\glue(\baselineskip) 3.11111
\hbox(6.94444+1.94444)x200.0, glue set 0.93843 []
\penalty 100
\glue(\baselineskip) 3.11111
\hbox(6.94444+0.0)x200.0, glue set 0.1729 []
\penalty 100
\glue(\baselineskip) 5.05556
\hbox(6.94444+1.94444)x200.0, glue set 0.93886 []
\penalty 250
\glue(\baselineskip) 5.75002
\hbox(4.30554+0.0)x200.0, glue set 179.41664fil []
total height 178.0 plus 1.0
 goal height 643.20255
prevdepth 0.0, prevgraf 9 lines

The first six lines are there, look for \parskip in order to see where the second paragraph starts. We have nine lines, all 200pt wide.

Where is it legal to change \hsize?

Anywhere. Unless \parshape is being used, TeX will use the value of \hsize which was current when \par has been scanned for breaking paragraphs into lines.

Of course, this setting of \hsize will respect TeX grouping (if you're not devious enough to do global settings). So if we try

{\hsize=1pt \nopar\lipsum{1}}\par

we get exactly the same as before, because the change to \hsize will have been forgotten when \par is found. Note that you cannot have two distinct values for \hsize when a paragraph is being built (barring \parshape, of course).

Does \hsize influence page shape?

Not by design of TeX. However, the output routine can and probably will use the standard \hsize for setting headers and footers. For instance, \plainoutput has \makeheadline that does

\line{\vbox to 8.5pt{}\the\headline}

and \line is \hbox to \hsize.

So this is the main meaning of Knuth's warning: if you change \hsize in the middle of a page, unexpected results can show up, depending on when the output routine comes into action.

Changing \hsize inside a \vbox is always safe, because excercising the page builder when working in a secondary vertical list never calls the output routine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.