# Conditional, vertical space distribution of for-loop created content

I'm creating a report template that has an "evaluation" section for each of many individuals. Each evaluation section is comprised of three components: a heading and two text blocks (each placed inside of a tcolorbox). The evaluation sections will be generated dynamically using a template engine, so the content and number of sections is unknown before hand.

To save on space, I want to allow each section to distribute over two pages if necessary. However, I don't want the heading and the first text block to be separated. To handle this, I placed them within a samepage block. Now the distribution of content is correct, but I want to improve the spacing on each page. I want to have the sections on each page be evenly distributed such that the spacing above, between, and below is all uniform (unlike flushbottom where above the top and below the bottom elements is no space).

I can use \vspace*{\vfill} to somewhat make this work, however, I want to ensure that if a whole evaluation section fits on one page, then there is no additional space between the two text blocks. I have tried various combinations of spacing and par to make this work, but I think I lack the terminology to concisely describe my problem and to research it.

Here is a MWE that demonstrates the general structure of content. I've written down some comments and tried placing some vfill commands in places that I think may help. It will typically end up being that one and one half reports will fit per page. Ideally, the spacing above the first report, between the first report and the second report, and below the second report would all be equal.

\documentclass{report}

\usepackage[letterpaper,margin=0.5in]{geometry}

\usepackage[most]{tcolorbox}
\usepackage{parskip}
\usepackage{pgffor}
\usepackage{lipsum}

\usepackage{showframe}

%\flushbottom
\newtcolorbox{containingBox}[1][]{%
width=16cm,
arc=0mm,
colframe=blue,
colback=blue,
nobeforeafter,
after skip=15pt,
#1,
}%

\pagestyle{empty}

\begin{document}

\foreach \n in {0,...,5} {
\vspace*{\fill}

% Heading and first text block
\begin{samepage}
\begin{center}
\begin{containingBox}
\lipsum[1]
\end{containingBox}
\end{center}
\end{samepage}

% Second section that can break to another page, but
% should stay close if on same page
% This is where I want to put a vfill only if
% this block has gotten separated

%\vspace*{\fill}

\begin{center}
\begin{containingBox}
\lipsum[1-2]
\end{containingBox}
\end{center}
\vspace*{\fill}
}
\end{document}


What I did here is to add a \vspace{\fill} after the first block and then, inside the 2nd center block, added a \vspace{-\fill}. If they appear on the same page, then the two \vspaces cancel, but if separated, a residual positive \vspace after the 1st block remains active at the bottom of the page. The negative \vspace at the top of the next page has no net effect.

Because the center environments add extra vertical space, and the OP indicated to keep the blocks together if on the same page, I also added a \vspace{-2\topsep} between blocks.

\documentclass{report}

\usepackage[letterpaper,margin=0.5in]{geometry}

\usepackage[most]{tcolorbox}
\usepackage{parskip}
\usepackage{pgffor}
\usepackage{lipsum}

\usepackage{showframe}

%\flushbottom
\newtcolorbox{containingBox}[1][]{%
width=16cm,
arc=0mm,
colframe=blue,
colback=blue,
nobeforeafter,
after skip=15pt,
#1,
}%

\pagestyle{empty}

\begin{document}

\foreach \n in {0,...,5} {
\vspace*{\fill}

% Heading and first text block
\begin{samepage}
\begin{center}
\begin{containingBox}
\lipsum[1]
\end{containingBox}
\end{center}
\end{samepage}
% Second section that can break to another page, but
% should stay close if on same page
% This is where I want to put a vfill only if
% this block has gotten separated
\vspace{-2\topsep}
\vspace{\fill}
\begin{center}
\vspace{-\fill}
\begin{containingBox}
\lipsum[1-2]
\end{containingBox}
\end{center}
\vspace*{\fill}
}
\end{document}

• Yes, that is almost exactly what I'm looking for. Additionally, I'd like to have spacing above text blocks that get separated on to another page. However, this solves the biggest challenge for me. If you don't mind elaborating - I'd really appreciate it. I could probably figure out the rest once I understand this. Jul 3 '17 at 14:07