3

I'm working on a screenplay and the rules are fairly strict as to formatting. I need to force a new page if there are less than three lines to the end of the page and the number of lines in an argument is greater than one line.

The macro is:

\newcommand{\dialogue}[2]{%
  \begin{center}#1\end{center}
  #2
}

A non-working illustration of what I want:

\newcommand{\dialogue}[2]{%
  \iflinesleftonpageis{<3}{%
    \iflinesinargument#1is{>1}{%
      \newpage}{}
    }{}
  }
  \begin{center}#1\end{center}
  #2
}

Any ideas on how to achieve this?

Edit

@DavidCarlisle suggested using the needspace package. By modifying my macro to the following I was able to produce the correct result:

\newcommand{\dialogue}[2]{%
  \needspace{3em}%
  \begin{center}#1\end{center}%
  #2
}

3em approximates three lines of text; if that space does not exist, it prints the macro on the following page.

Many thanks to David!

  • What if you have 2 lines left on the page and argument #1 is (say) 5 lines long? Your only condition for breaking the page is \iflinesleftonpageis{>3}, hence my question. – Werner Jul 19 '17 at 3:49
  • @Werner — I'm an idiot. I meant {<3}. – experimenthouse Jul 19 '17 at 4:00
  • see the needspace package, also add missing % to the ends of lines in your definitions. – David Carlisle Jul 19 '17 at 8:00
  • the normal way to express this in tex is not in terms of number of lines left on page (that isn't really how tex works, although \needspace gives an approximation to that) but rather to give the requirement that the paragraph can not be broken in the first three lines, If you prevent a line break at the start of a paragraph (\clubpenalties) then naturally the page will break short if no more than two lines would fit. – David Carlisle Jul 19 '17 at 8:04
  • @DavidCarlisle — I can see instances where needspace would not produce a desired result. However, it works for what I'm using. I set \needspace{3em} and across all 70 pages it has produced exactly what I was after. Thank you! – experimenthouse Jul 19 '17 at 12:10
3

TeX does not really work in terms of "number of lines left", page breaking happens (conceptually) later, after all macros have been expanded, and after all line breaking, so first the text is set on one long infinite scroll which is then cut into pages.

If you specify that pages may be left short, and that a page may not break after your centered heading, or after the first line of a paragraph, then your requirement that a heading moves to the next page if there is not room for at least one line of the following text should be followed, so

\raggedbottom
\clubpenalty=10000
\newcommand{\dialogue}[2]{%
  \begin{center}#1\end{center}%
  \nopagebreak
  #2%
}

If you would rather think in terms of "amount of space on the page" the needspace package does quite a good job of providing a command that offers this, even though it doesn't fit well with TeX's working model. If you add something like

\needspace{3cm}

before your heading, then a page break will be forced if you are (probably) closer than 3cm to the bottom of the page. (untested as you didn't supply a test file)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.