# Why does (La)TeX break the page in this situation?

I have the following code:

\documentclass{article}
\usepackage[cp1252]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{environ}
\usepackage{tabularx,array,booktabs}

\NewEnviron{woche}[2]{
\paragraph*{Woche vom #1 bis #2}~\nopagebreak
\vspace{1em}\\*
\tabularx{0.97\textwidth}{m{0.9cm}X}
\toprule
\BODY
\bottomrule
}[
\endtabularx]

\begin{document}
\vspace*{16cm}
\begin{woche}{2}{3}
28.11. & Was ich alles an dem ersten Tag dieser Woche getan habe! \\
29.11. & Der zweite Tag wa recht lang. \\
30.11. & Nur Doku geschrieben \\
01.12. & Ich freie mich schon auf's Wochenende, aber noch sind wir nicht soweit. Ich muss noch ganze zwei Tage runterreißen. \\
02.12. & Letzter Tag! Endlich! \\
\end{woche}
\end{document}


Now if I compile the file using pdfLaTeX I get two pages. On the first there is the heading Woche 2 bis 3 and on the second there is the table. The vspace is just to simulate some other text or things. How can I modify the environment to get it running?

-
Put a \par after the \endtabularx command. (It looks as if the grouping introduced by your environment confuses the page breaking). –  Ulrike Fischer Feb 13 '12 at 16:30

The usual setting for \paragraph is that it typesets an inline heading. Change it as to be like \section, for example with the titlesec package:

\usepackage{titlesec}
\titleformat{\paragraph}{}{}{0pt}{\bfseries}
\titlespacing*{\paragraph}{0pt}{\baselineskip}{1.5ex}
\usepackage{environ}
\usepackage{tabularx,array,booktabs}

\NewEnviron{woche}[2]{%
\paragraph*{Woche vom #1 bis #2}
\tabularx{\textwidth}{m{0.9cm}X}
\toprule
\BODY
\bottomrule
}[\endtabularx\par]


Alternatively, put the heading inside the tabularx and forget about \paragraph:

\NewEnviron{woche}[2]{%
\noindent\tabularx{\textwidth}{m{0.9cm}X}
\multicolumn{2}{@{}l@{}}{\bfseries Woche vom #1 bis #2}\\[1.5ex]
\toprule
\BODY
\bottomrule}
[\endtabularx\par]


This seems to be the cleanest solution.

-

Do you always want a line break after the heading? After a display heading LaTex goes to some trouble to prevent a page break, and will, as you wish, pull the entire heading on to the next page, you get that effect if you changed your code to say

 \section*{Woche vom #1 bis #2}

\tabularx{0.97\textwidth}{m{0.9cm}X}


So simplest is to define \paragraph* to be a display heading rather than run-in. If you want most paragraphs to have a run-in heading unless the next thing is a big item like a table, then @ulrike-fischer's comment (which I just noticed) is probably your answer.
I don't know if my comment is really the answer. In this case it works (and it was quite interesting to see that the grouping has this effect) but TeX can be quite stubborn and insert a page break at  \\  regardless of \nobreaks or \nopagebreaks or * used to prevent it. So I think your and Enrico's suggestion to use a real display heading command is the real answer. –  Ulrike Fischer Feb 14 '12 at 8:54
The \vspace*{16cm} is forcing the table to the next page. Test it with 1cm and then adjust to taste!