9

I'm wondering if it's possible to implement a peek method to inspect the head of a \vbox without changing the content of the \vbox. It could be done by brute force using a \copy of the original \vbox, but I'm looking for a not so brutal method.

With the following code, a \bigbox is constructed, its head split of as \partbox, and finally all is reassembled again as \bigbox. \testmybox serves for showing the boxes.

\documentclass{article}
\usepackage{blindtext}

\def\testmybox#1{%
  \par%
  {\fboxsep0pt\fbox{\copy#1}}\par%
  \edef\myboxheight{\the\dimexpr\ht#1+\dp#1\relax}%
  Total height: \myboxheight%
}

\begin{document}

\newbox\bigbox
\newbox\partbox

\setbox\bigbox=\vbox{\hsize 11cm%
  \footnotesize\blindtext[1]}

\textbf{Original box}\par
\testmybox\bigbox

%\splittopskip=0pt
%\splitmaxdepth=0pt

\setbox\partbox=\vsplit\bigbox to 45pt%

\textbf{Splitted box:}\par
\testmybox\partbox

\textbf{Rest:}\par
\testmybox\bigbox

\setbox\bigbox=\vbox%
{%
  \unvbox\partbox
  \unvbox\bigbox
}

\textbf{Reassembled box:}
\testmybox\bigbox

\end{document}

enter image description here

The problem is that the reassembled box is not identical to the original box. Here, there is too much space between the two parts. This space depends on the \splittopskip value, but I don't know how to set this value such that it works with all thinkable situations.

Is it possible to reassemble the original box without deviation?

8

Since nobody answered or commented this question, I thought there was no answer. But then I found \splitdiscards and \savingvdiscards from e-TeX which seem to be exactly the missing commands. With it, the answer seems to be stupidly easy - would have saved my many hours if I knewed this before:

\documentclass{article}
\usepackage{blindtext}

\def\testmybox#1{%
  \par%
  {\fboxsep0pt\fbox{\copy#1}}\par%
  \edef\myboxheight{\the\dimexpr\ht#1+\dp#1\relax}%
  Total height: \myboxheight%
}

\begin{document}

\newbox\bigbox
\newbox\partbox

\setbox\bigbox=\vbox{\hsize 11cm%
  \footnotesize\blindtext[1]}

\textbf{Original box}\par
\testmybox\bigbox

\splittopskip=0pt
%\splitmaxdepth=0pt
\savingvdiscards=1
\setbox\partbox=\vsplit\bigbox to 45pt%

\textbf{Splitted box:}\par
\testmybox\partbox

\textbf{Rest:}\par
\testmybox\bigbox

\setbox\bigbox=\vbox%
{%
  \unvbox\partbox%
  \splitdiscards%
  \unvbox\bigbox%
}

\textbf{Reassembled box:}
\testmybox\bigbox

\end{document}

enter image description here

  • I still believe that \setbox\tempbox=\copy\bigbox is cleaner if you just want to access \bigbox for measuring but not touching it. – egreg Jul 31 '14 at 6:45
  • @egreg but less efficient memory-wise, which may occasionally matter. – Bruno Le Floch Jul 31 '14 at 7:15
  • @egreg I use this for madly large boxes where a copy really costs memory. – Thomas F. Sturm Jul 31 '14 at 8:32

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.