有没有办法根据浮动元素是否位于同一调出页面上(再次)来改变浮动元素的样式?

有没有办法根据浮动元素是否位于同一调出页面上(再次)来改变浮动元素的样式?

(这是对(不幸被关闭的)问题的后续有没有办法根据浮动元素是否位于同一调出页面上来改变其样式?))

为了根据浮动元素是否位于同一调出页面上来更改其样式,我尝试实施 David 的建议:

设置标签,然后检查\pageref当前页面,如果它们相同,则在下次运行时修复排版

(据我所知,我不得不求助于,refcount因为\getpagerefnumber无法直接将\thepage与字符串进行比较\pageref{...},但我遇到的问题出现在这两者上。)

但这种方法似乎存在一个问题:在下面的 MCE 中,(第二个)浮动的调出页面(理论上由当前页面计数器给出\thepage)和显示页面(由和给出\pageref{...}\getpagerefnumber{...}都是第 2 页,但却\thepage显示它是第 1 页。

好的,\thepage以总能给出正确的数字而闻名。所以问题仍然存在:有没有办法根据浮动元素是否位于同一调出页面上来改变其样式?

\documentclass{article}
\usepackage{refcount}
\usepackage{mwe}

\AddToHook{env/figure/end}{
  \caption{The nice figure \#\thefigure{}
    (%
    thepage:          \thepage,
    pageref:          \pageref{ex-\thefigure},
    getpagerefnumber: \getpagerefnumber{ex-\thefigure}%
    )%
  }
  \label{ex-\thefigure}
}
\begin{document}
\begin{figure}[htb]
  \centering
  \includegraphics[width=.5\linewidth]{example-image-a}
\end{figure}

\begin{itemize}
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
  vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
  magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique
  senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras
  viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla
  ultrices. Phasellus eu tellus sit amet tortor gravida placerat. In- teger
  sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel
  leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, male- suada eu,
  pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla.  Donec varius
  orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis,
  diam. Duis eget orci sit amet orci dignissim rutrum.
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
  vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
  magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique
  senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras
  viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla
  ultrices. Phasellus eu tellus sit amet tortor gravida placerat. In- teger
  sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel
  leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, male- suada eu,
  pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla.  Donec varius
  orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis,
  diam. Duis eget orci sit amet orci dignissim rutrum.

  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
  vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
  magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique
  senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras
  viverra metus rhoncus sem. Nulla et lectus vestibulum urna
\item Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
  vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
  mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
  magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique
  senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras
  viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla
  ultrices. Phasellus eu tellus sit amet tortor gravida placerat. In- teger
  sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel
  leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, male- suada eu,
  pulvinar at, mollis ac, nulla. Curabitur auctor semper nulla.  Donec varius
  orci eget risus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis,
  diam. Duis eget orci sit amet orci dignissim rutrum.
  \begin{figure}[htb]
    \centering
    \includegraphics[width=.5\linewidth]{example-image-b}
  \end{figure}
\end{itemize}
\end{document}

在此处输入图片描述

答案1

这是对任务的一次尝试。你说你想根据浮动是否与标注在同一页来改变浮动的样式。但无论是在这个问题的 MCE 中还是在原始问题中都没有出现“标注”。据我所知,你需要两个标签,一个在浮动上,另一个在“标注”上,然后你可以比较两者的 pageref。

我没有使用env/figure/end钩子,因为就目前情况而言,它要求我们知道\thefigure何时进行调用。我更喜欢像往常一样使用标签,但在这个简单的实现中,这涉及重新声明条件的标签\IfOnTheSamePage,因此有一点重复。它仍然看起来合理,至少是一个概念证明。

\documentclass{article}
\usepackage{refcount}
\usepackage{mwe}

\newcounter{dbcallout}
\NewDocumentCommand\dbcallout{m}
  {%
    \refstepcounter{dbcallout}%
    \label{callout-#1}%
    callout~\ref{#1}%
  }
\ExplSyntaxOn
\prg_new_protected_conditional:Npnn \__db_if_on_same_page:n #1 { TF }
  {
    \group_begin:
    \tl_if_exist:cTF { r@ #1 }
      { \tl_set:Nx \l_tmpa_tl { \getpagerefnumber {#1} } }
      { \tl_clear:N \l_tmpa_tl }
    \tl_if_exist:cTF { r@ callout - #1 }
      { \tl_set:Nx \l_tmpb_tl { \getpagerefnumber { callout - #1 } } }
      { \tl_clear:N \l_tmpb_tl }
    \tl_if_eq:NNTF \l_tmpa_tl \l_tmpb_tl
      {
        \tl_if_empty:NTF \l_tmpa_tl
          { \group_insert_after:N \prg_return_false: }
          { \group_insert_after:N \prg_return_true:  }
      }
      { \group_insert_after:N \prg_return_false: }
    \group_end:
  }
\NewDocumentCommand \IfOnTheSamePage { m m m }
  { \__db_if_on_same_page:nTF {#1} {#2} {#3} }
\ExplSyntaxOff

\begin{document}
\begin{figure}[htb]
  \caption{%
    \IfOnTheSamePage{fig:1}{\bfseries}{}%
    Caption%
  }
  \label{fig:1}
  \centering
  \includegraphics[width=.5\linewidth]{example-image-a}
\end{figure}

\dbcallout{fig:1}

\dbcallout{fig:2}

\begin{itemize}
\item \lipsum[1]
\item \lipsum[2-3]
\item \lipsum[4]
  \begin{figure}[htb]
    \caption{%
      \IfOnTheSamePage{fig:2}{\bfseries}{}%
      Caption%
    }
    \label{fig:2}
    \centering
    \includegraphics[width=.5\linewidth]{example-image-b}
  \end{figure}
\end{itemize}
\end{document}

在此处输入图片描述


在此处输入图片描述

当然,上面检查了callout开始与浮动元素在同一页上。如果您想检查标注是否在同一页上开始和结束,则必须在标注末尾添加第三个标签,然后比较这三个标签(类似于验证引用varioref是否跨越页面边界的操作)。

答案2

最后,我想我可以借助这个技巧来自 Heiko Oberdiek 和来自gusbrs 的回答

\documentclass{article}
\usepackage{refcount}
\usepackage{mwe}
\usepackage{xcolor}

\ExplSyntaxOn
\int_new:N \g__call_out_label_int
\str_new:N \g__call_out_label_str

\AddToHook{env/figure/before}{
  \leavevmode
  \int_incr:N \g__call_out_label_int
  \str_set:Nx \g__call_out_label_str {call_out_label:\int_use:N \g__call_out_label_int}
  \label{\g__call_out_label_str}
}
\AddToHook{env/figure/end}{
  \caption{%
    \tl_set:Nx \l_tmpa_tl { \getpagerefnumber {\g__call_out_label_str} }
    \tl_set:Nx \l_tmpb_tl { \getpagerefnumber {ex-\thefigure} }
    \tl_if_eq:NNTF \l_tmpa_tl \l_tmpb_tl
    {
      \color{blue}
      Float~ on~ the~ same~ page~ as~ its~ call-out~ point
    }{
      \color{red}
      Float~ NOT~ on~ the~ same~ page~ as~ its~ call-out~ point
    }
  }
  \label{ex-\thefigure}
}
\ExplSyntaxOff
\begin{document}
\begin{figure}[htb]
  \centering
  \includegraphics[width=.5\linewidth]{example-image-a}
\end{figure}

\lipsum[1-2]

\begin{figure}[htb]
  \centering
  \includegraphics[width=.5\linewidth]{example-image-b}
\end{figure}
\end{document}

在此处输入图片描述

相关内容