summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-nopagebrk.html
blob: 69ca8a59ea234c88e53c4701b76b9c38e34476ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<head>
<title>UK TeX FAQ -- question label nopagebrk</title>
</head><body>
<h3>Preventing page breaks between lines</h3>
<p/>One commonly requires that a block of typeset material be kept on the
same page; it turns out to be surprisingly tricky to arrange this.
<p/>LaTeX provides a <code>samepage</code> environment which claims it
does this sort of thing for you.  It proceeds by setting infinite
penalties for all sorts of page-break situations; but in many
situations where you want to prevent a page break,
<code>samepage</code> doesn&rsquo;t help.  If you&rsquo;re trying to keep running
text together, you need to end the paragraph inside the environment
(see <a href="FAQ-paraparam.html">preserving paragraph parameters</a>).
Also, if the things you are trying to keep together insert their own
pagebreak hints, <code>samepage</code> has no power over them: a good
example is list items &mdash; they suggest page breaks between them.  Even
if <code>samepage</code> <em>does</em> work, it&rsquo;s likely to leave stuff
jutting out at the bottom of the page.
<p/>A convenient trick is to set all the relevant stuff in a <code>\</code><code>parbox</code>
(or a <code>minipage</code> if it contains things like verbatim text
that may not be in the argument of a <code>\</code><code>parbox</code>).  The resulting box
certainly <em>won&rsquo;t</em> break between pages, but that&rsquo;s not to say that
it will actually do what you want it to do: again, the box may be left
jutting out at the bottom of the page.
<p/>Why do neither of these obvious things work?  Because TeX can&rsquo;t
really distinguish between infinitely awful things.
<code>Samepage</code> will make any possible break point &ldquo;infinitely
bad&rdquo; and boxes don&rsquo;t even offer the option of breaks, but if the
alternative is the leave an infinitely bad few centimetres of blank
paper at the bottom of the page, TeX will take the line of least
resistance and do nothing.
<p/>This problem still arises even if you have <code>\</code><code>raggedbottom</code> in
effect: TeX doesn&rsquo;t notice the value of <em>that</em> until it starts
actually shipping a page out.  One approach is to set:
<blockquote>
<pre>
\raggedbottom
\addtolength{\topskip}{0pt plus 10pt}
</pre>
</blockquote><p>
The <code>10pt</code> offers a hint to the output routine that the column is
stretchable; this will cause TeX to be more tolerant of the need to
stretch while building the page.  If you&rsquo;re doing this as a temporary
measure, cancel the change to <code>\</code><code>topskip</code> by:
<blockquote>
<pre>
\addtolength{\topskip}{0pt plus-10pt}
</pre>
</blockquote><p>
as well as resetting <code>\</code><code>flushbottom</code>.  (Note that the <code>10pt</code> never
actually shows up, because it is overwhelmed when the page is shipped
out by the stretchability introduced by <code>\</code><code>raggedbottom</code>; however, it
could well have an effect if <code>\</code><code>flushbottom</code> was in effect.)
<p/>An alternative (which derives from a suggestion by Knuth in the
TeXbook) is the package <i>needspace</i> or the <i>memoir</i> class,
which both define a command <code>\</code><code>needspace</code> whose argument tells it
what space is needed.  If the space isn&rsquo;t available, the current page
is cleared, and the matter that needs to be kept together will be
inserted on the new page.  For example, if 4 lines of text need to be
kept together, the sequence
<blockquote>
<pre>
\par
\needspace{4\baselineskip}
% the stuff that must stay together
&lt;text generating lines 1-4&gt;
% now stuff we don't mind about
</pre>
</blockquote><p>
Yet another trick by Knuth is useful if you have a sequence of small
blocks of text that need, individually, to be kept on their own page.
Insert the command <code>\</code><code>filbreak</code> before each small block, and the
effect is achieved.  The technique can be used in the case of
sequences of LaTeX-style sections, by incorporating <code>\</code><code>filbreak</code>
into the definition of a command (as in 
<a href="FAQ-patch.html">patching commands</a>).  A simple and effective
patch would be:
<blockquote>
<pre>
\let\oldsubsubsection=\subsubsection
\renewcommand{\subsubsection}{%
  \filbreak
  \oldsubsubsection
}
</pre>
</blockquote><p>
While the trick works for consecutive sequences of blocks, it&rsquo;s
slightly tricky to get out of such sequences unless the sequence is
interrupted by a forced page break (such as <code>\</code><code>clearpage</code>, which may
be introduced by a <code>\</code><code>chapter</code> command, or the end of the document).
If the sequence is not interrupted, the last block is likely to be
forced onto a new page, regardless of whether it actually needs it.
<p/>If one is willing to accept that not everything can be accomplished
totally automatically, the way to go is to typeset the document and to
check for things that have the potential to give trouble.  In such a
scenario (which has Knuth&rsquo;s authority behind it, if one is to believe
the rather few words he says on the subject in the TeXbook) one can
decide, case by case, how to deal with problems at the last
proof-reading stage.  The alternatives are to insert <code>\</code><code>clearpage</code>
commands as necessary, or to use <code>\</code><code>enlargethispage</code>.  Supposing you
have a line or two that stray: issue the command
<code>\</code><code>enlargethispage{2<code>\</code><code>baselineskip</code>}</code> and two lines are
added to the page you&rsquo;re typesetting.  It depends on the document
whether this looks impossibly awful or entirely acceptable, but the
command remains a useful item in the armoury.
<dl>
<dt><tt><i>memoir.cls</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/memoir.zip">macros/latex/contrib/memoir</a> (or <a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archive/macros/latex/contrib/memoir/">browse the directory</a>)
<dt><tt><i>needspace.sty</i></tt><dd><a href="http://www.tex.ac.uk/www.tex.ac.uk  tex-archivemacros/latex/contrib/misc/needspace.sty">macros/latex/contrib/misc/needspace.sty</a>
</dl>
<p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=nopagebrk">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=nopagebrk</a>
</body>