我想设置一个新环境,将第一段排版为粗体,将后面的段落排版为普通文本,并且第一段和其余文本之间(可能)没有分页符。我该怎么做?
例如,类似下面的内容:
\begin{question_and_answer}
This is a question, which should be highlited in bold
(or by changing font, or by another method) and preceded by a progressive number.
This is the answer, in normal font.
The answer is composed by one or more paragraph. There shouldn't be a
pagebreak between the first paragraph and the second.
\end{question_and_answer}
或者可能是这样的:
\begin{question}
This is the question, as the previous example
\end{question}
% no pagebreak here
\begin{answer}
This is the answer, as the previous example
\end{answer}
答案1
如果你的段落只有普通文本,你可以定义
\newenvironment{highlightnobreak}
{\par\bfseries
\def\par{\widowpenalty=10000 \endgraf\penalty10000 }}
{\par}
像使用它一样
... some text before.
\begin{highlightnobreak}
The first paragraph here will be typeset in bold face.
\end{highlightnobreak}
And this text, which is in the paragraph following the
environment will be in normal type.
突出显示的段落永远不会随着页面结束,并且始终会伴随着页面上的下一个段落的至少一行。不过,分页符可以发生在突出显示的段落的倒数第二行之前。
可以进行更多自定义。例如,可以在突出显示的段落后插入垂直空格,将定义修改为
\newenvironment{highlightnobreak}
{\par\bfseries
\def\par{\widowpenalty=10000 \endgraf\penalty10000 \medskip}}
{\par}
我们可以用一个渐进的数字和它前面的垂直空间来改进它,以匹配后面的数字:
\newcounter{highlightpar}
\newenvironment{highlightnobreak}
{\medskip\par
\def\par{\widowpenalty=10000 \endgraf\penalty10000 \medskip}%
\stepcounter{highlightpar}\bfseries\thehighligthpar. \ignorespaces}
{\par}
技术说明
重要的是,\par
关闭环境的操作比平常更多:在这里我们命令它设置\widowpenalty
为 no-break-before-the-last-line 值,然后完成段落并告诉 TeX no-pagebreak-here。
TeX 将看到的相关项目如下
〈高亮段落的最后一行〉
〈罚款 10000〉
〈\medskipamount
胶水〉
〈\parskip
胶水〉
〈\baselineskip
胶水〉
〈下一段的第一行〉
因此,根据规则,它不会在任何粘合项处分页,因为它们前面没有不可丢弃的项。只有惩罚项才是可行的分页点,但它“无限大”,因此 TeX 不会考虑它。
答案2
您可以\par
本地重新定义以重置字体并使用\@par
重置\par
为正常定义。我还添加了 egreg 给出的惩罚,这应该可以避免在两个段落之间出现分页符。如果您希望所有包含的段落中都没有分页符,那么您可以将整个内容包装在minipage
.
\documentclass{article}
\makeatletter
\newenvironment{myblock}{%
\par\bfseries
\def\par{%
\widowpenalty=10000
\@par\normalfont
\penalty10000
}%
}{%
\par
}
\makeatother
\begin{document}
\begin{myblock}
text text text text
text text text text
text text text text
text text text text
further text further text
further text further text
further text further text
further text further text
further text further text
further text further text
further text further text
further text further text
\end{myblock}
\end{document}