1

I have an algorithm with caption on top. I want to control the vertical spacing of the caption: distance from text above and distance from algorithm below. As I understand I should use belowskip and aboveskip but they do nothing (I have put some crazy spacing just to exemplify), I guess because when I redefine the float style I am implicitly given it a fixed position? Thanks for help.

\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 Answer 1

2

The simplest solution is to create the algorithm environment (float type) using newfloat instead of algorithm. This will behave more like figure and table.

\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}
3
  • It turns out that the problem is coming from the float package, which is automatically loaded by algorithm. It places the caption either at the very top or very bottom of the float (no spacing). There is no way to turn off this feature. Oct 7, 2017 at 13:41
  • First solution proposed: I am writing a class file (as a journal template), and I really would like to move all those formatting instructions away from the document, into the class file. I'm fine with keeping \usebox\mybox in the document, but I would really like to move those two other instructions into the class file: \newsavebox{\mybox} \setbox\mybox=\vbox{\captionof{algorithm}{Algorithm caption}}caption}} Unfortunately, moving them away from the main document causes an error. Second solution: Works just fine! Oct 7, 2017 at 21:38
  • I am deleting the first solution. It was more of an experiment than a serious proposal. Oct 8, 2017 at 0:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .