参考算法中的相同脚注

参考算法中的相同脚注

我正在尝试引用算法中现有的脚注,并尝试执行以下操作此链接但它不起作用。我在 a 中添加了算法,minipage以便脚注紧随其后出现。

这是 MWE:

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % use color

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{arydshln} % for cdashline

\usepackage[noend]{algpseudocode}
\usepackage{algorithm}
\usepackage{algorithmicx}

\algtext*{EndWhile}% Remove "end while" text
\algtext*{EndFor}% Remove "end for" text
\algtext*{EndIf}% Remove "end if" text

\newcommand{\inlinecomment}[1]{{\color{gray} \it-- #1}}

\usepackage[colorlinks,bookmarksopen,bookmarksnumbered,citecolor=red,urlcolor=red]{hyperref}


% increase line spacing for algorithmicx
\usepackage{etoolbox}
\makeatletter
\expandafter\patchcmd\csname\string\algorithmic\endcsname{\itemsep\z@}
{\itemsep=0.2ex plus2pt}{}{}
\makeatother

% reduce font size for algorithms
\makeatletter
\renewcommand{\ALG@beginalgorithmic}
{\tt\small}
\makeatother

% redefine pseudo-code commands
\algnewcommand\algorithmicto{\textbf{to}}
%\algrenewtext{For}[3]{\algorithmicfor\ #1 $=$ #2 \algorithmicto\ #3}
\algrenewtext{While}{\textbf{while}\ }
%\algrenewtext{EndWhile}{\textbf{end}}
\algrenewtext{Procedure}[2]{\textbf{function}\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}
%\algrenewtext{EndProcedure}{\textbf{end}}



\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}



\newcommand{\bs}[1] {\boldsymbol{#1}}

\begin{document}

\noindent\begin{minipage}{\textwidth}
\renewcommand\footnoterule{}
\begin{algorithm}[H]
  \caption{test footnote \label{alg:assemble_t3i}}
  \begin{algorithmic}[0]
    \Require{Input goes here}
    \Statex
    \Function{BAR}{$\ldots$}
      \State \inlinecomment{loop}
      \For {$i \gets 1, \ldots , n$}
         \Let{$ foo $\footnote{This is the footnote $\bs \xi$}}{$\text{bar} \left({\bs \xi_i }\right) $} \Comment{Here I have the first footnote} 
         \Let{$foo^a$}{$ \text{test} (\ldots)$} \Comment{I want to refer to the same footnote here}

      \EndFor
    \EndFunction
  \end{algorithmic}
\end{algorithm}
\end{minipage}

\end{document}

这让我 在此处输入图片描述

您看,我现在是在作弊,因为我添加了^a。有办法解决这个问题吗?

答案1

基于https://tex.stackexchange.com/a/35044/36296

\documentclass{article}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor} % use color

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{arydshln} % for cdashline

\usepackage[noend]{algpseudocode}
\usepackage{algorithm}
\usepackage{algorithmicx}

\algtext*{EndWhile}% Remove "end while" text
\algtext*{EndFor}% Remove "end for" text
\algtext*{EndIf}% Remove "end if" text

\newcommand{\inlinecomment}[1]{{\color{gray} \it-- #1}}

\usepackage[colorlinks,bookmarksopen,bookmarksnumbered,citecolor=red,urlcolor=red]{hyperref}


% increase line spacing for algorithmicx
\usepackage{etoolbox}
\makeatletter
\expandafter\patchcmd\csname\string\algorithmic\endcsname{\itemsep\z@}
{\itemsep=0.2ex plus2pt}{}{}
\makeatother

% reduce font size for algorithms
\makeatletter
\renewcommand{\ALG@beginalgorithmic}
{\tt\small}
\makeatother

% redefine pseudo-code commands
\algnewcommand\algorithmicto{\textbf{to}}
%\algrenewtext{For}[3]{\algorithmicfor\ #1 $=$ #2 \algorithmicto\ #3}
\algrenewtext{While}{\textbf{while}\ }
%\algrenewtext{EndWhile}{\textbf{end}}
\algrenewtext{Procedure}[2]{\textbf{function}\ \textproc{#1}\ifthenelse{\equal{#2}{}}{}{(#2)}}
%\algrenewtext{EndProcedure}{\textbf{end}}



\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\algorithmicrequire{\textbf{Input:}}
\algrenewcommand\algorithmicensure{\textbf{Output:}}



\newcommand{\bs}[1] {\boldsymbol{#1}}


\makeatletter
\newcommand\footnoteref[1]{\protected@xdef\@thefnmark{\ref{#1}}\@footnotemark}
\makeatother

\begin{document}

\noindent\begin{minipage}{\textwidth}
\renewcommand\footnoterule{}
\begin{algorithm}[H]
  \caption{test footnote \label{alg:assemble_t3i}}
  \begin{algorithmic}[0]
    \Require{Input goes here}
    \Statex
    \Function{BAR}{$\ldots$}
      \State \inlinecomment{loop}
      \For {$i \gets 1, \ldots , n$}
         \Let{$ foo $\footnote{\label{test}This is the footnote $\bs \xi$}}{$\text{bar} \left({\bs \xi_i }\right) $} \Comment{Here I have the first footnote} 
         \Let{$foo$\footnoteref{test}}{$ \text{test} (\ldots)$} \Comment{I want to refer to the same footnote here}

      \EndFor
    \EndFunction
  \end{algorithmic}
\end{algorithm}
\end{minipage}

\end{document}

在此处输入图片描述

答案2

脚注标记的默认位置是通过\textsuperscript。所以我们可以定义\footnoteref来做类似的事情:

在此处输入图片描述

\documentclass{article}

\usepackage[noend]{algpseudocode}
\usepackage{algorithm}

\usepackage[colorlinks]{hyperref}

\newcommand\footnoteref[1]{\textsuperscript{\ref{#1}}}

\begin{document}

\noindent\begin{minipage}{\textwidth}
\renewcommand\footnoterule{}
\begin{algorithm}[H]
  \caption{test footnote}
  \begin{algorithmic}[0]
    \State $foo$\footnote{\label{test}This is the footnote} \Comment{Here I have the first footnote} 
    \State $foo$\footnoteref{test} \Comment{I want to refer to the same footnote here}
  \end{algorithmic}
\end{algorithm}
\end{minipage}

\end{document}

相关内容