如何在 Algorithm2e 中使用 Nameref

如何在 Algorithm2e 中使用 Nameref

正如标题所示,我试图引用由algorithm2e包创建的算法。这是一个 MWE:

\documentclass[12pt,a4paper]{report}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{hyperref}

\begin{document}
Hello world. To learn how to write pseudo-code, refer to algorithm \nameref{bla}.
%The pseudo-code is taken from the v4.01 manual    
\begin{algorithm}[H]
    \SetAlgoLined
    \KwData{this text}
    \KwResult{how to write algorithm with \LaTeX2e }
        initialization\;
        \While{not at end of this document}{
            read current\;
            \eIf{understand}{
                go to next section\;
                current section becomes this one\;
            }{
                go back to the beginning of current section\;
            }
        }
    \caption{How to write algorithms}
    \label{bla}
\end{algorithm}

\end{document}

结果如下:

enter image description here

如您所见,hyperlink-ref 已正确生成,但显然没有文本(但是我可以单击空链接框,它会将我带到正确的位置)。

问题是,如何为要使用的算法分配一个名称nameref

答案1

软件包重新定义了其环境中algorithm2e的内部内容,因此的修补遗漏了这种情况。以下修补程序可获取 的标题:\caption\nameref\algocf@latexcaption\nameref

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}
\usepackage{hyperref}

\makeatletter
\let\original@algocf@latexcaption\algocf@latexcaption
\long\def\algocf@latexcaption#1[#2]{%
  \@ifundefined{NR@gettitle}{%
    \def\@currentlabelname{#2}%
  }{%
    \NR@gettitle{#2}%
  }%
  \original@algocf@latexcaption{#1}[{#2}]%
}
\makeatother

\begin{document}
Hello world. To learn how to write pseudo-code, refer to algorithm
\nameref{bla}.

%The pseudo-code is taken from the v4.01 manual
\begin{algorithm}[H]
    \SetAlgoLined
    \KwData{this text}
    \KwResult{how to write algorithm with \LaTeX2e }
        initialization\;
        \While{not at end of this document}{
            read current\;
            \eIf{understand}{
                go to next section\;
                current section becomes this one\;
            }{
                go back to the beginning of current section\;
            }
        }
    \caption{How to write algorithms}
    \label{bla}
\end{algorithm}
\end{document}

Result

具有独立所有权的示例

在以下示例中,宏\algotitle设置了一个锚点并需要两个参数,即的标题\nameref和的名称\label

\documentclass{article}

\usepackage[linesnumbered]{algorithm2e}
\usepackage{hyperref}

\makeatletter
\newcommand*{\algotitle}[2]{%
  \stepcounter{algocf}%
  \hypertarget{algocf.title.\theHalgocf}{}%
  \NR@gettitle{#1}%
  \label{#2}%
  \addtocounter{algocf}{-1}%
}
\makeatother

\begin{document}
Hello world. To learn how to write pseudo-code, refer to algorithm
\nameref{bla.title}.

%The pseudo-code is taken from the v4.01 manual
\begin{algorithm}[H]
    \algotitle{Bla bla}{bla.title}
    \SetAlgoLined
    \KwData{this text}
    \KwResult{how to write algorithm with \LaTeX2e }
        initialization\;
        \While{not at end of this document}{
            read current\;
            \eIf{understand}{
                go to next section\;
                current section becomes this one\;
            }{
                go back to the beginning of current section\;
            }
        }
    \caption{How to write algorithms}
    \label{bla}
\end{algorithm}
\end{document} 

Result

答案2

algorithm2e使用其自己的 caption 定义。因此您必须重新定义它。您必须为 提供一个字符串\nameref。这可以通过 来实现\Nr@gettitle

\documentclass[12pt,a4paper]{report}
\usepackage[linesnumbered]{algorithm2e}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{etoolbox}
\makeatletter
\long\def\algocf@caption@algo#1[#2]#3{%
  \ifthenelse{\equal{\algocf@algocfref}{\relax}}{}{\algocf@captionref}%
  \@ifundefined{hyper@refstepcounter}{\relax}{% if hyper@refstepcounter undefind, no hyperref, else...
    \ifthenelse{\equal{\algocf@algocfref}{\relax}}{\renewcommand{\theHalgocf}{\thealgocf}}{% take algocf as Href
      \renewcommand{\theHalgocf}{\algocf@algocfref}}%else if SetAlgoRefName done, take this name as ref.
    \hyper@refstepcounter{algocf}%set algocf as category of ref
  }%
     \NR@gettitle{#2}% 
  \algocf@latexcaption{#1}[{#2}]{{#3}}% call original caption
}%
\makeatother

\begin{document}
\begin{verbatim}
\ref{fig} + \nameref{fig} + \autoref{fig}

\ref{algo} + \nameref{algo} + \autoref{algo}
\end{verbatim}

\ref{fig} + \nameref{fig} + \autoref{fig}

\ref{algo} + \nameref{algo} + \autoref{algo}

\begin{algorithm}[H]
    \caption{How to write algorithms}
    \label{algo}
\end{algorithm}

\begin{figure}[!ht]
\caption{figure fdoo}\label{fig}
\end{figure}
\end{document}

enter image description here

相关内容