我刚刚发现了 hypcap 软件包,它改进了\ref
指向图表之前的链接,而不是指向标题。如何使用该ruled
选项对使用 algorithm2e 编写的算法执行相同的操作?
这是一个简单的例子,我们点击 ref 后无法阅读标题:
\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{hyperref}
\usepackage{hypcap}
\begin{document}
\begin{algorithm}[H]
\capstart
\caption{How to write algorithms}
\label{alg:sample}
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\end{algorithm}
Text with a ref to algorithm \ref{alg:sample}.
\end{document}
答案1
其工作方式与其他浮点数相同:放在\capstart
浮点数的开头。
\documentclass{article}
\usepackage{algorithm2e}
\usepackage{hyperref}
\usepackage{hypcap}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{algorithm}[H]
\capstart
\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{alg:sample}
\end{algorithm}
Text.
\lipsum[2-5]
Algorithm~\ref{alg:sample}.
\lipsum[3]
\end{document}