[Tex/LaTex] How to force new page if paragraph will break

page-breakingparagraphs

I am working on a project where a book gets generated from a JSON file with thousands of entries which are used as paragraphs. When I plug my code into LaTeX some paragraphs are split across two pages. I was wondering if there is a way to say something like: “If paragraph will break, move the whole of it to the next page”?

Something like \newpage will not be useful in my case because there are too many entries to do it by hand. I'm looking for a more programmatic solution. I have also looked at:

\widowpenalties 1 10000
\raggedbottom

But what that seems to do in my case is to just expand the text until the page is finished and not propagating the text to new page. Here a simplified version of my code with the formatting that is being used:

%------------------------
% Invoke Packages
%------------------------

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{lipsum,lineno}

%------------------------
% Set Dimensions
%------------------------
\usepackage[ 
paperwidth = 168.3mm,
paperheight = 260.4mm,
top = 6mm,
bottom = 7mm,
outer = 6mm,
inner = 20mm
]{geometry}

%------------------------
% Remove indentation from all paragraphs
%------------------------
\setlength\parindent{0pt}

% Comments
\newcommand{\CommentFontSize}{21}
\newcommand{\CommentSkipMult}{25}
% Author
\newcommand{\UserFontSize}{16}
\newcommand{\UserSkipMult}{0}
% Date
\newcommand{\DateFontSize}{15}
\newcommand{\DateSkipMult}{0}

% Comments
\newenvironment{commentTextFont}{
\fontfamily{mdugm} 
\fontsize{\CommentFontSize}{\CommentSkipMult}
\selectfont}
{\par}
% Author
\newenvironment{userFont}{
\fontfamily{mdugm} 
\fontsize{\UserFontSize}{\UserSkipMult}
\selectfont}
{\par}
% Date
\newenvironment{dateFont}{
\fontfamily{mdugm} 
\fontsize{\DateFontSize}{\DateSkipMult}
\selectfont}
{\par}

%------------------------
% Set commands 
%------------------------

\newcommand{\uComment}[3]{
\begin{commentTextFont} #1 \end{commentTextFont}
\vspace*{0.5cm}
\begin{userFont} \textit{#2}
\hspace*{\fill} \begin{dateFont} #3 \end{dateFont}
\end{userFont}
\vspace*{0.8cm}
}

%------------------------
% Document
%------------------------

\begin{document}
\begin{flushleft}

\uComment{\lipsum[73]}{someone}{2000}
\uComment{\lipsum[75]}{someone}{2000}
\uComment{\lipsum[66]}{someone}{2000}

\end{flushleft}
\end{document}

Any ideas how this could be approached?

Best Answer

The definitions you use are quite complicated and could be simplified, in my opinion, but the solution is anyway easy:

\newcommand{\uComment}[3]{%
  \filbreak
  \begin{commentTextFont}#1\end{commentTextFont}%
  \vspace*{0.5cm}
  \begin{userFont}\textit{#2}\hspace*{\fill}%
  \begin{dateFont}#3\end{dateFont}\end{userFont}%
  \vspace*{0.8cm}
}

I used \filbreak before the paragraph, which means: break a page here unless a further \filbreak comes along and the text in between fits the page.

Here's the complete code, with all spurious spaces you insert removed.

%------------------------
% Invoke Packages
%------------------------

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{lipsum,lineno}

%------------------------
% Set Dimensions
%------------------------
\usepackage[ 
  paperwidth = 168.3mm,
  paperheight = 260.4mm,
  top = 6mm,
  bottom = 7mm,
  outer = 6mm,
  inner = 20mm
]{geometry}

%------------------------
% Remove indentation from all paragraphs
%------------------------
\setlength\parindent{0pt}

% Comments
\newcommand{\CommentFontSize}{21}
\newcommand{\CommentSkipMult}{25}
% Author
\newcommand{\UserFontSize}{16}
\newcommand{\UserSkipMult}{0}
% Date
\newcommand{\DateFontSize}{15}
\newcommand{\DateSkipMult}{0}

% Comments
\newenvironment{commentTextFont}
 {\fontfamily{mdugm}%
  \fontsize{\CommentFontSize}{\CommentSkipMult}%
  \selectfont}
 {\par}
% Author
\newenvironment{userFont}
 {\fontfamily{mdugm}%
  \fontsize{\UserFontSize}{\UserSkipMult}%
  \selectfont}
 {\par}
% Date
\newenvironment{dateFont}
 {\fontfamily{mdugm}%
  \fontsize{\DateFontSize}{\DateSkipMult}%
  \selectfont}
 {\par}

%------------------------
% Set commands 
%------------------------

\newcommand{\uComment}[3]{%
  \filbreak
  \begin{commentTextFont}#1\end{commentTextFont}%
  \vspace*{0.5cm}
  \begin{userFont}\textit{#2}\hspace*{\fill}%
  \begin{dateFont}#3\end{dateFont}\end{userFont}%
  \vspace*{0.8cm}
}

%------------------------
% Document
%------------------------

\begin{document}

\raggedright

\uComment{\lipsum[73]}{someone}{2000}
\uComment{\lipsum[75]}{someone}{2000}
\uComment{\lipsum[66]}{someone}{2000}

\end{document}

A picture of the page break.

enter image description here