仅当段落紧接在前/不在证明开头时才添加 vspace

仅当段落紧接在前/不在证明开头时才添加 vspace

我的问题非常类似这个但是我的情况更具体,我想知道是否有比执行代码更简单(且更高效)的解决方案每一个段落:

我编写了一个小宏来构造冗长的证明并突出显示关键步骤,称为\proofpart。为了将有问题的步骤与它使用的证明的前一步区分开来\noindent,并在之前添加了一些\vspace。现在,当我在证明的最开始使用它时,问题就出现了:在这种情况下,它\vspace会影响我整个证明的定位并增加其顶部边距。我该如何解决这个问题?

梅威瑟:

\documentclass[12pt,a4paper]{memoir}

\usepackage{fontspec}
\usepackage{xunicode}

\usepackage{mathtools}
\usepackage{unicode-math}

\usepackage{blindtext}

\usepackage{amsthm}
\usepackage{thmtools}

\newcommand{\proofpart}[1]{%
  \vspace{0.7em}%
  \noindent%
  \textsc{#1}%
}

\declaretheoremstyle[
  spaceabove=2em,
  spacebelow=2em,
  postheadspace=1em,
  headfont=\upshape\bfseries,
  notefont=\mdseries,
  notebraces={(}{)},
  bodyfont=\normalfont,
]{mytheoremstyle}

\declaretheoremstyle[
  spaceabove=-1em, % <-- reduce margin, so that it is clear that the
  % proof belongs to the previous theorem/lemma/…
  %
  spacebelow=2em,
  postheadspace=1em,
  % headfont=\itshape,
  headfont=\upshape\bfseries,
  bodyfont = \normalfont,
  qed=$\qedsymbol$,
  headpunct={:},
]{myproofstyle}

\newcounter{mathcounter}
\numberwithin{mathcounter}{chapter}

\declaretheorem[
  name={Theorem},
  style=mytheoremstyle,
  numberlike=mathcounter
]{theorem}

% Remove amsthm's proof environment, so that we can override it below.
\let\proof\undefined
\let\endproof\undefined

\declaretheorem[
  name={Proof},
  style=myproofstyle,
  numbered=no
]{proof}


\begin{document}


\begin{theorem}
  \blindtext
\end{theorem}
\begin{proof}
  Some preliminary remarks.

  \proofpart{Step 1:} % <-- No problem here
    Foo

  \proofpart{Step 2:} % <-- No problem here
    Bar
\end{proof}


\begin{theorem}
  \blindtext
\end{theorem}
\begin{proof}
  \proofpart{Step 1:} % <-- This will increase spacing between theorem and proof
    Foo

  \proofpart{Step 2:} % <-- No problem here
    Bar
\end{proof}


\end{document}

答案1

这里我利用了\everypar在环境开始时,trivlist当第一段尚未陈述时,以及在第一段开始之后,标记列表不为空的事实。由于所有定理环境内部都使用trivlist,我用它来测试我们是否处于环境的开始处proof

\newcommand{\proofpart}[1]{%
  % store contents of \everypar in \@tempa
  \expandafter\def\expandafter\@tempa\expandafter{\the\everypar}%
  \ifx\@tempa\@empty
    % if \@tempa is empty, insert vspace
    \vspace{.7em}%
  \else
    % otherwise, reconstruct \everypar
    \everypar\expandafter{\@tempa}%
  \fi
  \noindent
  \textsc{#1}%
}

完整示例

\documentclass[12pt,a4paper]{memoir}

\usepackage{fontspec}
\usepackage{xunicode}

\usepackage{mathtools}
\usepackage{unicode-math}

\usepackage{blindtext}

\usepackage{amsthm}
\usepackage{thmtools}

\makeatletter
\newcommand{\proofpart}[1]{%
  % token list \everypar is non-empty at the beginning of proof env 
  % (inherited from trivlist env)
  \expandafter\def\expandafter\@tempa\expandafter{\the\everypar}%
  \ifx\@tempa\@empty
    \vspace{.7em}%
  \else
    \everypar\expandafter{\@tempa}%
  \fi
  \noindent
  \textsc{#1}%
}
\makeatother

\declaretheoremstyle[
  spaceabove=2em,
  spacebelow=2em,
  postheadspace=1em,
  headfont=\upshape\bfseries,
  notefont=\mdseries,
  notebraces={(}{)},
  bodyfont=\normalfont,
]{mytheoremstyle}

\declaretheoremstyle[
  spaceabove=-1em, % <-- reduce margin, so that it is clear that the
  % proof belongs to the previous theorem/lemma/…
  %
  spacebelow=2em,
  postheadspace=1em,
  % headfont=\itshape,
  headfont=\upshape\bfseries,
  bodyfont = \normalfont,
  qed=$\qedsymbol$,
  headpunct={:},
]{myproofstyle}

\newcounter{mathcounter}
\numberwithin{mathcounter}{chapter}

\declaretheorem[
  name={Theorem},
  style=mytheoremstyle,
  numberlike=mathcounter
]{theorem}

% Remove amsthm's proof environment, so that we can override it below.
\let\proof\undefined
\let\endproof\undefined

\declaretheorem[
  name={Proof},
  style=myproofstyle,
  numbered=no
]{proof}


\begin{document}


\begin{theorem}
  \blindtext
\end{theorem}
\begin{proof}
  Some preliminary remarks.

  \proofpart{Step 1:} % <-- No problem here
    Foo

  \proofpart{Step 2:} % <-- No problem here
    Bar
\end{proof}


\begin{theorem}
  \blindtext
\end{theorem}
\begin{proof}\showthe\everypar
  \proofpart{Step 1:} % <-- This will increase spacing between theorem and proof
    Foo

  \proofpart{Step 2:} % <-- No problem here
    Bar
\end{proof}


\end{document}

在此处输入图片描述

相关内容