无法将标题放在算法的伪代码上

无法将标题放在算法的伪代码上

我使用以下算法编写了伪代码\userpackage{算法}当我想添加标题时,出现错误。这是代码:

\begin{center}
\begin{algorithmic}
\STATE $N\gets sizeof(\textit{input data})$\\
\textbf{Function} Twiddle(W, N, stuff)
\STATE $        W.r \gets cos(stuff.2.\Pi/N)$
\STATE $        W.i \gets -sin(stuff.2.\Pi/N)$\\
...
\IF{$N_{2}!=1$}
\STATE \textbf{Call} $radix-4(data[N_{2}.k1], N_{2})$
\ENDIF
\label{algo1}
\end{algorithmic}
\end{center}

有人可以帮我解决这个问题吗?有没有其他更好的替代品可以让这个包看起来更好看?在此处输入图片描述

答案1

algorithmic环境不是浮动环境,您必须使用algorithm包装器环境。请注意,此环境与 配合使用caption,因此您可以轻松地为此环境自定义标题。以下是与默认环境不同的示例:

\documentclass{article}
\usepackage{fourier, erewhon}
 \usepackage{caption}
\usepackage{algorithm, algorithmic}%

\begin{document}

\begin{algorithm}\captionsetup{labelfont={sc,bf}, labelsep=newline}
  \caption{Tweedledee and Tweedledum}
  \begin{algorithmic}
    \STATE $N\gets sizeof(\textit{input data})$\\
    \textbf{Function} Twiddle(W, N, stuff)
    \STATE $ W.r \gets cos(stuff.2.\Pi/N)$
    \STATE $ W.i \gets -sin(stuff.2.\Pi/N)$\\
    ...
    \IF{$N_{2}!=1$}
    \STATE \textbf{Call} $radix-4(data[N_{2}.k1], N_{2})$
    \ENDIF
    \label{algo1}
  \end{algorithmic}
\end{algorithm}

\end{document} 

在此处输入图片描述

答案2

应该通过\renewcommand\thealgorithm{}\begin{algorithm}

\documentclass{article}
\usepackage{algorithmic,algorithm}

\begin{document}

\begin{algorithm}
\renewcommand\thealgorithm{}
\caption{A numberless algorithm}
\begin{algorithmic}
\STATE $N\gets sizeof(\textit{input data})$\\
\textbf{Function} Twiddle(W, N, stuff)
\STATE $        W.r \gets cos(stuff.2.\Pi/N)$
\STATE $        W.i \gets -sin(stuff.2.\Pi/N)$\\
...
\IF{$N_{2}!=1$}
\STATE \textbf{Call} $radix-4(data[N_{2}.k1], N_{2})$
\ENDIF
\end{algorithmic}
\addtocounter{algorithm}{-1}
\end{algorithm}

\end{document}

在此处输入图片描述

答案3

您可以使用algorithm-package,它为算法定义一个浮点类型。

提示:如果您正在使用-environment center,那么您很可能做错了什么。

输出

在此处输入图片描述

代码

% algorithmic - Cannot put Caption on a pseudocode of algorithm - TeX - LaTeX Stack Exchange
% Url: http://tex.stackexchange.com/questions/328108/cannot-put-caption-on-a-pseudocode-of-algorithm
% Date: mandag 5. september 2016 16.46.04


\documentclass{article}
\usepackage{algorithmic}
\usepackage{algorithm}

\begin{document}


 \begin{algorithm}
\begin{algorithmic}
  \STATE $N\gets sizeof(\textit{input data})$\\
  \textbf{Function} Twiddle(W, N, stuff)
  \STATE $        W.r \gets cos(stuff.2.\Pi/N)$
  \STATE $        W.i \gets -sin(stuff.2.\Pi/N)$\\
  ...
  \IF{$N_{2}!=1$}
  \STATE \textbf{Call} $radix-4(data[N_{2}.k1], N_{2})$
  \ENDIF
  \label{algo1}
\end{algorithmic}
\caption{Some awesome caption}
 \end{algorithm}

\end{document}

相关内容