在算法中控制标题上方和下方的文本

在算法中控制标题上方和下方的文本

我有一个算法,标题在顶部。我想控制标题的垂直间距:与上方文本的距离和与下方算法的距离。据我所知,我应该使用 belowskip 和 aboveskip,但它们什么也不做(我设置了一些疯狂的间距只是为了举例说明),我猜是因为当我重新定义浮动样式时,我隐式地赋予了它一个固定的位置?谢谢帮助。

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} % set input encoding
\usepackage[english]{babel}
\usepackage{varwidth}

%% Algorithms
\usepackage{algorithm,algorithmic}
\usepackage{caption}
\usepackage{setspace}

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

% Change caption format
\captionsetup[algorithm]{
    belowskip=100pt,
    aboveskip=50pt,
    font=scriptsize,
    justification = raggedright,
    singlelinecheck = false,
    name = Algorithm
}

% Change width of horizontal lines 
\makeatletter
\newcommand{\algrule}[1][1pt]{\par\vskip.5\baselineskip\hrule height             
#1\par\vskip.5\baselineskip}
\makeatother

%% Notes to figures, tables, algorithms
\newcommand{\notes}[1]{{ \vspace{3pt} \begin{spacing}{1} \scriptsize #1 \end{spacing}}}        

\begin{document}
Some text here.

\begin{algorithm}
    \caption{Algorithm caption}
    \label{alg:algorithm-label}
    \begin{algorithmic}[1]
        \algrule
        \IF{some condition is true}
            \STATE do some processing
        \ELSIF{some other condition is true}
            \STATE do some different processing
        \ELSE
            \STATE do the default actions
        \ENDIF
        \algrule
    \end{algorithmic}
    \notes{Some notes here.}
\end{algorithm}

Some more text here.

\end{document}

答案1

最简单的解决方案是使用 newfloat 而不是 algorithm 创建算法环境(float 类型)。这将更像图形和表格。

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc} % set input encoding
\usepackage[english]{babel}
\usepackage{varwidth}

%% Algorithm
\usepackage{newfloat}
\DeclareFloatingEnvironment[placement=htp]{algorithm}
\usepackage{algorithmic}
\usepackage{caption}
\let\globalcaption=\caption
\usepackage{setspace}

% Change caption format
\captionsetup[algorithm]{
    belowskip=100pt,
    aboveskip=50pt,
    font=scriptsize,
    justification = raggedright,
    singlelinecheck = false,
    name = Algorithm
}

% Change width of horizontal lines 
\makeatletter
\newcommand{\algrule}[1][1pt]{\par\vskip.5\baselineskip\hrule height#1\vskip.5\baselineskip}
\makeatother

%% Notes to figures, tables, algorithms
\newcommand{\notes}[1]{{ \vspace{3pt} \begin{spacing}{1} \scriptsize #1 \end{spacing}}}        

\begin{document}
Some text here.
\begin{algorithm}
    \caption{Algorithm caption}
    \begin{algorithmic}[1]
        \algrule
        \IF{some condition is true}
            \STATE do some processing
        \ELSIF{some other condition is true}
            \STATE do some different processing
        \ELSE
            \STATE do the default actions
        \ENDIF
        \algrule
    \end{algorithmic}
    \notes{Some notes here.}
\end{algorithm}

Some more text here.

\end{document}

相关内容