# How can I make sure that an *arbitrary* part of a document is printed on the same page?

I want to make sure that an arbitrary part of the document is always printed on the same page.

Note 1:

My problem focuses on lists and tables. However, I believe that the solution (if there is one) is general and does not apply exclusively to these two elements.

Note 2:

I give (simple) codes that illustrate what I try to do on my GitHub account (putting the code here would make the post difficult to read). Please look at this link

Note 3:

This question may seem identical to the one which link follows, but it is not the case: Absolutely, definitely, preventing page break

The answer given to the question which link is given above applies to a list of "entire elements". I mean : an entire paragraph, an entire list of items, or an entire table, for example. You can make sure that a paragraph and a table (that is: 2 "entire elements") are printed on the same page, for example.

But can you make sure that an "entire element" and "some part of another element" are printed on the same page ?

For example : I want to make sure that a title (which is a paragraph - an "entire element") and, at least, the first 2 lines of the table that follows (some part of an element) will be printed on the same page ?

I tried, using the macro shown below (as suggested by the response mentioned earlier).

% See https://tex.stackexchange.com/questions/94699/absolutely-definitely-preventing-page-break
\newenvironment{absolutelynopagebreak}{
\par\nobreak\vfil\penalty0\vfilneg
\vtop\bgroup
}{\par\xdef\tpd{\the\prevdepth}\egroup\prevdepth=\tpd}


I also tried to use the "minipage" container (also as suggested by the response mentioned earlier).

I found a workaround that works for lists only. Instead of declaring one long list, you break the list into smaller parts. This is not a fully satisfying workaround because, in another context it may not work well. See this example and the result.

However, this workaround does not work for tables which columns widths are not fixed. Since the widths of the columns are not fixed, the same column may have different widths, depending on the part of the table. The this example and the result.

This fundamental problem seems to be related to the syntax of the LaTeX code itself : "begin/end" statements must be balanced. Therefore you can't interfere with the structure of an "entire element" (a full list of a full table). You are forced to split it into smaller parts.

Any idea ?

Thanks.

# ANNEXE

## list.tex

\documentclass{class}

% See http://borntocode.fr/latex-customisation-de-listes-a-puces/
\newenvironment{packed_enum}{
\begin{itemize}[topsep=0pt, itemsep=1pt, parsep=0pt, partopsep=0pt]%
}{\end{itemize}}

% See https://tex.stackexchange.com/questions/94699/absolutely-definitely-preventing-page-break
\newenvironment{absolutelynopagebreak}{
\par\nobreak\vfil\penalty0\vfilneg
\vtop\bgroup
}{\par\xdef\tpd{\the\prevdepth}\egroup\prevdepth=\tpd}

\begin{document}

% Nothing prevents the title to be printed on one page and the 10 items on
% the next page. I would like to make sure that the title and, at least 2
% items, will be printed on the same page.

Title
\vspace{15pt}

\begin{packed_enum}
\item Iten 1
\item Iten 2
\item Iten 3
\item Iten 4
\item Iten 5
\item Iten 6
\item Iten 7
\item Iten 8
\item Iten 9
\item Iten 10
\item Iten 11
\item Iten 12
\item Iten 13
\item Iten 14
\item Iten 15
\item Iten 16
\item Iten 17
\item Iten 18
\end{packed_enum}
\vspace{5cm}

% One solution, that works for the list.

\begin{absolutelynopagebreak}
Title
\vspace{15pt}

\begin{packed_enum}         % <= first list
\item Iten 1
\item Iten 2
\end{packed_enum}
\end{absolutelynopagebreak}
\begin{packed_enum}            % <= second list
\item Iten 3
\item Iten 4
\item Iten 5
\item Iten 6
\item Iten 7
\item Iten 8
\item Iten 9
\item Iten 10
\item Iten 11
\item Iten 12
\item Iten 13
\item Iten 14
\item Iten 15
\item Iten 16
\item Iten 17
\item Iten 18
\end{packed_enum}

\end{document}


## table.tex

\documentclass{class}

% ---------------------------------------------------------------------------
% I define fiew environments.
% ---------------------------------------------------------------------------

% See https://www.sharelatex.com/learn/Tables
\newenvironment{packed_tabular}{
\setlength{\tabcolsep}{0pt}
}

% See https://tex.stackexchange.com/questions/94699/absolutely-definitely-preventing-page-break
\newenvironment{absolutelynopagebreak}{
\par\nobreak\vfil\penalty0\vfilneg
\vtop\bgroup
}{\par\xdef\tpd{\the\prevdepth}\egroup\prevdepth=\tpd}

\begin{document}

% 1. Nothing prevents the title to be printed on one page and the 5 lines of
%    the table to be printed on the next page. I would like to make sure that
%    the title and, at least 2 lines, will be printed on the same page.
% 2. The table cannot be broken. All rows are printed the one after the other...
%    even if the end of the text area is reached.

Title
\vspace{15pt}

\begin{packed_tabular}
\begin{tabular}{lcl}
a   & ~ & experience 0 \\
aa  & ~ & experience 1 \\
aaa & ~ & experience 2 \\
a   & ~ & experience 3 \\
aa  & ~ & experience 4 \\
aaa & ~ & experience 5 \\
a   & ~ & experience 6 \\
aa  & ~ & experience 7 \\
aaa & ~ & experience 8 \\
a   & ~ & experience 9 \\
aa  & ~ & experience 10 \\
aaa & ~ & experience 11 \\
a   & ~ & experience 12 \\
aa  & ~ & experience 13 \\
aaa & ~ & experience 14 \\
a   & ~ & experience 15 \\
aa  & ~ & experience 16 \\
aaa & ~ & experience 17 \\
\end{tabular}%
\end{packed_tabular}%

% One solution may be to split the table into smaller tables.
% But this solution does not work **well**.
% The widths of the columns are not fixed.
% Therefore the tables may have different widths for the same columns.

\begin{absolutelynopagebreak}
Title
\vspace{15pt}

\begin{packed_tabular}
\begin{tabular}{|l|c|l|}
a   & ~ & experience 0 \\
aa  & ~ & experience 1 \\
aaa & ~ & experience 2 \\
a   & ~ & experience 3 \\
\end{tabular}%
\end{packed_tabular}%
\end{absolutelynopagebreak}%
%
\begin{absolutelynopagebreak}
\begin{packed_tabular}%
\begin{tabular}{|l|c|l|}%
a & ~ & experience 4 \\
a & ~ & experience 5 \\
\end{tabular}%
\end{packed_tabular}%
\end{absolutelynopagebreak}%
%
\begin{absolutelynopagebreak}%
\begin{packed_tabular}%
\begin{tabular}{|l|c|l|}%
a & ~ & experience 6 \\
a & ~ & experience 7 \\
a & ~ & experience 8 \\
a & ~ & experience 9 \\
a & ~ & experience 10 \\
a & ~ & experience 11 \\
a & ~ & experience 12 \\
a & ~ & experience 13 \\
a & ~ & experience 14 \\
a & ~ & experience 15 \\
a & ~ & experience 16 \\
a & ~ & experience 17 \\
\end{tabular}%
\end{packed_tabular}%
\end{absolutelynopagebreak}%

\end{document}


## class.cls

% This file contains LaTeX configuration for all CV.

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{class}[1995/10/30 Standard LaTeX minimal class]

% This class "article" defines many important things.
% See https://tex.stackexchange.com/questions/440462/hline-does-not-produce-anything/440468#440468

% [geometry] This package provides a flexible and easy interface to page dimensions.
\usepackage[a4paper,
% We set the dimensions of the left and right margins.
left=1cm,
right=1cm,
% We set the dimensions of the top and bottom margins.
top=12cm,
bottom=12cm]{geometry}

% We (must) redefine the font size named "normalsize" (this is mandatory).
% Please note that LaTeX defines other names for font sizes (for example: tiny, small...).
% See https://www.sharelatex.com/learn/Font_sizes,_families,_and_styles#Reference_guide
% WARNING: Make sure to redefine the command "\normalsize" before loading the package "fontspec".
\renewcommand{\normalsize}{\fontsize{10pt}{12pt}\selectfont}

% This package allows the use of formatting features that are required to draw tables.
\usepackage{array}

% This package provides flexibility for fine-tuning the enumerated lists.
% See https://texfaq.org/FAQ-complist
\usepackage{enumitem}

% This package is really awasome.
\usepackage{tcolorbox}

% This will print a frame around the text area.
\usepackage{showframe}


## The Makefile

# This document illustrates the use of the "make" tool to produce PDF files from LaTeX sources.
#
#      Generate the PDF file:                              make all
#      Clean all the temporary files:                      make clean
#      Remove all temporary files, including the PDF file: make clear

LATEX_EXE     = /usr/bin/lualatex
LATEX_OPTIONS = -halt-on-error -shell-escape -interaction=nonstopmode --output-format=pdf
PDF_VIEWER    = xdg-open

list.pdf: list.tex class.cls
${LATEX_EXE}${LATEX_OPTIONS} $< && echo "SUCCESS" &&${PDF_VIEWER} $@ table.pdf: table.tex class.cls${LATEX_EXE} ${LATEX_OPTIONS}$< && echo "SUCCESS" && ${PDF_VIEWER}$@

all: list.pdf table.pdf

clean:
rm -f *.log *.out *.aux

clear: clean
rm -f *.pdf

• In case you are manually writing your document then usually it is the easiest to play around with the environments and the text until you like the layout. If you try to impose strict requirements from within the code you run into all kinds of trouble, and besides it goes against the LaTeX philosophy that you provide the content and LaTeX arranges the 'best' placement of elements on the pages. If you are generating documents automatically then you need different solutions, but problems with overfull pages or page breaks within tables may be unavoidable. – Marijn Jul 14 '18 at 10:12
• You shouldn't link to external code like this. The question should be self contained and not break in a few days when you decide to change or remove the code on github. – Ulrike Fischer Jul 14 '18 at 10:45
• Regarding the tabular: use longtable if you want the tabular to split over pages. You can use \\*  to suppress page breaks if needed. – Ulrike Fischer Jul 14 '18 at 10:49
• Thanks for all your responses. I try to automate the creation of nice resumes (as part of a free software). The idea is: the user gives the data, and LaTeX is responsible for the rendering (given a LaTeX template, the data, and a script that inject the data into the template). The user is not supposed to know LateX, thus I try to automate things as much as possible. – Denis Beurive Jul 14 '18 at 11:12
• @Ulrike Fischer: I included the codes within the post. However, the documents published on my GitHub account should remain available (unless GitHub disappears). – Denis Beurive Jul 14 '18 at 12:35