我有以下 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}