对于特定的应用程序,我需要知道页面是否包含顶部浮动。因此,我采用了条件\iftopfloat{<true>}{<false>}
以下链接中给出的条件定义:有没有办法确定页面上是否有浮动?。
最初,此条件用于文档页眉和页脚。但就我而言,我需要在文档的“正文”中使用它。然而,此条件在此上下文中不起作用,并且无论是否存在顶部浮动,始终都会执行 true 分支。
如何(从文档“主体”中)确定页面上是否有顶部浮动?
\documentclass{article}
\usepackage{mwe}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% From : https://tex.stackexchange.com/questions/56673/is-there-a-way-to-determine-if-there-is-a-float-on-a-page
\makeatletter
\let\latex@makecol\@makecol
\def\@makecol{%
\let\topfloat\@toplist%
\latex@makecol%
}
\def\iftopfloat#1#2{\ifx\topfloat\empty #2\else #1\fi}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\iftopfloat{Yes}{No}
\clearpage
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\clearpage\iftopfloat{Yes}{No}
\clearpage
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\clearpage
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\clearpage\iftopfloat{Yes}{No}
\clearpage\iftopfloat{Yes}{No}
\end{document}
答案1
您无法在文档正文内进行测试,但您可以检测上次运行时清除页面后的第一行是否设置在页面顶部,从而选择合适的分支。这总是需要至少两次运行才能收敛。
\documentclass{article}
\usepackage{graphicx}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
% pdftex version, others would be similar
\def\iftopfloat{%
\leavevmode
\pdfsavepos
\write\@auxout{\string\gdef\string\TPF\roman{page}{\the\pdflastypos}}%
\ifdim\ifcsname TPF\roman{page}\endcsname
\csname TPF\roman{page}\endcsname sp\else\z@\fi
>400pt
\expandafter\@secondoftwo
\else
\expandafter\@firstoftwo
\fi
}
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\Huge\bfseries
\iftopfloat{Yes}{No}
\clearpage
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\clearpage\iftopfloat{Yes}{No}
\clearpage
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\clearpage
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\clearpage\iftopfloat{Yes}{No}
\clearpage\iftopfloat{Yes}{No}
\end{document}
答案2
借鉴 David Carlisle 的两次连续编译并写入.aux
文件的想法,我修改了命令,以便将有顶部浮动的页面\@combinefloats
写入文件。.aux
\documentclass{article}
\usepackage{mwe,lipsum,refcount}
\makeatletter
\def \@combinefloats {%
\ifx \@toplist\@empty%
\else%
\@cflt%
\immediate\write\@auxout{\string\global\string\@namedef{pageWithTopFloat-\thepage}{1}}%
\fi%
\ifx \@botlist\@empty \else \@cflb \fi%
}
\newcounter{ifTopFloatCnt}
\def\iftopfloat#1#2{%
\global\advance\c@ifTopFloatCnt\@ne%
\label{\the\c@ifTopFloatCnt @ifTopFloat}%
\ifcsname pageWithTopFloat-\getpagerefnumber{\the\c@ifTopFloatCnt @ifTopFloat}\endcsname%
{\Large\bfseries #1}%
\else%
{\Large\bfseries #2}%
\fi%
}
\makeatother
\begin{document}
\iftopfloat{Yes}{No}
\noindent
p. 1: \ifcsname pageWithTopFloat-1\endcsname yes \else no \fi\\
p. 2: \ifcsname pageWithTopFloat-2\endcsname yes \else no \fi\\
p. 3: \ifcsname pageWithTopFloat-3\endcsname yes \else no \fi\\
p. 4: \ifcsname pageWithTopFloat-4\endcsname yes \else no \fi\\
p. 5: \ifcsname pageWithTopFloat-5\endcsname yes \else no \fi\\
p. 6: \ifcsname pageWithTopFloat-6\endcsname yes \else no \fi\\
p. 7: \ifcsname pageWithTopFloat-7\endcsname yes \else no \fi
\lipsum[1-4]
\clearpage
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\lipsum[1-4]
\iftopfloat{Yes}{No}
\lipsum[1-4]
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\lipsum[1-4]
\begin{figure}[t]
\centering
\includegraphics{example-image}
\end{figure}
\iftopfloat{Yes}{No}
\lipsum[1-4]
\iftopfloat{Yes}{No}
\lipsum[1-4]
\iftopfloat{Yes}{No}
\end{document}