# No page break between a command and the following paragraph

For a project, I have created a sectioning level called \numsubpara using an independent counter.

Unfortunately, quite often I get this situation :

I would like to avoid as much as possible this page break. In this case, I would like my title to be on the next page.

I know this feature exists with the usual \section, \subsection etc. But in this case, I cannot use these commands, I have to stick to my own command.

The document I am creating being quite long, it has to be automatic, i.e. I cannot manually add \pagebreak before lonely \numsubpara commands.

Does anyone know how to do this? Thank you in advance !

Here is my MWE :

\documentclass[12pt,twoside]{book}
\usepackage[paperheight=240mm,paperwidth=160mm, left=22mm, right = 20mm, top = 20mm, bottom = 22mm]{geometry}

\usepackage[cam,width=17.5truecm,height=25.54truecm,center,dvips,noinfo]{crop}
\usepackage[french]{babel}

\usepackage{fontspec}
\usepackage{lipsum}
\usepackage{titlesec}

%\usepackage{needspace}
%\newcounter{subpara}
%\setcounter{subpara}{0}
%\newcommand\numsubpara[1]%
%{\vspace{3.36mm}\par\refstepcounter{subpara}%
%\needspace{2em}%
%\fontsize{10}{12}\selectfont\textbf{\thesubpara.}\fontsize{12}{12}\selectfont%
%\space\textbf{#1}\space\vspace{2.8mm}\\%
%\fontsize{12}{12}\selectfont\nobreak%
%}

\newcounter{subpara}
\setcounter{subpara}{0}
\newcommand\numsubpara[1]%
{\vspace{3.36mm}\par\refstepcounter{subpara}%
\fontsize{10}{12}\selectfont\textbf{\thesubpara.}\fontsize{12}{12}\selectfont%
\space\textbf{#1}\space\vspace{2.8mm}\\%
\fontsize{12}{12}\selectfont\nobreak%
}

\newcommand{\nocontentsline}[3]{}
\newcommand{\hnumsubpara}{\tocless\numsubpara}

\begin{document}

\chapter{Test}
\section{Test}
\hnumsubpara{Test}
\lipsum[1]

\hnumsubpara{My command\footnote{\lipsum[1]}}
\lipsum[1]

\tableofcontents

\end{document}


I would suggest using the titlesec package. Also, I would recommend not using vspace nor hard coded font values but I'm not a \LaTeX guru and you may have your reasons to do so. I changed you custom paragraph but it looks quite the same.

Here is what you could use :

\documentclass[12pt,twoside]{book}
\usepackage[paperheight=240mm,paperwidth=160mm, left=22mm, right = 20mm, top = 20mm, bottom = 22mm]{geometry}

\usepackage[cam,width=17.5truecm,height=25.54truecm,center,dvips,noinfo]{crop}
\usepackage[french]{babel}

\usepackage{fontspec}

\usepackage{lipsum}

\usepackage{titlesec}

\titleclass{\numsubpara}{straight}[\chapter]
\newcounter{numsubpara}

\titleformat{\numsubpara}{}{}{0em}{\bfseries{\footnotesize\thenumsubpara.}\space}
\titlespacing*{\numsubpara}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\begin{document}

\numsubpara{Test}
Test

Test

Test

Test

Test

Test

Test

Test

Test

Test

Test
\lipsum[1-2]

\numsubpara{My command}

\lipsum[1]

\end{document}

• Working great, thank you ! You are right, \titlespacing is way better than \vspace. Double thanks :). – Guliup May 22 at 16:09
• I have one remaining question: I dont want \numsubpara not to be shown in the ToC. It seems that in this case it is appearing, do you know how to avoid this ? Thank you ! – Guliup May 22 at 21:37
• Interesting, we are getting close, thank you ! One last thing and it would be perfect: how to deal with footnotes? I have edited my MWE, I get this result with your code : imgur.com/09Relt1 – Guliup May 22 at 22:49
• I am now talking about something I never experienced myself. A few minutes research gave me this where it is adviced not to use footnotes in section headings and this where you learn a package can take care of this for you. So, basically, just add \usepackage[stable]{footmisc} to your preamble. – Ivan May 23 at 10:07

Your example shows several mistakes. There is misunderstanding of TeX modes. The alternation of vertical and horizontal modes in TeX belongs to the basic principles of TeX.

\newcommand\numsubpara[1]%
{\vspace{3.36mm}\par\refstepcounter{subpara}%
\fontsize{10}{12}\selectfont\textbf{\thesubpara.}\fontsize{12}{12}\selectfont%
\space\textbf{#1}\space\vspace{2.8mm}\\%
\fontsize{12}{12}\selectfont\nobreak%
}


The first \vspace depends on the fact you have or have not the empty line before usage of \numsubpara. If yes, then you are in vertical mode and \vsapce behaves like \vskip. If not, then you are in the horizontal mode and \vspace behaves like \vsadjust{\vskip...} in horizontal mode. The following \par closes the horizontal mode, so the visual effect is the same. But why is this complication here? Next, you are switching to smaller font explicitly by \fontsize and return back to current size explicitly by \fontsize. This is not good practice, we have TeX groups {...} for this task and use only one font switch inside such group. Main problem is that the title is followed by \vspace in horizontal mode (it behaves like \vadjust{\vskip}, then there is \\ which behaves like \hfil\break but it does not close horizontal mode. The \fonsize is irrelevant here and \nobreak is in horizontal mode. You did not put \nobreak to vertical mode. This is your main problem.

\newcount\subparanum
\def\numsubpara#1{%
\par                  % closes previous horizontal mode ebentualy
\vskip 3.36cm \relax  % puts vertical space
\global\advance\subparanum by1 % increments number of \numsubpara
\indent               % opens horizontal mode
\llap{\fontsize... \the\subparanum. }%  % prints number in smaller font in the TeX group and in llaped \hbox
{\bf #1\par}          % prints the title and closes horizontal mode
\nobreak              % this is in vertical mode, following space is protected
\vskip 2.8mm \relax   % desired space, page break cannot be here
}


Note that the space is in \llap after dot, so each title will have its left boundary at \indent horizontal position. Spaces are typicaly stretchable and shrinkable, but space imprisoned in \llap box is constant.

Without detail understanding of vertical/horizontal modes and box principles in TeX is almost impossible to create a good macro for the title. Last note: the vertical spaces are typically designed in respect to baseline grid, no as accidental dimensions in mm.

• Thank you for your help, I obviously have a lot to learn... It is much clearer now, thank you again ! – Guliup May 23 at 12:04
• Hello again @wipet. I have tried your answer, are you sure you get the same answer than me ? Here is the counter I have using your code (it is not working) : imgur.com/zEPmWCl – Guliup May 27 at 14:50
• Sorry, I don't understand. What is not working? I am unable to reproduce your problem. – wipet May 27 at 16:09
• Well, when I copy and paste your command instead of mine in my MWE, this is what I get. The counter does not count (it stays equal to 0) and "y1" is added before. – Guliup May 27 at 17:18
• My "command" includes three dots. It means that you need to add something LaTeX specific about font sizes here. What did you do with these three dots? And what exacly means "copy instead mine"? What lines were deleted, what lines were added? – wipet May 27 at 18:22

To solve this issue you can use the needspace package. It checks if you have enough space and if not adds a break page command to keep the title with the rest of the text.

So you MWE will became:

\documentclass[12pt,twoside]{book}
\usepackage[paperheight=240mm,paperwidth=160mm, left=22mm, right = 20mm, top = 20mm, bottom = 22mm]{geometry}

\usepackage[cam,width=17.5truecm,height=25.54truecm,center,dvips,noinfo]{crop}
\usepackage[french]{babel}

\usepackage{fontspec}

\usepackage{lipsum}
\usepackage{needspace}

\newcounter{subpara}
\setcounter{subpara}{0}
\newcommand\numsubpara[1]%
{\vspace{3.36mm}\par\refstepcounter{subpara}%
\needspace{6em}%
\fontsize{10}{12}\selectfont\textbf{\thesubpara.}\fontsize{12}{12}\selectfont%
\space\textbf{#1}\space\vspace{2.8mm}\\%
\fontsize{12}{12}\selectfont\nobreak%
}

\begin{document}

\numsubpara{Test}

Test

Test

Test

Test

Test

\lipsum[1-3]

\numsubpara{My command}

\lipsum[1]

\end{document}


I HTH.

• Looks perfect, thank you ! – Guliup May 22 at 16:10
• Ive talked too fast : it seems that it is not working when there are footnotes. I have edited my MWE, you will see that my "2. My command" is alone at the end of the first page. – Guliup May 22 at 21:34
• This is because you have 2em on the bottom of the page but you fill it with the footnote. You can tweak with the amount of space to accommodate this case but this will have the side effect of leaving a lot of space at the end of the page in most of the other cases. What I would do is to add an extra parameter with 2em as the default value to your command as in: – Walter Cazzola May 22 at 22:33
• \newcommand\numsubpara[2][2em]% {\vspace{3.36mm}\par\refstepcounter{subpara}% \needspace{#1}% \fontsize{10}{12}\selectfont\textbf{\thesubpara.}\fontsize{12}{12}\selectfont% \space\textbf{#2}\space\vspace{2.8mm}\\% \fontsize{12}{12}\selectfont\nobreak% }  them \numsubpara[5em]{My command\footnote{\lipsum[1]}} does the trick for me but with a quite large empty space at the bottom of first page – Walter Cazzola May 22 at 22:35
• Arf, I need it to be automatic... But thank you for your help ! – Guliup May 22 at 22:51