For my homework, I use this enviroment:
\newcounter{exercise}
\renewcommand{\theexercise}{\arabic{exercise}}
\newenvironment{exercise}[1]
{\smallskip\refstepcounter{exercise}\textbf{Solution to exercise~\theexercise:}\par\vspace{#1}}
{\par \hspace*{\fill}$\square$\medskip}
Sometimes, I have that 'Solution to exercise x' is at the end of the page and the solution starts at the next page or that the '$square$' at the end starts at the next page. I wonder if there is a possibility to let tex check if the next line would start at the next page and then force it to put 'Solution to exercise x' also onto the next page, or in the other case that it would check, if the '$square$' would start at the next page and take the line before that also to the next page.
Code that still causes problems:
\begin{center}
\begin{tikzpicture}[point/.style={fill, circle, inner sep = 1.3pt}]
%x axis
\draw[->] (-2,0) -- (6,0) node[below] {$x$};
\foreach \x in {-1,1,2,...,5}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
%y axis
\draw[->] (0,-2) -- (0,3) node[left] {$y$};
\foreach \y in {-1,1,2}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
\node[below left] at (0,0) {\footnotesize $0$};
% points
\node (H) [point,label=above:$H$] at (1,1) {};
\node (F) [point,label=above:$F$] at (4,2) {};
\node (X) [point,label={[label distance=0.15cm]350:$x^*$}] at (2,0) {};
% river
\draw[blue, line width = 0.4mm] (-1.5,0) -- (5.5,0);
% path
\draw[dashed] (H) -- (X);
\draw[dashed] (X) -- (F);
\end{tikzpicture}
\end{center}





\nopagebreak... – user202729 Dec 11 '21 at 14:04\itemmakes a difference. – user202729 Dec 11 '21 at 14:05\par\vspace{#1}solves the first problem but not the second, but thank you! Do you know, how to solve the second problem? – question Dec 11 '21 at 15:31