我尝试在算法的底线下方添加一些用于说明算法的文本。
\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[hang,flushmargin]{footmisc}
\usepackage{caption}
\begin{document}
\begin{algorithm}
\caption{How to write algorithms}\label{algorithm1}
\KwIn{this text}
\KwOut{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\;
}{
$y_0\leftarrow y$\;
}
}
\end{algorithm}
\end{document}
我想在底线下方添加一些文字,如下所示:
基于如何为 algorithmicx 的评论创建脚注,应该使用minipage
。我更新了我的代码如下:
\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[hang,flushmargin]{footmisc}
\usepackage{caption}
\begin{document}
\noindent\begin{minipage}{\textwidth}
\begin{algorithm}
\caption{How to write algorithms}\label{algorithm1}\footnotetext{$y_0$ denotes the initial value.}
\KwIn{this text}
\KwOut{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\;
}{
$y_0\leftarrow y$\;
}
}
\end{algorithm}
\renewcommand\footnoterule{}
\end{minipage}
\end{document}
但会出现错误。
任何建议都将不胜感激。
答案1
建设algorithm2e
的浮动很复杂,因为它允许指定一个静止的H
浮动位置,而无需使用float
。此外,它还管理在顶部或底部放置标题以适应不同的风格。因此,为了满足您对此浮动内脚注的要求,您需要将其拆分到不会影响构造中其他组件的地方。这里有一个补丁,它提供\algorithmfootnote[<style>]{<stuff>}
(<style>
默认为\footnotesize
):
\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amssymb,amsmath,caption}
\usepackage[hang,flushmargin]{footmisc}
\usepackage{lipsum}
\makeatletter
\newcommand{\algorithmfootnote}[2][\footnotesize]{%
\let\old@algocf@finish\@algocf@finish% Store algorithm finish macro
\def\@algocf@finish{\old@algocf@finish% Update finish macro to insert "footnote"
\leavevmode\rlap{\begin{minipage}{\linewidth}
#1#2
\end{minipage}}%
}%
}
\makeatother
\begin{document}
\lipsum[1-3]
\begin{algorithm}[tb]
\caption{How to write algorithms}\label{algorithm1}
\algorithmfootnote{$y_0$ denotes the initial value.}
\KwIn{this text}
\KwOut{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\;
}{
$y_0 \leftarrow y$\;
}
}
\end{algorithm}
\end{document}
答案2
LaTeX Error: Not in outer par mode
由于 内部浮动算法而导致的结果minipage
。您可以使用[H]
位置说明符来摆脱这种情况。另外,我已经将其放在开始\renewcommand\footnoterule{}
之后minipage
,否则就太晚了。
\documentclass{article}
\usepackage[ruled]{algorithm2e}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage[hang,flushmargin]{footmisc}
\usepackage{caption}
\begin{document}
\noindent\begin{minipage}{\textwidth}
\renewcommand\footnoterule{} %% This line should come here.
\begin{algorithm}[H] %% <-------------
\caption{How to write algorithms}\label{algorithm1}\footnotetext{$y_0$ denotes the initial value.}
\KwIn{this text}
\KwOut{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\;
}{
$y_0\leftarrow y$\;
}
}
\end{algorithm}
\end{minipage}
\end{document}