我使用了这里用于检查环境是否放置在封闭环境以外的其他页面上。我想在进入新页面时立即执行其他命令,但在该页面上只执行一次,即使B
该新页面上有多个部分。因此我对其进行了修改,如下所示:
\documentclass{article}
\usepackage{refcount}
\usepackage{etoolbox}
\newcounter{envAcounter}
\newcounter{envBcounter}
\newtoggle{newPage}
\togglefalse{newPage}
\newenvironment{A}{%
\stepcounter{envAcounter}%
\null\label{env:A:start:\arabic{envAcounter}}%
}{\label{env:A:end:\arabic{envAcounter}}
\togglefalse{newPage}
}%
\newenvironment{B}{%
\stepcounter{envBcounter}%
\null\label{env:B:start:\arabic{envBcounter}}%
\iftoggle{newPage}{}{
\ifnum\getpagerefnumber{env:A:start:\arabic{envAcounter}}=%
\getpagerefnumber{env:B:start:\arabic{envBcounter}}\relax%
Pagebreak did not take place before starting environment B!%
\else
Pagebreak took place before starting environment B!%
\toggletrue{newPage}
\fi}
}{\label{env:B:end:\arabic{envBcounter}}}%
\begin{document}
\begin{A}
Text
\begin{B}
SubText
\end{B}
\end{A}
\begin{A}
Text2
\newpage
\begin{B}
SubText2
\end{B}
\begin{B}
SubText3
\end{B}
\end{A}
\end{document}
true
这个想法是,一旦检测到第一个分页符,就设置一个-value,然后跳过检查。但这样做不行,每次我在环境中时,我仍然会检查是否在执行新页面B
。为什么切换不能按预期工作,我该如何修复?
答案1
(La)TeX 中的某些内容具有有限的范围。如果您希望其修改在进行更改的组中继续有效,则必须确保它是\global
ly 进行的(或者,也许以在组之后进行的方式进行):
\documentclass{article}
\usepackage{etoolbox,etextools}
\newcommand{\showtoggle}[1]{#1: \iftoggle{#1}{true}{false}\par}
\newtoggle{mytoggle}
%\togglefalse{mytoggle}% default
\newenvironment{myenv}%
{\bigskip
begin: \showtoggle{mytoggle}}
{end: \showtoggle{mytoggle}}
\begin{document}
\togglefalse{mytoggle}
\begin{myenv}% false
inside: \showtoggle{mytoggle}% false
\end{myenv}% false
outside: \showtoggle{mytoggle}% false
\toggletrue{mytoggle}
\begin{myenv}% true
inside: \showtoggle{mytoggle}% true
\end{myenv}% true
outside: \showtoggle{mytoggle}% true
\togglefalse{mytoggle}
\begin{myenv}% false
\toggletrue{mytoggle}
inside: \showtoggle{mytoggle}% true
\end{myenv}% true
outside: \showtoggle{mytoggle}% false
\togglefalse{mytoggle}
\begin{myenv}% false
\AfterGroup{\toggletrue{mytoggle}}
inside: \showtoggle{mytoggle}% false
\end{myenv}% false
outside: \showtoggle{mytoggle}% true
\togglefalse{mytoggle}
\begin{myenv}% false
\global\toggletrue{mytoggle}
inside: \showtoggle{mytoggle}% true
\end{myenv}% true
outside: \showtoggle{mytoggle}% true
\end{document}
计数器是不同的...