![algorithm2e 的锚点错误,标题位于顶部](https://linux22.com/image/465735/algorithm2e%20%E7%9A%84%E9%94%9A%E7%82%B9%E9%94%99%E8%AF%AF%EF%BC%8C%E6%A0%87%E9%A2%98%E4%BD%8D%E4%BA%8E%E9%A1%B6%E9%83%A8.png)
我有以下 MWE,我使用该包algorithm2e
打印算法。但是,我喜欢将算法的标题放在顶部(如何做到这一点已经在这个问题)。标题的放置很好。
不幸的是,hyperref
锚点放错了位置。当我点击链接时,页面跳转到算法的底部“返回”行。我怎样才能让它跳转,使标题成为显示屏上的第一行?
\documentclass{article}
\usepackage[boxed]{algorithm2e}
% https://tex.stackexchange.com/questions/147747/algorithm2e-caption-above-code
\makeatletter
\renewcommand{\@algocf@capt@boxed}{above} % formerly {bottom}
\makeatother
\usepackage[linkcolor=red]{hyperref}
\begin{document}
Algorithm~\ref{algo:myalgo}.
\begin{algorithm}
Do something.\\
Calculate something.\\
Erase something.\\
Return.
\caption{The caption}
\label{algo:myalgo}
\end{algorithm}
\end{document}
答案1
您可以尝试这样的事情:
\documentclass{article}
\usepackage[boxed]{algorithm2e}
\usepackage{etoolbox}
% https://tex.stackexchange.com/questions/147747/algorithm2e-caption-above-code
\makeatletter
\renewcommand{\@algocf@capt@boxed}{above} % formerly {bottom}
\renewcommand{\algocf@caption@boxed}{\vskip\AlCapSkip\MakeLinkTarget[topalgocf]{algocf}\box\algocf@capbox}%
\patchcmd\algocf@caption@algo
{\hyper@refstepcounter{algocf}}
{\hyper@refstepcounter{algocf}\xdef\@currentHref{top\@currentHref}}{}{\fail}
\makeatother
\usepackage[linkcolor=red]{hyperref}
\begin{document}
Algorithm~\ref{algo:myalgo}.
\begin{algorithm}
Do something.\\
Calculate something.\\
Erase something.\\
Return.
\caption{The caption}\label{algo:myalgo}
\end{algorithm}
\end{document}