算法标题应位于顶部并向左对齐

算法标题应位于顶部并向左对齐

我希望我的算法的标题位于顶部并向左对齐:

\documentclass{article}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}
\begin{document}

\begin{algorithm}
\caption{Sketch of our Compression Algorithm}
\label{fig:GENIDEA}
\begin{algorithmic}[1]
\While{Input contains characters}
\State Find matching reference block $B$ for current input position
\State Perform referential compression with respect to $B$ until we cannot find ``long'' matches anymore
\EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}

答案1

\restylefloat“on top” 可以通过使用包提供float的选项来实现algorithm。(不幸的是,algorithm包没有提供plaintop这个选项。)

可以使用包\captionsetup[algorithm]{...}提供的实现“向左刷新” caption

\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}

% Change float style of algorithm from "ruled" to "plaintop"
\floatstyle{plaintop}
\restylefloat{algorithm}

% Make algorithm captions left-aligned
\usepackage{caption}
\captionsetup[algorithm]{singlelinecheck=off}

\begin{document}
\begin{algorithm}
\caption{Sketch of our Compression Algorithm}
\label{fig:GENIDEA}
\begin{algorithmic}[1]
\While{Input contains characters}
\State Find matching reference block $B$ for current input position
\State Perform referential compression with respect to $B$ until we cannot find ``long'' matches anymore
\EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}

在此处输入图片描述

答案2

我找到了我自己问题的答案。

\documentclass{article}
\usepackage{floatrow}
\usepackage[plain]{algorithm}
\floatsetup[algorithm]{capposition=top}
\usepackage{caption}
\captionsetup[algorithm]{singlelinecheck=off}
\usepackage{algpseudocode}
\begin{document}

\begin{algorithm}
\caption{Sketch of our Compression Algorithm}
\label{fig:GENIDEA}
\begin{algorithmic}[1]
\While{Input contains characters}
  \State Find matching reference block $B$ for current input position
  \State Perform referential compression with respect to $B$ until we cannot find    ``long'' matches anymore
\EndWhile
\end{algorithmic}
\end{algorithm}

\end{document}

相关内容