我正在尝试编写一些代码来清理一些未来的笔记。本质上,我希望能够通过一个简单的命令“打开”或“关闭”环境(结果证明)。我使用 environ 包很容易做到这一点,但如果没有证明环境,定理就会太接近,变得混乱。因此,我想注入一些空间和一个 hrule 来将它们分开,只是为了获得良好的视觉效果。同样,完成这一点很简单,只需在我的新环境代码末尾添加相关代码,但这会在每个定理后面放置空间和 hrule……包括最后一个。有没有办法知道给定的环境是否是要排版的“最后”环境,然后不把行放在那里?我在考虑尝试做某种步进计数器系统,它会在最后展开,从而知道具有“计数器 17”的那个是最后一个,并只在那里(或除了那里以外的任何地方)展开一个宏。
这是我的 MWE,其末尾有不受欢迎的 hrule:
\documentclass{article}
% Packages
\usepackage{amsmath}
\usepackage{environ}% Used to hide proofs for condensed version of what we want to see.
\newcommand{\hideproofs}{%
\RenewEnviron{proof}{%
\vspace{1cm}
\hrule%
\vspace{1cm}
}%
}%
\begin{document}
\hideproofs% Comment this out to show proofs.
\begin{theorem}
This is theorem 1!
\end{theorem}
\begin{proof}
This is the first proof! It should be hidden when the hideproof command is on
and instead have the spacing and the hrule,
but it should show when hideproof is commented out.
\end{proof}
\begin{theorem}
This is theorem 2!
\end{theorem}
\begin{proof}
This is the second proof! It should also be hidden when the hideproof command is on
and instead have the spacing and the hrule,
but it should show when hideproof is commented out.
\end{proof}
\begin{theorem}
This is theorem 3! The last thoerem!
\end{theorem}
\begin{proof}
This is the third proof! It should be hidden when the hideproof command is on
and instead NOT have the spacing OR the hrule,
but it should still show when hideproof is commented out.
\end{proof}
\end{document}
答案1
建议:使用 zref 的 abspage-modul 并让 LaTeX 在定理的开头和结尾放置标签。
仅当前一个定理的结尾和当前定理的开头位于同一页面上时,LaTeX 才会在定理上方画线。
如果同一页面上的定理之间存在一些文本,则可以使用\suppressline
。
\documentclass{article}
% Packages
\usepackage{amsmath}
\usepackage{environ}% Used to hide proofs for condensed version of what we want to see.
\usepackage[abspage]{zref}%
\usepackage[english]{fnumprint}%
\usepackage{lipsum}%
\newcounter{absthms}%
\newif\ifproofs\global\proofstrue
\newcommand{\hideproofs}{%
\RenewEnviron{proof}{}%
\global\proofsfalse
}%
\newif\ifsuppressline\global\suppresslinefalse
\newcommand\suppressline{\global\suppresslinetrue}%
\makeatletter
\newtheorem{theorem}{Theorem:\zref@labelbyprops{absthmsstrt\number\value{absthms}}{abspage}}%
\newtheorem{proof}{Proof:}
\newcommand\MySavedTheorem{}%
\newcommand\MySavedEndTheorem{}%
\let\MySavedTheorem=\theorem
\let\MySavedEndTheorem=\endtheorem
\def\theorem{%
\global\stepcounter{absthms}%
\ifproofs\else
\ifsuppressline\global\suppresslinefalse\else
\vspace{1cm}%
\hrule
\ifnum\number\numexpr\number
\zref@extractdefault{absthmsend\number\numexpr\number\value{absthms}-1\relax}{abspage}{0}-%
\number\zref@extractdefault{absthmsstrt\number\value{absthms}}{abspage}{0}\relax=0 %
\else
width 0pt
\fi
\vspace{1cm}%
\fi\fi
\MySavedTheorem
}%
\def\endtheorem{\zref@labelbyprops{absthmsend\number\value{absthms}}{abspage}\MySavedEndTheorem}%
\makeatother
\begin{document}
\hideproofs% Comment this out to show proofs.
\def\tmpcnt{1}
\loop
\begin{theorem}
This is theorem \tmpcnt!
\end{theorem}
\begin{proof}
This is proof number \fnumprint{\tmpcnt}! It should be hidden when the hideproof command is on
and instead NOT have the spacing OR the hrule,
but it should still show when hideproof is commented out.
\end{proof}
\ifnum\tmpcnt<8 %
\edef\tmpcnt{\number\numexpr\tmpcnt+1\relax}
\repeat
\lipsum[1]
\suppressline
\def\tmpcnt{9}
\loop
\begin{theorem}
This is theorem \tmpcnt!
\end{theorem}
\begin{proof}
This is proof number \fnumprint{\tmpcnt}! It should be hidden when the hideproof command is on
and instead NOT have the spacing OR the hrule,
but it should still show when hideproof is commented out.
\end{proof}
\ifnum\tmpcnt<20 %
\edef\tmpcnt{\number\numexpr\tmpcnt+1\relax}
\repeat
\end{document}
答案2
您可以使用totcount
:
\documentclass{article}
% Packages
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{environ}
\usepackage{totcount}
\newtheorem{theorem}{Theorem}
\newtotcounter{proof}
\newcommand{\hideproofs}{%
\RenewEnviron{proof}{%
\stepcounter{proof}
\ifnum\value{proof}=\totvalue{proof}%
\else
\vspace{1cm}
\hrule
\vspace{1cm}
\fi
}%
}
\begin{document}
\hideproofs% Comment this out to show proofs.
\begin{theorem}
This is theorem 1!
\end{theorem}
\begin{proof}
This is the first proof! It should be hidden when the hideproof command is on
and instead have the spacing and the hrule,
but it should show when hideproof is commented out.
\end{proof}
\begin{theorem}
This is theorem 2!
\end{theorem}
\begin{proof}
This is the second proof! It should also be hidden when the hideproof command is on
and instead have the spacing and the hrule,
but it should show when hideproof is commented out.
\end{proof}
\begin{theorem}
This is theorem 3! The last theorem!
\end{theorem}
\begin{proof}
This is the third proof! It should be hidden when the hideproof command is on
and instead NOT have the spacing OR the hrule,
but it should still show when hideproof is commented out.
\end{proof}
\end{document}
这需要两次运行来同步。
答案3
还有一个建议:
通过在每个定理的开头\vskip
插入。垂直粘连/a和因此将被删除,以防粘连出现在下一页的开头,因为它不再适合当前页面。\cleaders
\vskip
\cleaders
为了准确获取的一个实例\cleaders
,垂直跳过必须适合垂直引导框的高度和宽度。
有一种极端情况,即页面底部有空间用于垂直间隙和规则,但没有其他空间。也许可以通过减少垂直间隙之前的(页面)分页惩罚来解决此问题,从而鼓励 (La)TeX 在插入垂直跳过之前进行分页\cleaders
。
\documentclass{article}
% Packages
\usepackage{amsmath}
\usepackage{environ}% Used to hide proofs for condensed version of what we want to see.
\usepackage[english]{fnumprint}%
\usepackage{lipsum}%
\newlength\thmruledistance
% Above and below theorems \topsep-glue is added by the
% theorem-environment.
\setlength\thmruledistance{\dimexpr1cm-\topsep\relax}
\newif\ifproofs\global\proofstrue
\newcommand{\hideproofs}{%
\RenewEnviron{proof}{}%
\global\proofsfalse
}%
\newif\ifsuppressline\global\suppresslinefalse
\newcommand\suppressline{\global\suppresslinetrue}%
\newbox\MyTempBox
\newcommand\breakwithline{%
\ifproofs\else
\ifsuppressline
\par\noindent\vskip-\parskip\global\suppresslinefalse
\else
\par\noindent\vskip-\parskip\penalty-200\relax
% Without specification height of a \hrule is 0.4pt, depth is 0pt.
\setbox\MyTempBox\vtop to \dimexpr0.4pt+2\thmruledistance\relax{%
{\offinterlineskip\vfill\noindent\hrulefill\vfill}%
}%
\cleaders\box\MyTempBox\vskip\dimexpr0.4pt+2\thmruledistance\relax
\vskip-\topsep\vskip-\lineskip
\fi
\fi
}
\newtheorem{theorem}{Theorem:}%
\newtheorem{proof}{Proof:}
\newcommand\MySavedTheorem{}%
\let\MySavedTheorem=\theorem
\def\theorem{%
\breakwithline
\MySavedTheorem
}%
\begin{document}
\hideproofs% Comment this out to show proofs.
\lipsum[3]
\suppressline
\def\tmpcnt{1}
\loop
\begin{theorem}
This is theorem \tmpcnt!
\end{theorem}
\begin{proof}
This is proof number \fnumprint{\tmpcnt}! It should be hidden when the hideproof command is on
and instead NOT have the spacing OR the hrule,
but it should still show when hideproof is commented out.
\end{proof}
\ifnum\tmpcnt<9 %
\edef\tmpcnt{\number\numexpr\tmpcnt+1\relax}
\repeat
\lipsum[1]\lipsum[3]
\suppressline
\def\tmpcnt{10}
\loop
\begin{theorem}
This is theorem \tmpcnt!
\end{theorem}
\begin{proof}
This is proof number \fnumprint{\tmpcnt}! It should be hidden when the hideproof command is on
and instead NOT have the spacing OR the hrule,
but it should still show when hideproof is commented out.
\end{proof}
\ifnum\tmpcnt<30 %
\edef\tmpcnt{\number\numexpr\tmpcnt+1\relax}
\repeat
\lipsum[4]
\end{document}