\thethisframe 在流程框架的第一行返回错误的 ID

\thethisframe 在流程框架的第一行返回错误的 ID

我有一个代码,可以贯穿并自动化流程以生成乳胶布局。

在这个过程中,我需要识别文本的特定点位于哪个流程框架中。例如,我需要知道第二段的结尾是在流程框架 1 还是 2 上。

为了实现这一点,我使用命令 \thethis frame,它返回计数器 \c@thisframe 中包含的与当前帧相对应的值。

在我查询的行是流程框架的第一行之前,这都是可行的。在这种情况下,返回的 ID 来自以前的框架。

这是一个简单的例子

\documentclass[a4paper]{article}
\usepackage{flowfram}

\twocolumn

\begin{document}
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa \textbf{The current frame id is: \thethisframe} aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa \textbf{And now the frame id is: \thethisframe}

aaa \textbf{Here the frame id is: \thethisframe}

\end{document}

产生以下结果。

Code sample result

有没有办法让我始终能够识别任何特定位置的正确框架?或者我该如何修复代码以避免发生此错误?

答案1

问题是由 TeX 的异步输出例程引起的。计数寄存器直到段落中断时才会更新。计数也出现了同样的问题,page如下所示:

\documentclass[a4paper]{article}

\usepackage{geometry}
\usepackage{lipsum}

\begin{document}
\lipsum[1-6]

\lipsum*[7] Page: \thepage.

\end{document}

解决这个问题的标准方法是使用\label/\pageref像这样:

\documentclass[a4paper]{article}

\usepackage{geometry}
\usepackage{lipsum}

\begin{document}
\lipsum[1-6]

\lipsum*[7] Page: \label{here}\pageref{here}.

\end{document}

不幸的是,这仅适用于使用 增加的计数器\refstepcounter。计数寄存器\c@thisframe不会增加,而是设置为对给定页面有效的下一个可用帧。但是,flowfram提供了一个命令来标记当前帧:

\newcommand*{\labelflowidn}[1]{%
  {%
    \def\@currentlabel{\thethisframe}%
    \label{#1}%
  }%
}

不幸的是,与 不同\c@page,直到文件发生\c@thisframe之后才稳定下来。\protected@write.aux

解决此问题的一种方法是进行\@setcol如下修补:

\makeatletter
\let\@org@setcol\@setcol

\def\dodeferredstuff{}

\renewcommand{\@setcol}[1]{\@org@setcol{#1}\dodeferredstuff}

\newcommand{\deferredlabelflowidn}[1]{%
  \def\dodeferredstuff{\labelflowidn{#1}\let\dodeferredstuff\relax}%
}
\makeatother

并使用\deferredlabelflowidx。调整后的 MWE:

\documentclass[a4paper]{article}

\usepackage{flowfram}

\makeatletter
\let\@org@setcol\@setcol

\def\dodeferredstuff{}

\renewcommand{\@setcol}[1]{\@org@setcol{#1}\dodeferredstuff}

\newcommand{\deferredlabelflowidn}[1]{%
  \def\dodeferredstuff{\labelflowidn{#1}\let\dodeferredstuff\relax}%
}
\makeatother

\twocolumn

\begin{document}
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa \textbf{The current frame id is:
\thethisframe} aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa \textbf{And
now the frame id is: \thethisframe} Label/ref: 
\labelflowidn{here}\ref{here}.
Deferred: \deferredlabelflowidn{defhere}\ref{defhere}.

aaa \textbf{Here the frame id is: \thethisframe}


\end{document}

Label/ref: 1. Deferred: 2.

不幸的是,这仅在框架的最后一段中使用时才有效。更好的方法是在下一个段落的开头使用以下方法执行延迟命令\everypar

\def\dodeferredstuff{}

\newcommand{\deferredlabelflowidn}[1]{%
  \def\dodeferredstuff{\labelflowidn{#1}\let\dodeferredstuff\relax}%
}

\everypar{\dodeferredstuff}

为了防止重置部分命令\everypar

\def\dodeferredstuff{}

\newcommand{\deferredlabelflowidn}[1]{%
  \def\dodeferredstuff{\labelflowidn{#1}\let\dodeferredstuff\relax}%
  \everypar{\dodeferredstuff}%
}

以下是修改后的 MWE:

\documentclass[a4paper]{article}

\usepackage{flowfram}

\def\dodeferredstuff{}

\newcommand{\deferredlabelflowidn}[1]{%
  \def\dodeferredstuff{\labelflowidn{#1}\let\dodeferredstuff\relax}%
  \everypar{\dodeferredstuff}%
}

\twocolumn

\begin{document}
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
Deferred: \deferredlabelflowidn{sample}\ref{sample}.

aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa 
aaa aaa aaa aaa aaa aaa aaa aaa \textbf{The current frame id is:
\thethisframe} aaa aaa aaa aaa aaa aaa aaa aaa aaa aaa \textbf{And
now the frame id is: \thethisframe} Label/ref: 
\labelflowidn{here}\ref{here}.
Deferred: \deferredlabelflowidn{defhere}\ref{defhere}.

aaa \textbf{Here the frame id is: \thethisframe}


\end{document}

image of result

相关内容