测试页面是否为空

测试页面是否为空

如果页面没有文本,我需要一个宏来测试,这意味着当前点是文本区域的左上角。这不起作用:

\documentclass{article}

\makeatletter
\newcommand*\isOnTopOfPage{%
  \ifdim \pagetotal > \pagegoal
   \expandafter\@secondoftwo
  \else
   \expandafter\@firstoftwo
  \fi
}
\makeatother

\begin{document}
foo bar \par
\isOnTopOfPage{>> On top of the page}%
  {>> not on top of the page}

\clearpage
\isOnTopOfPage{>> On top of the page}%
  {>> not on top of the page}

\end{document}

编辑 1:问题仍然是没有 a 的行\par和空框。只有第二个是正确的:

\documentclass{article}

\makeatletter
\providecommand\@secondofthree[3]{#2}
\newcommand*\isOnTopOfPage{%
  \ifdim\pagetotal=\z@
    \unless\ifdim\pagegoal=\maxdimen
      \expandafter\@gobbletwo
    \fi
    \expandafter\@secondofthree
  \fi
  \@secondoftwo
} 
\makeatother

\begin{document}
f
\isOnTopOfPage{>> On top of the page}%
  {>> not on top of the page}

\clearpage
\isOnTopOfPage{>> On top of the page}%
  {>> not on top of the page}

\clearpage
\null% empty box
\isOnTopOfPage{>> On top of the page}%
  {>> not on top of the page}

\end{document}

答案1

摘自 TeX 书:

当当前页面不包含任何盒子时,\pagetotal其相对值为零,即\pagegoal16383.99998 pt(TeX 最大的 ⟨dimen⟩);

因此以下测试可以完成您想要的操作:

\documentclass{article}

\makeatletter
\providecommand\@secondofthree[3]{#2}
\newcommand*\isOnTopOfPage{%
  \ifdim\pagetotal=\z@
    \unless\ifdim\pagegoal=\maxdimen
      \expandafter\@gobbletwo
    \fi
    \expandafter\@secondofthree
  \fi
  \@secondoftwo
}
\makeatother

\begin{document}
foo bar \par
\isOnTopOfPage{>> On top of the page}%
  {>> not on top of the page}

\clearpage
\isOnTopOfPage{>> On top of the page}%
  {>> not on top of the page}

\end{document} 

相关内容