Is it possible to redefine the section-command so that it automatically starts on a new page if say less than a quarter of the current page remains?

Below is a minimal working example. In this case LaTeX allows placing a heading under which just two lines of text from the following section can be placed before the pagebreak. This is probably fins for most texts, but is quite disturbing in mathematical texts when the text below the heading is for example a theorem. I know I can fix this manually by inserting \newpage och \clearpage when needed, but is it possible to fix this automatically?



Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip  ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 

\section{Bad placed section}

  • If you want more lines to keep together after a section heading, you can say for instance \interlinepenalties 5 10000 10000 10000 10000 10000 after the section command to keep five lines together. – Stephan Lehmke May 6 '13 at 19:13
  • 2
    The needspace package allows you to specify how much space you need before executing a command. If it doesn't have that space, it will clear the page before executing the command. – Steven B. Segletes May 6 '13 at 19:14