# “Smart” page breaking for boxed content

I am trying to load content from a central .tex file, then display it.

%%%%%%%%%%%%%%%
% central.tex %
%%%%%%%%%%%%%%%
\saveContent{A}{this is content "A"}
\saveContent{B}{this is content "B"}
\saveContent{C}{this is content "C"}
\saveContent{D}{this is content "D"}
% \vdots


.

%%%%%%%%%%%%%%%%
% document.tex %
%%%%%%%%%%%%%%%%
\documentclass{article}
\input{central.tex}

\useContent{A}
\useContent{B}
\useContent{C}

\begin{document}
\displayUsableContent
\end{document}


The particular use case is that this allows me to write problem sets and exams using a question bank, editing only the top few lines of the document. I am sure there are many ways to do this.

Different content blocks have different sizes. Let's say that A takes up 0.5 (vertical) pages, B takes up 1.0 page, C takes up 1.5 pages, and D takes up 0.5 pages. I would like them to display as

+---+  +---+  +---+  +---+
| A |  | B |  | C |  | C |
|   |  | B |  | C |  | D | % (breaking D onto its own page would be acceptable)
|(1)|  |(2)|  |(3)|  |(4)|
+---+  +---+  +---+  +---+


I have tried wrapping the content in minipage, which cuts off the last 0.5 pages of C:

+---+  +---+  +---+  +---+
| A |  | B |  | C |  | D |
|   |  | B |  | C |  |   | % where is my C :(
|(1)|  |(2)|  |(3)|  |(4)|
+---+  +---+  +---+  +---+


I have tried wrapping the content in list, which doesn't respect trying to keep content together where possible:

+---+  +---+  +---+  +---+
| A |  | B |  | C |  | D |
| B |  | C |  | C |  |   | % B is broken but doesn't need to be :(
|(1)|  |(2)|  |(3)|  |(4)|
+---+  +---+  +---+  +---+


Is there a way to have LaTeX automatically break pages in this way? The rough rule is, If additional content cannot fit on the same page as previous content, put it on a new page; create page breaks in content if necessary.

• Nice pages! If I understand correctly, you would want |A|BB|CC|C|D| effectively having 5 pages in the end? Or is your first design example acceptable? – thymaro Nov 14 '18 at 3:14
• @thymaro either 4 or 5 pages would work. 5 pages could probably be done with \clearpage, but I'd rather condense content where possible. – kyle Nov 14 '18 at 4:49
• What is the "boxed" in your title? Is it because you tried minipages? I don't see any other reference to boxes. – Donald Arseneau Aug 5 at 20:30

I'm not sure about how to apply this solution but I think it can work. It consists in using tcolorboxes to include the problems in main document. In this case it's possible to control how much vertical space is used by a box. By default it's its natural height but you can use options like feight fill to use all available space in every page. It's also possible to use option heigth fixed for option in breakable boxes to decide if last fragment uses a complete page or only its natural height. Here you have some examples:

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\tcbset{enhanced, breakable, height fixed for=all}

\begin{document}
\begin{tcolorbox}[height fill, title=Problem A]
\lipsum[1]
\end{tcolorbox}
\begin{tcolorbox}[height fill, title=Problem B]
\lipsum[1-3]
\end{tcolorbox}
\begin{tcolorbox}[title=Problem C]
\lipsum[1-5]
\end{tcolorbox}
\begin{tcolorbox}[height fixed for=none, title=Problem D]
\lipsum[1-5]
\end{tcolorbox}
\begin{tcolorbox}[height fill, title=Problem E]
\lipsum[1]
\end{tcolorbox}
\end{document}


Note: option blanker will suppress all frames and colors but keep vertical space.

Presuming the content is free to undergo page breaks (not boxed), just put

\filbreak


between content items