我想要两件东西
- 在算法标题后面的括号中显示输入参数列表,但隐藏在算法中(传统的“输入:X,Y,Z”)。
- 无需在括号中输入即可引用标题。
我的代码如下所示。它未用于生成上面所示的所需输出:
\documentclass{report}
\usepackage[linesnumbered,commentsnumbered,resetcount,noalgohanging,ruled]{algorithm2e}
\NoCaptionOfAlgo
\usepackage{hyperref}
\begin{document}
\section{ex}
\nameref{abc} - Should read 'HowToWriteAlgorithms' without the input in parenthesis\\
\begin{algorithm}[H]
\caption{HowToWriteAlgorithms}
\label{abc}
\KwIn{x}
\While{not at end of this document}{Try this}
\end{algorithm}
\end{document}
答案1
algorithm2e
需要补丁才能正常运行,\nameref{}
具体说明如何在 Algorithm2e 中使用 Nameref
除此之外,要在算法内部和外部(\nameref
,listofalgorithms
)使用不同的标题文本,请使用可选参数\caption
。
\documentclass{report}
\usepackage[linesnumbered,commentsnumbered,resetcount,noalgohanging,ruled]{algorithm2e}
\NoCaptionOfAlgo
\usepackage{hyperref}
% Patch of algorithm2e according to https://tex.stackexchange.com/questions/113403/how-to-use-nameref-with-algorithm2e
\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}
\section{ex}\label{sec:ex}
\nameref{abc} - Should read 'HowToWriteAlgorithms' without the input in parenthesis\\
\begin{algorithm}[H]
\caption[HowToWriteAlgorithms]{HowToWriteAlgorithms(X,Y,Z)
\label{abc}
}
%\addtocounter{algorithm}{-1}
%\TitleOfAlgo{HowToWriteAlgorithms(X,Y,Z)}
\KwIn{x}
\While{not at end of this document}{Try this}
\end{algorithm}
A reference to algorithm \nameref{abc}
\listofalgorithms
\end{document}