如何在算法的后半部分对页面引用进行超引用,而不依赖于前半部分

如何在算法的后半部分对页面引用进行超引用,而不依赖于前半部分

我正在努力解决一个对我来说似乎相当复杂的事情,但也许已经有更好的解决方案了。如果没有,我真的很感激任何帮助解决以下问题的帮助。

我的文档中有一个很长的算法,我把它分成了两部分。它是使用编写的algorithmicx。我的要求如下:

  1. 由于它们是相同的算法,因此算法编号(例如算法 1.3)应该相同。
  2. 在前半部分的结尾,我想要一行说该算法稍后继续。
  3. 如果后半部分刚好在下一页,我只想说“下页继续”。如果它在下一页之后的页面上,我希望注释说“第 x 页继续”。
  4. 我想要“续第 x 页”来超级引用算法后半部分的页面。

我在第 3 点中意识到,后半部分不出现在下一页是不寻常的,但我对这作为练习感到好奇。此外,您可能希望在文本中描述冗长的算法的一半,展示前半部分的伪代码,然后在文本中再次讨论后半部分,然后再继续伪代码,在这种情况下,它们最终会相隔多页。

我目前针对这些问题的解决方案是:

  1. 这是使用包解决的\ContinuedFloatsubcaption如果没有这个命令,算法的后半部分将在标题中得到一个递增的数字。
  2. 根据我提出的问题,这个问题可以通过 zref 和对绝对页码的条件检查来解决这里
  3. 见2。
  4. 这就是问题所在。目前,我\label在每个算法环境中都有一个单独的环境,但当使用超链接时,后半部分的链接会将我带回到前半部分(正确),而前半部分的链接只会将我带到前半部分(不正确),而它应该将我带到后半部分。

我认为 4 的问题在于 subcaption 和 hyperref 包之间存在冲突,因为指向图形的链接通常会将您带到图形的顶部,因此指向算法后半部分的链接会将我带到第一个算法的顶部,因为我使用了,所以它将\ContinuedFloat它们都视为一个大浮点数。此外,当我构建时,我收到一条警告“具有相同标识符(name{algorithm0.1})的目标已被使用”。但是,PDF 中显示的页码是正确的,它们只是链接不正确。

我在下面提供了 MWE。请注意,当您单击算法前半部分底部的“继续”链接时,它不会带您进入后半部分。

有解决方案吗?另外,如果有一个整体解决方案可以拆分算法并完成我在这里尝试做的所有事情,那就太好了。

非常感谢每一位阅读这个冗长问题的人。

梅威瑟:

\documentclass[12pt, oneside]{report} 

\usepackage[english]{babel}    
\usepackage{blindtext}

\usepackage[font=small,tableposition=top]{caption}
\usepackage[skip=6pt,hypcap=true]{subcaption}

\usepackage[chapter]{algorithm}
\usepackage{algorithmicx,algpseudocode}

\usepackage[user,abspage]{zref}
% macro to compare two absolute page numbers to see if the first is greater than the second by more than one
\makeatletter
\newcommand\cmpabspage[2]{%
   \ifnum\zref@extract{#1}{abspage}>\numexpr\zref@extract{#2}{abspage}+1\relax
       \expandafter\@firstoftwo
   \else
       \expandafter\@secondoftwo
   \fi
}
\makeatother

\usepackage[pdftex]{hyperref}
\hypersetup{colorlinks=true}
\newcommand{\pagename}{page }

\begin{document}

\blindtext

Pseudocode is shown in Algorithm \ref{longalg}.

\begin{algorithm}[!htbp]
  \caption{Long algorithm part 1}
    \label{longalg}
    \label{longalg-1}
    \zlabel{z.longalg-1}

\begin{algorithmic}[1]  

        \State The first part of a lengthy algorithm would go here. 

        \cmpabspage{z.longalg-2}{z.longalg-1}
        {\Statex Continued on \pagename \pageref{longalg-2}}
        {\Statex Continued on next page}

    \algstore{algstore.longalg}
    \end{algorithmic}
\end{algorithm}

\Blindtext[4][4]

\begin{algorithm}[!htbp]

\ContinuedFloat
\caption{Long algorithm part 2}
\label{longalg-2}
\zlabel{z.longalg-2}

\begin{algorithmic}[1]
        \algrestore{algstore.longalg}

        \cmpabspage{z.longalg-2}{z.longalg-1}
        {\Statex Continued from \pagename \pageref{longalg-1}}
        {\Statex Continued from previous page}

        \State The second part of a lengthy algorithm would go here.    

    \end{algorithmic}
\end{algorithm}

\end{document}

谢谢。

答案1

问题在于 的包加载顺序不正确。因此,在编译示例代码时会发出hyperref警告。pdfTeX warning (ext4): destination with the same id entifier (name{algorithm.0.1}) has been already used, duplicate ignored

algorithm加载使用float包,浮动包应该加载hyperref,但应该使用软件包hyperref。(详情请参阅 README 文件)

float可以通过手动加载包(之前hyperref)和algorithm之后加载包来解决此冲突hyperref;这给了我一个正确的超链接:


\documentclass[12pt, oneside]{report} 

\usepackage[english]{babel}    
\usepackage{blindtext}

\usepackage[font=small,tableposition=top]{caption}
\usepackage[skip=6pt,hypcap=true]{subcaption}

\usepackage{float}

\usepackage[user,abspage]{zref}
% macro to compare two absolute page numbers to see if the first is greater than the second by more than one
\makeatletter
\newcommand\cmpabspage[2]{%
   \ifnum\zref@extract{#1}{abspage}>\numexpr\zref@extract{#2}{abspage}+1\relax
       \expandafter\@firstoftwo
   \else
       \expandafter\@secondoftwo
   \fi
}
\makeatother

\usepackage[pdftex]{hyperref}
\hypersetup{colorlinks=true}
\newcommand{\pagename}{page }

\usepackage[chapter]{algorithm}
\usepackage{algorithmicx,algpseudocode}

\begin{document}

\blindtext

Pseudocode is shown in Algorithm \ref{longalg}.

\begin{algorithm}[!htbp]
  \caption{Long algorithm part 1}
    \label{longalg}
    \label{longalg-1}
    \zlabel{z.longalg-1}

\begin{algorithmic}[1]  

        \State The first part of a lengthy algorithm would go here. 

        \cmpabspage{z.longalg-2}{z.longalg-1}
        {\Statex Continued on \pagename \pageref{longalg-2}}
        {\Statex Continued on next page}

    \algstore{algstore.longalg}
    \end{algorithmic}
\end{algorithm}

\Blindtext[4][4]

\begin{algorithm}[!htbp]

\ContinuedFloat
\caption{Long algorithm part 2}
\label{longalg-2}
\zlabel{z.longalg-2}

\begin{algorithmic}[1]
        \algrestore{algstore.longalg}

        \cmpabspage{z.longalg-2}{z.longalg-1}
        {\Statex Continued from \pagename \pageref{longalg-1}}
        {\Statex Continued from previous page}

        \State The second part of a lengthy algorithm would go here.    

    \end{algorithmic}
\end{algorithm}

\end{document}

也可以看看:https://texfaq.org/FAQ-hyperdupdest

答案2

我将通过定义一个来处理你的页面引用问题新的计数器将用于规避hyperref“具有相同标识符(name{algorithm0.1})的目标已被使用”警告。保持某事物与其他事物“独立”的想法本能地表明了这一点。新的计数器将仅用于引用页面,尽管它甚至还可以用于正确引用算法的第二部分。为此,以下简化的更改对我来说很有效:

...
\newcounter{newalgcounter}% New counter definition
\renewcommand{\thenewalgcounter}{\thechapter.\arabic{newalgcounter}}% New counter typesetting (this should match that of the algorithm package
...
\begin{algorithm}[!htbp]% Part 1 of algorithm
  \caption{Long algorithm part 1}
  ...
  {\Statex Continued on \pagename \pageref{longalg-2-page}}% Reference to new counter
  ...
\end{algorithm}
...
...
\begin{algorithm}[!htbp]% Part 2 of algorithm
  \ContinuedFloat
  \setcounter{newalgcounter}{\value{algorithm}}% update new algorithm counter
  \caption{Long algorithm part 2}
  ...
  \refstepcounter{newalgcounter} \label{longalg-2-page}% New counter incremented & labelled
\end{algorithm}
...

虽然这不能消除对重复名称的引用(来自hyperref),但也可以通过定义与 重复的环境来避免algorithm- 例如\begin{dupalgorithm}...\end{dupalgorithm}。但是,这取决于您的编码偏好。

相关内容