如果内容也从新页面开始,则强制句子从下一页开始

如果内容也从新页面开始,则强制句子从下一页开始

对于我的家庭作业,我使用这个环境:

\newcounter{exercise}
\renewcommand{\theexercise}{\arabic{exercise}}

\newenvironment{exercise}[1]
{\smallskip\refstepcounter{exercise}\textbf{Solution to exercise~\theexercise:}\par\vspace{#1}}
{\par \hspace*{\fill}$\square$\medskip}

有时,我会发现“练习 x 的解决方案”位于页面末尾,而解决方案从下一页开始,或者末尾的“$square$”从下一页开始。我想知道是否有可能让 tex 检查下一行是否从下一页开始,然后强制它将“练习 x 的解决方案”也放到下一页,或者在另一种情况下,它会检查“$square$”是否从下一页开始,并将之前的一行也放到下一页。

仍然会导致问题的代码:

\begin{center}
        \begin{tikzpicture}[point/.style={fill, circle, inner sep = 1.3pt}]
            %x axis
            \draw[->] (-2,0) -- (6,0) node[below] {$x$};
            \foreach \x in {-1,1,2,...,5}
            \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
            %y axis
            \draw[->] (0,-2) -- (0,3) node[left] {$y$};
            \foreach \y in {-1,1,2}
            \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
            \node[below left] at (0,0) {\footnotesize $0$};
            % points
            \node (H) [point,label=above:$H$] at (1,1) {};
            \node (F) [point,label=above:$F$] at (4,2) {};
            \node (X) [point,label={[label distance=0.15cm]350:$x^*$}] at (2,0) {};
            % river
            \draw[blue, line width = 0.4mm] (-1.5,0) -- (5.5,0);
            % path
            \draw[dashed] (H) -- (X);
            \draw[dashed] (X) -- (F);
        \end{tikzpicture}
    \end{center}

答案1

正如@user202729在评论中指出的那样,您可以在代码中\nopagebreak直接在宏之后使用。如果您将参数添加到此宏中,则 (La)TeX 此时破坏页面的可能性很小。\par[4]

如果您还想防止出现寡妇(即下一页上的一行),您可以\widowpenalty10000在代码开头进行额外设置。

下面的代码包含两个极端的例子,表明在页眉和正文之间以及正文和方块之间将防止分页:

\documentclass{article}
\usepackage{amssymb}

\usepackage{lipsum, xcolor} % just for illustrative purposes

\newcounter{exercise}
\renewcommand{\theexercise}{\arabic{exercise}}

\newenvironment{exercise}[1]
{\widowpenalty10000\smallskip\refstepcounter{exercise}\textbf{Solution to exercise~\theexercise:}\par\nopagebreak[4]\vspace{#1}}
{\par\nopagebreak[4]\hspace*{\fill}$\square$\medskip}

\begin{document}

{\color{lightgray}\lipsum[1-3]}

\begin{exercise}{1.6cm}
\lipsum[1]
\end{exercise}

{\color{lightgray}\lipsum[1-3]}

\begin{exercise}{5cm}
\lipsum[1]
\end{exercise}

\end{document}

在此处输入图片描述

在此处输入图片描述

在此处输入图片描述

答案2

我建议amsthm在校样结束时使用其内置的方块机制。

\documentclass{article}
\usepackage{amsthm}
\usepackage{tikz}

\usepackage{showframe,lipsum} % just for this example

\theoremstyle{definition}
\newtheorem{innerexercise}{Solution to exercise}
\newenvironment{exercise}
 {\pushQED{\qed}\innerexercise}
 {\popQED\endinnerexercise}

\begin{document}

\begin{exercise}
\lipsum[1][1-4]
\[
  \begin{tikzpicture}[point/.style={fill, circle, inner sep = 1.3pt}]
    %x axis
    \draw[->] (-2,0) -- (6,0) node[below] {$x$};
    \foreach \x in {-1,1,2,...,5}{
      \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
    }
    %y axis
    \draw[->] (0,-2) -- (0,3) node[left] {$y$};
    \foreach \y in {-1,1,2}{
      \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
    }
    \node[below left] at (0,0) {\footnotesize $0$};
    % points
    \node (H) [point,label=above:$H$] at (1,1) {};
    \node (F) [point,label=above:$F$] at (4,2) {};
    \node (X) [point,label={[label distance=0.15cm]350:$x^*$}] at (2,0) {};
    % river
    \draw[blue, line width = 0.4mm] (-1.5,0) -- (5.5,0);
    % path
    \draw[dashed] (H) -- (X);
    \draw[dashed] (X) -- (F);
  \end{tikzpicture}\qedhere
\]
\end{exercise}

\begin{exercise}
\lipsum[2][1-9]
\end{exercise}

\end{document}

通过数学显示,您可以确保图片之前不会出现分页符。

在此处输入图片描述

如果您希望文本从新行开始:

\documentclass{article}
\usepackage{amsthm}
\usepackage{tikz}

\usepackage{showframe,lipsum} % just for this example

\newtheoremstyle{exercise}
  {\topsep}     % ABOVESPACE
  {\topsep}     % BELOWSPACE
  {\normalfont} % BODYFONT
  {0pt}         % INDENT (empty value is the same as 0pt)
  {\bfseries}   % HEADFONT
  {:}           % HEADPUNCT
  {\newline}    % HEADSPACE
  {}            % CUSTOM-HEAD-SPEC

\theoremstyle{exercise}
\newtheorem{innerexercise}{Solution to exercise}
\newenvironment{exercise}
 {\pushQED{\qed}\innerexercise}
 {\popQED\endinnerexercise}

\begin{document}

\begin{exercise}
\lipsum[1][1-4]
\[
  \begin{tikzpicture}[point/.style={fill, circle, inner sep = 1.3pt}]
    %x axis
    \draw[->] (-2,0) -- (6,0) node[below] {$x$};
    \foreach \x in {-1,1,2,...,5}{
      \draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {\footnotesize $\x$};
    }
    %y axis
    \draw[->] (0,-2) -- (0,3) node[left] {$y$};
    \foreach \y in {-1,1,2}{
      \draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {\footnotesize $\y$};
    }
    \node[below left] at (0,0) {\footnotesize $0$};
    % points
    \node (H) [point,label=above:$H$] at (1,1) {};
    \node (F) [point,label=above:$F$] at (4,2) {};
    \node (X) [point,label={[label distance=0.15cm]350:$x^*$}] at (2,0) {};
    % river
    \draw[blue, line width = 0.4mm] (-1.5,0) -- (5.5,0);
    % path
    \draw[dashed] (H) -- (X);
    \draw[dashed] (X) -- (F);
  \end{tikzpicture}\qedhere
\]
\end{exercise}

\begin{exercise}
\lipsum[2][1-9]
\end{exercise}

\end{document}

在此处输入图片描述

相关内容