[Tex/LaTex] What happens when \textheight changes mid-document

marginspage-breaking

I know it's generally considered to be unwise, but I need to understand what happens when \textheight changes mid-document. From this example, it appears that the change takes effect on the next page, when \vsize is changed.

\documentclass{article}
\begin{document}
\typeout{Page 1:}
\showthe\textheight
\showthe\vsize
abc
\newpage
\typeout{Page 2:}
\addtolength{\textheight}{1cm}
\showthe\textheight
\showthe\vsize
def
\newpage
\typeout{Page 3:}
\showthe\textheight
\showthe\vsize
ghi
\end{document}

Is this always the case?

Best Answer

It is not "unwise" to change \textheight in mid-document - it is unsupported and by changing it midway strange things can happen. On the whole you are right, if you do this then it appears to affect the next page, but there are dependencies to the float algorithm which are asynchronous to the pages (for example, a float is tested when encountered to ensure that it doesn't exceed \textheight, and if so it will get artificially shortened so that the float algorithm can expect that all floats fit onto the page).

But there are also other surprising side-effects: try out this example:

\documentclass[twocolumn]{article}

\usepackage{lipsum}

\setlength\textheight{4\baselineskip}\addtolength\textheight{\topskip}

\begin{document}

%\begin{figure*}  X\\Y \end{figure*}  %  <--- uncomment for test and watch what happens to page 2

1\\2\\3\\4\\5\\6\\7\\8\\9 \newpage

\addtolength\textheight{-3\baselineskip}

\lipsum

\end{document}

Here is what you get with and without the float on page 2 (the height reduction happens only from page 3 onwards):

enter image description here

But with the float already the second page gets shortened but has too much material now: enter image description here

Now this is something one could argue is a LaTeX 2e (or even LaTeX2.09) bug, but then one could call it a feature as it is unsupported input.

In summary, if you need to understand what's really going on because you want to build something that changes \textheight then I fear you need to study all the places where it is being used

grep textheight latex.ltx
grep @colht latex.ltx

and whether what happens there can effect what you are trying to do --- but as I said it is not a supported interface variable.