# How to make sections start on pages that are multiple of four?

I teach programming and one of the ways I use Latex is to take an entire class' worth of submitted programs and generate one large PDF document I can send to the printer. (With about a hundred students it's annoying to create and print 100 documents separately.) In the interest of saving trees, I'd like to print four pages per sheet of paper (two on front, two on back). This is a setting that is supported naturally by my printer.

The problem is that student's programs cannot end up on the same piece of paper. For example, if the first student's program ends up as three pages, and the second student's program ends up as three pages, then the first sheet of paper will have the first student's program in positions 1-3 and the first page of the second student's program in position 4. Then the second sheet of paper will have the next two pages of the second student's program in places 1 and 2, and the third student's program in places 3 and 4, etc.

What I would like is a LaTeX command that inserts between zero and three pages depending on the current absolute page. If the current absolute page is a multiple of four, then insert zero pages. If not, it inserts up to three pages so the next command will start on a multiple of four.

To use a LaTeX analogy, I'd like a version of \cleartooddpage that instead clears to a fourth page. This is actually what I currently use to get double sided printing, but as mentioned above I'd like to get four pages per paper.

Welcome to TeX-SE! This proposal, using this answer, seems to work. (You can also get rid of the \ifcase if needed.)

\documentclass[11pt]{article}
\usepackage{lipsum}
\usepackage{pgffor}
% from https://tex.stackexchange.com/a/398999/121799
\newcommand\InsertBlankPages[1]{% \InsertBlankPages{n} => insert n blank pages
\foreach \blank in {1,...,#1} {
\newpage
\thispagestyle{plain}
\mbox{}
}%
}
\ifcase\qcpg%
\InsertBlankPages{3}\clearpage%
\or%
\InsertBlankPages{2}\clearpage%
\or%
\InsertBlankPages{1}\clearpage%
\or%
\clearpage%
\fi}
\begin{document}
\section{A}
\lipsum[1-10]
\section{B}
\lipsum[1-2]
\section{C}
\lipsum[1-12]
\section{D}
\lipsum[1-18]
\end{document}


You may generalize this to any number. (4 is here the default.)

\documentclass[11pt]{article}
\usepackage{lipsum}
\usepackage{pgffor}
% from https://tex.stackexchange.com/a/398999/121799
\newcommand\InsertBlankPages[1]{% \InsertBlankPages{n} => insert n blank pages
\foreach \blank in {1,...,#1} {
\newpage
\thispagestyle{plain}
\mbox{}
}%
}
\newcommand\cleartopage[1][4]{\pgfmathtruncatemacro{\qcpg}{#1-1-mod(\value{page},#1)}%
\ifnum\qcpg=0%
\else%
\InsertBlankPages{\qcpg}%
\fi%
\clearpage}
\begin{document}
\section{A}
\lipsum[1-10]
\cleartopage[4]
\section{B}
\lipsum[1-2]
\cleartopage[4]
\section{C}
\lipsum[1-12]
\cleartopage[4]
\section{D}
\lipsum[1-18]
\cleartopage[4]
\end{document}