In Plain TeX how can I make vboxes breakable, and can I handle/measure them separately? What I'm trying to do, is that I have a portion of text, that is treated separately (no indention, narrower, coloured background, with the use of \hrule/\vrule) and make it breakable.

Additionally I'm interested in how to brake automatically hboxes

  • 1
    hboxes cannot break - that is rather the point of them!
    – Joseph Wright
    Jun 16, 2011 at 12:34

2 Answers 2


A \vbox is by itself not breakable. But assigning it to a box register allows TeX to split it with the \vsplit operation.

<measurements setting \partialboxdim to the available space>
\setbox\partialbox=\vsplit\totalbox to \partialdim
<measurements setting \partialboxdim to the available space>

This is the scheme, you have to provide the \addcoloredbackground macro and the measurements, probably based on \pagegoal and \pagetotal. The TeXbook and TeX by Topic have examples about \vsplit.

Let's expand a bit; I assume you have a \colorthisbox macro that adds a colored background to a box and requirese 3pt of extra space above and below it. The strategy is first to gather the text to be boxed:

\setbox\totalbox=\vbox\bgroup\advance\hsize by -6pt}

At this point we have all the text; now we start the splitting:


The macro \finishcoloredbox should provide the finishing touches, I've only put a \bigskip. The macro \continuesplitting is the heart of the process: it first measures the available space, but first of all it places a \null box in the main vertical list, because when the current page is empty, \pagegoal equals \maxdimen. The 6pt is the extra space reserved for printing the colored box. If the (remaining) part of box \totalbox is less high than the available space, we simply set box \partialbox to it, otherwise we split off from \totalbox what is necessary to fill the available space; then we pass the control to \colorthisbox, but we add \eject in the second case, since we are sure that we have reached the bottom of a page and we want to start at a fresh one with \pagetotal=0pt. After processing we call \splitcoloredbox again.

\def\continuesplitting{\null % In case this starts a new page
    \setbox\partialbox=\vsplit\totalbox to\dimen255

Here is the definition of \colorthisbox I used for testing; it's important that it encloses everything in a box (\hbox or \vbox) in order not to start horizontal mode.


(yes, the testing was done in LaTeX, because of \lipsum). The \vbox{\unvbox\partialbox} part is important because it avoids underfull boxes.

  • @egreg: Am I correct in thinking that this approach means that splitting has to come before adding background colour?
    – Joseph Wright
    Jun 16, 2011 at 14:01
  • @Joseph: I think this is the easiest way, but I'm no expert in pdfTeX color management.
    – egreg
    Jun 16, 2011 at 14:07
  • @egreg: neither am I. A quick look at color.sty shows me that \colorbox is implemented using a rule the size of the box, which of course can't then be split. So if the colour goes in before splitting it would have to be done some other way.
    – Joseph Wright
    Jun 16, 2011 at 14:37
  • @Joseph: so it's better to delay the coloring
    – egreg
    Jun 16, 2011 at 14:45
  • Well that does not solve the underfull boxes, and also seems awkward, when it splits at the first line, but it does the trick. Thanks.
    – Adam L. S.
    Jun 18, 2011 at 10:54

An experiment

This approach of breaking vertical boxes helped me. I tried to figure out how many lines I am dealing with in a block. I am usually using the lineno package for that and its \linelabel and \lineref commands. These commands are similar to their \label and \ref counterparts. I am able to get a number of lines using this method. If I need a number of section I use common \label next to the \linelabel command.

However, when I am typesetting via \parbox, this approach is not working. We can list line numbers using the internal command \internallinenumbers from the lineno package but I don't know how to refer to them.

Therefore, I saved my paragraph into a vertical box and started to splitting lines up by \baselineskip. Once the height of that box (\myboxtemp) is equal to 0pt I am done. I am enclosing an example where I rotated and coloured the lines differently. Number of lines is stored in the counter named \mycounter.


\advance\mujtemp by -35mm

\section{My section}
My first line.\par My second line.\linelabel{myfavoriteline}\label{mysection}
\par My third line.\par

I am referring to line~\lineref{myfavoriteline}, section~\ref{mysection} on page~\pageref{myfavoriteline}.


width \the\wd\mybox, height \the\ht\mybox, depth \the\dp\mybox

\setbox\mybox=\vbox to \vsize{\rightskip=35mm\kant[1]}%
\advance\mycounter by 1%
\setbox\myboxtemp=\vsplit\mybox to \baselineskip%

\makebox[35mm][l]{\ifnum\mycounter<10 0\fi
\the\mycounter---\the\ht\myboxtemp\ \the\dp\myboxtemp\  }
\ifnum\ht\myboxtemp=0 There are no more lines!\fi
{\color{green}\vbox to 1\baselineskip
\begin{tikzpicture}[remember picture, overlay]
\node[anchor=west, rotate=2.5*\the\mycounter, color=green!50!black!\myaddition]{\usebox\myboxtemp};



Edit: This is my best shot of rewriting and cutting down the code into Plain TeX format. We couldn't use \newsavebox, \savebox and \usebox commands as these are the LaTeX format commands, please see latex.ltx for more details. I didn't know how to load a paragraph from the kantlipsum package, so I extracted a small portion of their code. I enclose the source code and a page preview.

%! pdftex boxes-plain.tex
\hsize=345pt % Taken from LaTeX...
{\bf From kantlipsum package:}\par\vskip2mm
\def\mypara{As any dedicated reader can clearly see, the Ideal of practical reason is a representation of, as far as I know, the things in themselves; as I have shown elsewhere, the phenomena should only be used as a canon for our understanding. The paralogisms of practical reason are what first give rise to the architectonic of practical reason. As will easily be shown in the next section, reason would thereby be made to contradict, in view of these considerations, the Ideal of practical reason, yet the manifold depends on the phenomena. Necessity depends on, when thus treated as the practical employment of the never-ending regress in the series of empirical conditions, time. Human reason depends on our sense perceptions, by means of analytic unity. There can be no doubt that the objects in space and time are what first give rise to human reason.}
  \advance\mycounter by 1%
  \setbox\myboxtemp=\vsplit\mybox to \baselineskip
  \hbox to\hsize{Line number \the\mycounter\hfill}%
\ifnum\ht\myboxtemp=0{\it There are no more lines! I am escaping from the loop.}\fi


  • I'm specifically looking for a plain-[Xe]TeX solution. I did not dive in your solution yet, but I'll check it out when I'm there.
    – Adam L. S.
    Mar 25, 2014 at 8:47
  • 1
    Yes, it happens to me quite often, I am mixing LaTeX commands with the TeX primitives. I have used the \newsavebox in the first part of the example which is defined in the latex.ltx file, that part is not going to work for you right away. This is working for me \newbox\mybox\setbox\mybox=\vbox{\rightskip=35mm typeset material}.
    – Malipivo
    Mar 25, 2014 at 9:18
  • Just wanted to say thanks for this code, helped me a lot. Cheers!
    – sdaau
    Jul 3, 2014 at 8:46

You must log in to answer this question.

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