Let's say my document has only three elements; questions and answers
(that always come in pairs), varying number of which are collected into
groups ("sections") with a section heading at the start of each group.
So, my source text has a structure like:
\sectionhead{First section heading}
\question{Question text}
\answer{Answer text}
\question{Question text}
\answer{Answer text}
\question{Question text}
\answer{Answer text}
\sectionhead{Second section heading}
\question{Question text}
\answer{Answer text}
\question{Question text}
\answer{Answer text}
and so on. I want a question and its answer to be always on the same
page, which I can accomplish by coding
\def\question#1{\filbreak --formatting stuff-- #1}
\def\answer#1{--formatting stuff-- #1 \filbreak}
But I'd also like section heads to always have at least one question
after them, or else be moved to the top of the next page. Is there a
clever way to do this? -- other than having the first question after
each section head be entered instead as
\sectionhead{Section heading text}
\firstquestion{Question text{
\answer{Answer text{
with \def\firstquestion being the same as \def\question except for no
initial \filbreak.
Is there a more savvy way to do this?
\def\
Let's put it together:
- You want section-heads to have at least one question
after them.
- You want question and answer to be always on same page.
-> You want section-heads to have at least one question
_and one answer_ after them all on the same page.
If you don't use \filbreak anywhere else in your document but
within \question and \answer, you could override the definition
of \filbreak within your \sectionhead-producing-macro so that a
subsequent \filbreak (which is delivered by \question) does not
affect page-breaking but restore the old definition for
subsubsequent \filbreak (which is delivered by \answer).
\def\mysectionhead#1{%
% perform two \filbreak so that in case of subsequent section-
% headings the first \filbreak will restore the definition for
% the second one:
\filbreak\filbreak
% redefine \filbreak to do nothing but restore the old
% definition: -> Definition will be restored by subsequent
% \question; restored-definition will be first-time executed
% by subsubsequent \answer
\def\filbreak{%
\relax
\def\filbreak{\par\vfil\penalty -200\vfilneg}%
}%
\sectionhead{#1}%
}%
\def\question#1{\filbreak --formatting stuff-- #1}
\def\answer#1{--formatting stuff-- #1 \filbreak}
(If you implement grouping in the sense of TeX, you might have to
work with \global definitions.)
Ulrich
% Start a new paragraph but prevent page-break:
\par\nobreak
}%
\def\question#1{\filbreak --formatting stuff-- #1}
\def\answer#1{--formatting stuff-- #1 \filbreak}
(If you implement grouping in the sense of TeX, you might have to
> So, my source text has a structure like:
>
> \sectionhead{First section heading}
>
> \question{Question text}
> \answer{Answer text}
>
> \question{Question text}
> \answer{Answer text}
>
> \question{Question text}
> \answer{Answer text}
>
> \sectionhead{Second section heading}
>
> \question{Question text}
> \answer{Answer text}
>
> \question{Question text}
> \answer{Answer text}
>
> and so on. I want a question and its answer to be always on the same
> page, which I can accomplish by coding
>
> \def\question#1{\filbreak --formatting stuff-- #1}
>
> \def\answer#1{--formatting stuff-- #1 \filbreak}
If your source file really is exactly as you said. This puts two
\filbreaks in a row except before the first question and after the last
answer in a section.
>
> But I'd also like section heads to always have at least one question
> after them, or else be moved to the top of the next page. Is there a
> clever way to do this?
Remove the \filbreak from \question.
Dan