在我的 LaTeX 文档中,我希望将可以完全放在一页上的部分自动强制从下一页开始,这样该部分就不会变得孤零零的。文档中有图像。如果该部分可以放在一页上,那么我希望在\clearpage
该部分之前添加“ ”的等效效果。我需要这个过程自动化,因为在“真实”文档中,数据是自动生成的。这是一个工作示例:
% test.tex
% For mypic.jpg I used this example image: http://media.istockphoto.com/photos/yellow-flowers-picture-id506321010
\documentclass[letterpaper,12pt,twoside,onecolumn,notitlepage]{report}
\usepackage{float}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage[margin=0.7in]{geometry}
\raggedbottom
\begin{document}
\chapter{First chapter}
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\section{First section}
The image in this section is going to be widowed onto the next page. If I hypothetically put ``{\textbackslash}clearpage" before this section, then the entire section including the image would fit on one page. I'd like to make LaTeX detect that and automatically force this section to start on the next page so that the image doesn't get widowed.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic.jpg}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\section{Second section}
This section has two images and cannot fit entirely on one page (putting ``{\textbackslash}clearpage" before the section would still result in the section spanning two pages anyway), so I'm fine having this section start here and having the images flow onto the next page -- i.e., I don't want to force this section to start on the next page.
\lipsum[1]
\includegraphics[height=3.4in]{mypic.jpg}
\vspace{\baselineskip}
\includegraphics[height=3.4in]{mypic.jpg}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\section{Third section}
This section has no images and fits on the same page after the second section, so I'm fine having it stay here and don't want to force it to start on the next page.
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\section{Fourth section}
This is also a section that could fit entirely on one page and gets orphaned or widowed, so I'd like it to be automatically forced to start on the next page.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic.jpg}
\end{document}
这可以在 LaTeX 中完成吗?
答案1
以下是一个建议。
更改代码的自动生成,以便
\section{<title>} ...
被替换为
\begin{chunk}{<title>} ... \end{chunk}
用于
environ
定义环境chunk
,以便您可以捕获其内容并测试其总高度。- 使用
calc
这样您就可以轻松计算总高度(即高度+深度)并更容易管理尺寸。 needspace
当且仅当该部分的总高度不大于文本块的高度时,才使用它来为该部分的总高度保留足够的垂直空间。
这样做的结果是强制在该部分之前分页,除非该部分适合当前页面或该部分需要多页。
代码:
\documentclass[letterpaper,12pt,twoside,onecolumn,notitlepage]{report}
\usepackage{float}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage[margin=0.7in]{geometry}
\usepackage{environ,calc,needspace}
\raggedbottom
\newlength\chunktotheight
\NewEnviron{chunk}[1]{%
\settototalheight{\chunktotheight}{%
\begin{minipage}{\textwidth}
\section{#1}
\BODY
\end{minipage}%
}%
\ifdim\chunktotheight>\textheight
\else
\needspace{\chunktotheight}%
\fi
\addtocounter{section}{-1}%
\section{#1}
\BODY
}{}
\begin{document}
\chapter{First chapter}
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{First section}
The image in this section is going to be widowed onto the next page. If I hypothetically put ``{\textbackslash}clearpage'' before this section, then the entire section including the image would fit on one page. I'd like to make \LaTeX{} detect that and automatically force this section to start on the next page so that the image doesn't get widowed.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic}% better not to specify the extension ; may not look quite the same as the original
\end{chunk}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{Second section}
This section has two images and cannot fit entirely on one page (putting ``{\textbackslash}clearpage'' before the section would still result in the section spanning two pages anyway), so I'm fine having this section start here and having the images flow onto the next page -- i.e., I don't want to force this section to start on the next page.
\lipsum[1]
\includegraphics[height=3.4in]{mypic}
\vspace{\baselineskip}
\includegraphics[height=3.4in]{mypic}
\end{chunk}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{Third section}
This section has no images and fits on the same page after the second section, so I'm fine having it stay here and don't want to force it to start on the next page.
\end{chunk}
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{Fourth section}
This is also a section that could fit entirely on one page and gets orphaned or widowed, so I'd like it to be automatically forced to start on the next page.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic}
\end{chunk}
\end{document}
编辑
这是对有关 PDF 等困难的评论的回应。没有示例,很难说我是否解决了正确的问题。不过,我做得很好A\includepdf[]{}
在内部使用存在问题chunk
,但在 外部却不存在问题。
以下代码应该可以解决这个特定问题。这个想法是,由于 PDF 包含总是从新页面开始,因此任何包含的部分\includepdf[]{}
必然需要多页排版。因此,我们检查\includepdf[]{}
并在这种情况下不采取任何措施。
为了进行检查,我只需使用一个切换开关并临时重新定义\includepdf[]{}
以将切换开关设置为 true。检查完成后,原始定义将被替换。
\documentclass[letterpaper,12pt,twoside,onecolumn,notitlepage]{report}
\usepackage{float}
\usepackage{lipsum}
\usepackage{graphicx,pdfpages}
\usepackage[margin=0.7in]{geometry}
\usepackage{environ,calc,needspace}
\raggedbottom
\newlength\chunktotheight
\NewEnviron{chunk}[1]{%
\let\savedincludepdf\includepdf
\let\includepdf\chunkincludepdf
\chunkincludepdffalse
\settototalheight{\chunktotheight}{%
\begin{minipage}{\textwidth}
\section{#1}
\BODY
\end{minipage}%
}%
\ifdim\chunktotheight>\textheight
\else
\ifchunkincludepdf
\else
\needspace{\chunktotheight}%
\fi
\fi
\let\includepdf\savedincludepdf
\addtocounter{section}{-1}%
\section{#1}
\BODY
}{}
\newcommand*\chunkincludepdf[1][]{\chunkincludepdftrue}
\newif\ifchunkincludepdf
\chunkincludepdffalse
\begin{document}
\chapter{First chapter}
\includepdf{example-image-letter}
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{First section}
The image in this section is going to be widowed onto the next page. If I hypothetically put ``{\textbackslash}clearpage'' before this section, then the entire section including the image would fit on one page. I'd like to make \LaTeX{} detect that and automatically force this section to start on the next page so that the image doesn't get widowed.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic}% better not to specify the extension ; may not look quite the same as the original
\end{chunk}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{Second section}
This section has two images and cannot fit entirely on one page (putting ``{\textbackslash}clearpage'' before the section would still result in the section spanning two pages anyway), so I'm fine having this section start here and having the images flow onto the next page -- i.e., I don't want to force this section to start on the next page.
\lipsum[1]
\includegraphics[height=3.4in]{mypic}
\vspace{\baselineskip}
\includegraphics[height=3.4in]{mypic}
\end{chunk}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{Third section}
This section has no images and fits on the same page after the second section, so I'm fine having it stay here and don't want to force it to start on the next page.
\end{chunk}
\includepdf{example-image-letter}
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{chunk}{Fourth section}
This is also a section that could fit entirely on one page and gets orphaned or widowed, so I'd like it to be automatically forced to start on the next page.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic}
\end{chunk}
\begin{chunk}{Fifth section}
This include a full-page PDF, so obviously won't fit on one page.
Hence, we don't expect a page break prior to this section unless there would be one anyhow.
\lipsum[1]
\includepdf{example-image-letter}
\lipsum[2]
\end{chunk}
\end{document}
如果不进行修复,最终的 PDF 包含将无法正常工作:PDF 被包含两次,第一次包含覆盖了前一节。发生这种情况是因为这次包含位于 内chunk
,而前两次包含则不在 内。
答案2
我理解您的目标是尽可能将各个部分放在同一页上,尽管“寡妇”和“孤儿”术语的使用非常规。
扩大我的以上评论答案:这个想法是把整个部分放进一个盒子里,然后测量盒子的高度。如果盒子的高度足够小,可以放在一页纸里,和如果它足够大,无法放在当前页面上,那么我们插入一个\clearpage
。我相信这正是你想要的。
为了实现这个想法,
- 这将有助于将您的自动生成的代码更改为使用类似
\begin{mysection}{Name of section}
和 之类的东西\end{mysection}
,而不仅仅是\section{Name of section}
。 您可以使用以下代码来
mysection
相应地定义环境:\newbox\mysectionbox \newenvironment{mysection}[1]{% \setbox\mysectionbox=\vbox\bgroup% \section{#1} }{\egroup % Calculate remaining space on the page \newdimen\pageremaining \pageremaining=\pagegoal \advance\pageremaining by -\pagetotal % If \mysectionbox fits on a page, but not on current page, then insert a clearpage. \ifdim\ht\mysectionbox>\pagegoal \else \ifdim\ht\mysectionbox>\pageremaining \clearpage \fi \fi % Unbox the section box and typeset it. \unvbox\mysectionbox }
(这不需要额外的包,并且只使用纯 TeX 中可用的东西,当然除了\clearpage
你想要的之外,还有\newenvironment
……更多内容见下文。)
这是您的示例产生的输出,并进行了相应的修改:
% test.tex
% For mypic.jpg I used this example image: http://media.istockphoto.com/photos/yellow-flowers-picture-id506321010
\documentclass[letterpaper,12pt,twoside,onecolumn,notitlepage]{report}
\usepackage{float}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage[margin=0.7in]{geometry}
\raggedbottom
\newbox\mysectionbox
\newenvironment{mysection}[1]{
\setbox\mysectionbox=\vbox\bgroup
\section{#1}
}{\egroup
% Calculate remaining space on the page
\newdimen\pageremaining
\pageremaining=\pagegoal
\advance\pageremaining by -\pagetotal
% If \mysectionbox fits on a page, but not on current page, then insert a clearpage.
\ifdim\ht\mysectionbox>\pagegoal
\else
\ifdim\ht\mysectionbox>\pageremaining
\clearpage
\fi
\fi
% Unbox the section box and typeset it.
\unvbox\mysectionbox
}
\begin{document}
\chapter{First chapter}
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{mysection}{First section}
The image in this section is going to be widowed onto the next page. If I hypothetically put ``{\textbackslash}clearpage" before this section, then the entire section including the image would fit on one page. I'd like to make LaTeX detect that and automatically force this section to start on the next page so that the image doesn't get widowed.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic.jpg}
\end{mysection}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{mysection}{Second section}
This section has two images and cannot fit entirely on one page (putting ``{\textbackslash}clearpage" before the section would still result in the section spanning two pages anyway), so I'm fine having this section start here and having the images flow onto the next page -- i.e., I don't want to force this section to start on the next page.
\lipsum[1]
\includegraphics[height=3.4in]{mypic.jpg}
\vspace{\baselineskip}
\includegraphics[height=3.4in]{mypic.jpg}
\end{mysection}
% Don't want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{mysection}{Third section}
This section has no images and fits on the same page after the second section, so I'm fine having it stay here and don't want to force it to start on the next page.
\end{mysection}
% Want LaTeX to automatically put the equivalent of a \clearpage here.
\begin{mysection}{Fourth section}
This is also a section that could fit entirely on one page and gets orphaned or widowed, so I'd like it to be automatically forced to start on the next page.
\lipsum[1-2]
\includegraphics[height=3.4in]{mypic.jpg}
\end{mysection}
\end{document}
\dp
(注意:我还没有仔细考虑过除了高度之外,是否还需要考虑 vbox 的深度( )。可能吧。)
进一步提出一个可能在本站引起争议的建议:在我看来,对于自动生成的文档,没有充分的理由使用 LaTeX 而不是普通的 TeX;无论如何,如果你花几个小时学习 TeX 的盒子和胶水排版模型(例如来自TeX 初学者指南或者当然TeXbook),并将 (La)TeX 仅用作排版工具,并尽可能在外部进行编程。LaTeX 是一种使用“逻辑结构”的“文档准备系统”的深刻理念(正如 Leslie Lamport 所倡导的(转载自 TUGboat)),让作者更加关注内容而不是外观(将他们的“文档”视为“章节”、“节”、“小节”等的集合),但如果您已经在其他地方管理您的内容并且只使用 (La)TeX 来制作页面,并且非常关心外观,那么准确了解幕后发生的事情是有意义的,这样您就可以对其进行适当的影响。
编辑:为了遵循我的建议,尽可能不要使用 (La)TeX 进行编程以保持您的理智,这里有一些用 Lua 完成的“编程”部分(获取高度并进行比较)的等效代码:我的第一行 LuaTeX 代码 :-)(您需要使用lualatex
而不是 来编译文件pdflatex
。)
\newbox\mysectionbox
\newenvironment{mysection}[1]{
\setbox\mysectionbox=\vbox\bgroup
\section{#1}
}{\egroup
\directlua{
local remaining = tex.get('pagegoal') - tex.get('pagetotal')
local myboxheight = tex.getbox('mysectionbox').height
-- If the box can fit on a page *and* cannot fit on current page, then clear page.
if myboxheight <= tex.get('pagegoal') and myboxheight > remaining then
tex.print([[\noexpand\clearpage]])
end
}
% Unbox the section box and typeset it.
\unvbox\mysectionbox
}
答案3
我对已经发布的两个答案并不完全满意,因此我将尝试对同一基本想法提出改进。请允许我解释一下我认为其他答案的相对弱点。
@cfr 的答案将整个部分的内容作为宏参数吞噬并处理两次作为标记列表,第一次是为了决定做什么,第二次是为了进行实际排版。我认为这是唯一能够解决 问题的方法\includepdf
,因为获取所包含页面的正确位置需要\shipout
在适当的时间执行一个或多个,并且\shipout
是 TeX 原语命令,因此它只能在 TeX 的胃中执行,同时消化 token(这与例如,即执行 a 的方式\write
)。因此,在我看来,@cfr 的答案应该被视为该问题最通用、最有力的解决方案,我建议您接受它并将赏金授予她。尽管如此,将整个节的标记存储为宏参数并对其进行两次处理 (1) 效率低下,并且 (2) 容易引入不良的副作用,就像节计数器增加两次的情况一样;因此,我们看到寻找基于不同策略的解决方案是值得的。
事实上,环境的内容也可以在 TeX 处理线的下游某个点捕获,即作为框列表;更准确地说,(可能)跨越多个段落的内容实际上只能作为垂直列表收集。这正是 @ShreevatsaR 的解决方案所做的:它测量收集到的列表的高度,并根据测量结果,将该列表的适当操作版本传递给页面构建器。请注意,这种操作发生在 TeX 处理线中 TeX 胃的下游,此时不再可能执行原始命令,这就是该解决方案无法解决问题的原因\includepdf
:简单来说,没有什么您可以放入一个框列表,这些框可以使 TeX\shipout
在该列表的某个点执行操作(再次将其与 的行为进行对比\write
)。
然而,@ShreevatsaR 的解决方案实现这一总体思路的方式,在我看来,并不完全正确:基本上,我发现它的主要缺点是它只考虑了自然身高捕获内容的压缩能力,而不是其收缩能力;但还有一些其他问题,下面给出的代码中包含的注释将对此进行讨论。出于这个原因,我敢于基于相同的一般原则提交另一个答案,但通过 实现\vsplit
;它的工作原理如下:
它将环境的内容捕获为垂直列表,就像@ShreevatsaR 所做的那样;
it tries to
\vsplit
this list either to the available height remaining on the current page, or to the full\textheight
(note that\vsplit
will consider the available shrinkability);it eventually delivers to the page builder a suitably manipulated version of the captured list, again exactly as in @ShreevatsaR’s solution.
Since it is based on the same principle, my solution doesn’t permit to use \includepdf
inside the environment either. I consider it a significant enhancement, though, to have introduced proper management of the interline glue at the environment’s boundaries.
I’ve split the code into two files. The first is a package, named tryfittinginpage
, that defines an environment by the same name, inside which you should put the material that you want to try to make fit in a single page:
\NeedsTeXFormat{LaTeX2e}[2004/06/01] % LaTeX2e is required!
\ProvidesPackage{tryfittinginpage}
[2017/01/17 version information here]
% Diagnostic commands.
\newcommand*\@TFIP@Show[1]{%
\begingroup
\showboxbreadth \@m
\showboxdepth \sixt@@n
#1% what should be shown
\endgroup
}
\newcommand*\ShowList{\@TFIP@Show\showlists}
% In the following command, "\relax" is not needed after "#1" since
% "\endgroup" will follow anyway:
\newcommand*\ShowBox[1]{\@TFIP@Show{\showbox #1}}
\newcommand*\@TFIP@debug@Show[2]{%
\typeout{}%
\typeout{================================================%
================}%
\typeout{#2}%
\typeout{}%
\ShowBox{#1}%
}
\newcommand*\@TFIP@maybe@debug@Show[2]{}
\newcommand*\TFIPDebugInfoOn{%
\let \@TFIP@maybe@debug@Show \@TFIP@debug@Show
}
\newcommand*\TFIPDebugInfoOff{%
\let \@TFIP@maybe@debug@Show \@gobbletwo
}
\DeclareOption{debug}{\TFIPDebugInfoOn}
\DeclareOption{quiet}{\TFIPDebugInfoOff}
\ExecuteOptions{quiet}
\ProcessOptions\relax
\newenvironment*{tryfittinginpage}{%
\par
% The following assignment need not be "\global", bur we prefer to do
% so in order to conserve save stack positions, given the fact that,
% in any case, "\dimen@i" is going to be altered below.
\global \dimen@i \prevdepth
\setbox\z@ \vbox\bgroup % collect environment contents in "\box0"
\prevdepth \dimen@i
}{%
\par
\global \dimen@i \prevdepth
\egroup % end of "\vbox"
\@TFIP@maybe@debug@Show \z@
{Original contents of the environment:}%
\dimen@ \pagegoal
\advance \dimen@ -\pagetotal
% Do not forget that the material accumulated in the page so far can
% cooperate in making our content fit in the current page.
\advance \dimen@ \pagestretch % now "\dimen@" = available space
% Note that we cannot prevent the "\splittopskip" glue from being
% added during a "\vsplit", so we make sure that at least it is zero.
\splittopskip \z@skip
% Use in "\vsplit" the same setting used by the page builder:
\splitmaxdepth \maxdepth
% We'll need to reinsert the discared items if we eventually find that
% the material does not fit:
\savingvdiscards \@ne
% Finally, avoid warnings:
\vbadness \@M
\setbox\tw@ \vsplit\z@ to \dimen@
% Convention: we'll leave in "\box0" the material that should be
% returned to the main vertical list.
\ifvoid\z@ % if everything fits in the current page...
\setbox\z@ \vbox{% ...put it back in "\box0"
\@TFIP@clever@unbox@two \dimen@
}%
\@TFIP@maybe@debug@Show \z@
{It fits in the current page; will append:}%
\else
\@TFIP@reassemble@contents
\@TFIP@maybe@debug@Show \z@
{It does NOT fit in the current page; reassembled contents:}%
% Note that it is wrong, in general, to use the current value of
% "\pagegoal" as the measure of the height of the text.
\setbox\tw@ \vsplit\z@ to \textheight
\ifvoid\z@ % if the contents fit in a single page...
\setbox\z@ \vbox{% ...package them in "\box0" with a page break
% We insert here the equivalent of "\clearpage": the page
% builder will take care of forwarding the "\penalty -\@Mi"
% message to the output routine.
\vfil\break
\write\m@ne{}%
\vbox{}%
\penalty -\@Mi
\@TFIP@clever@unbox@two \textheight
}%
\@TFIP@maybe@debug@Show \z@
{It fits in the next page; will append:}%
\else % if the oontents do not fit in a single page...
\@TFIP@reassemble@contents % ...put them back in "\box0"
\@TFIP@maybe@debug@Show \z@
{It does NOT fit in a page by itself; will append:}%
\fi
\fi
\unvbox\z@
\prevdepth \dimen@i
}
\newcommand*\@TFIP@reassemble@contents{%
% This macro reassembles in "\box0" the original contents of the
% environment after they have been split.
\setbox\z@ \vbox{% now be careful:
\unvbox\tw@ % split-off material
\splitdiscards % includes chosen breakpoint
\nobreak % because the "\splittopskip" glue follows, and of course
% it hadn't been considered as a possible breakpoint in
% the previous steps...
\unvbox\z@ % rest of the contents
}%
}
\newcommand*\@TFIP@clever@unbox@two[1]{%
% This macro unboxes "\box2" while taking care of the following
% problem: if "\vsplit" shrank the contents in order to make them fit
% in the given height (and note that "\vsplit" assumes an implicit
% "\penalty -10000" at the end of the vertical list being split, see
% "The TeXbook", p. 124, lines 6-7), we need to be sure that the page
% builder will choose the same breakpoint; for this, we insert
% "\break" at the end of the current list, but only if the natural
% height of "\box2" exceeds the dimension passed as argument #1.
\ifdim\ht\tw@>#1% assume #1 is a <dimen> token
\unvbox\tw@
\break
\else
\unvbox\tw@
\fi
}
\endinput
Save this code as tryfittinginpage.sty
and put it in a place where TeX looks for input files (e.g., in the same directory as the file you want to compile). Note that the code includes some diagnostic commands you could very well get rid of.
The second file is an ordinary LaTeX document that invokes the tryfittinginpage
package and tests it:
% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly
% declare the paper format.
\usepackage[T1]{fontenc} % Not always necessary, but recommended.
% End of standard header. What follows pertains to the problem at hand.
\usepackage{tryfittinginpage}
\usepackage{caption}
\usepackage{pdfpages}
\usepackage{mwe}
\begin{document}
\tableofcontents
\listoffigures
\section{Some recipes}
\lipsum[1-2]
\begin{figure}[tbp]
\centering
\includegraphics{image}
\caption{An insertion that makes things a little bit more difficult}
\label{fig:image}
\end{figure}
The presence of figure~\ref{fig:image} explains why it is necessary to ensure
that dangling insertions are processed before the contents of our environment
get re-inserted.
% \showthe\prevdepth
%
% \TFIPDebugInfoOn
\begin{tryfittinginpage}
\subsection{Sachertorte} % :-)
\lipsum[3]
\begin{center}
\includegraphics{image-a}
\captionof{figure}{Is this a Sachertorte?}
\end{center}
\lipsum[4]
\end{tryfittinginpage}
Note that some ordinary text may follow on the same page.
\par\lipsum[5-7]
\begin{tryfittinginpage}
\subsection{Gnocchi alla bolognese}
This is expected to fit in the same page.
\par\lipsum[8]
\end{tryfittinginpage}
% \TFIPDebugInfoOff
Again, ordinary text may follow (of course, it's always possible to prevent this
from happening by inserting an explicit \verb|\pagebreak| immediately after the
end of the environment.
\begin{tryfittinginpage}
Now, a section that will not fit in a single page.
\subsection{Canard \`{a} l'orange}
\lipsum[9-16]
\begin{center}
\includegraphics{image-b}
\captionof{figure}{And this? Could it ever be a canard?}
\end{center}
\end{tryfittinginpage}
\section{Technical trials}
Here we check some technical issues.
\begin{tryfittinginpage}
\subsection{Page breaks are honored}
Without the page breaks\ldots
\newpage
\ldots this section would almost certainly\ldots
\newpage
\ldots fit in a single page (but not at the end of the page on which the
environments starts, because of the ``lipsum'' text).
\par\lipsum[17-18]
\end{tryfittinginpage}
\newpage
Text between two sections.
\lipsum*[19]
This text starts on a new page and continues until it gets to the eighth line.
\begin{tryfittinginpage}
\subsection{Without shrinkability}
This section contains an unshrinkable vertical space that prevents it from
fitting into a single page. Lorem ipsum adipisci elit num. This text takes
up three lines, no more and no less.
\par\vspace{.9\textheight}
This text comes at the end of a section, after a big, unshrinkable vertical
space. Lorem ipsum dolor sit amet.
\end{tryfittinginpage}
\newpage
Now we repeat the test under the same conditions, but with shrinkability.
\lipsum*[19]
This text gets to the eighth line too.
\begin{tryfittinginpage}
\subsection{With shrinkability}
This section contains a shrinkable vertical space with the same natural
length as the space contained in the previous section. This text takes up
three lines, no more and no less.
\par\vspace{.9\textheight minus .1\textheight}
This text comes at the end of a section, after a big, but shrinkable
vertical space. Lorem ipsum dolor sit amet.
\end{tryfittinginpage}
\subsection{Including pages}
Unfortunately, it is possible to include a whole page with \verb|\includepdf|
only when you are outside the \texttt{tryfittinginpage} environment.
\includepdf{image-a4}
\lipsum[20]
\end{document}