LaTeX has three length variables to control (i) the distance between two adjacent floating objects (such as figure, table, or algorithm objects), (ii) the distance between a float at the top (bottom) of a page and the text below (above) it, and (iii) the distance between an in-text float and the text above and below it; they are called \floatsep
, \textfloatsep
, and \intextsep
, respectively. (LaTeX also has three more such variables to control the spacing above and/or below floats on floats-only pages; these are \@fptop
, \@fpbot
, and \@fpsep
, respectively.)
To completely suppress the in-text separation of a float (not recommended, by the way!!), you'd type (in the preamble)
\setlength{\intextsep}{0pt}
i.e., you'd set \intextsep
to a fixed length of 0 points. A better solution, if you're pressed for space (pun intended), would be to set
\setlength{\intextsep}{1\baselineskip}
Here's a MWE that uses the algorithm2e package:
\documentclass{article}
\usepackage{algorithm2e}
\newcommand{\lipsone}{Lorem ipsum dolor sit amet, consectetuer
adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,
adipiscing vitae, felis.}
\newcommand{\lipstwo}{Donec vehicula augue eu neque. Pellentesque
habitant morbi tristique senectus et netus et malesuada fames ac
turpis egestas.}
\begin{document}
\subsubsection*{With default setting of \texttt{\textbackslash intextsep}}
\lipsone
\begin{algorithm}
\caption{A random example}
\SetAlgoLined
\KwData{Some input}[h]
\KwResult{Some output}
initialization\;
\While{not at end of this document}{read}
{go back to beginning\;}
\end{algorithm}
\lipstwo
\subsubsection*{After setting \texttt{\textbackslash intextsep} to 0pt}
\setlength{\intextsep}{0pt}
\lipsone
\begin{algorithm}
\caption{Another random example}
\SetAlgoLined
\KwData{Some input}
\KwResult{Some output}
initialization\;
\While{not at end of this document}{read}
{go back to beginning\;}
\end{algorithm}
\lipstwo
\end{document}
Here's an option that provides feasible output. I've removed the physical numbering in the caption:
\documentclass[10pt,twocolumn]{article}
\usepackage{algorithm,algpseudocode}% http://ctan.org/pkg/{algorithms,algorithmx}
\algnewcommand{\Inputs}[1]{%
\State \textbf{Inputs:}
\Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}
\algnewcommand{\Initialize}[1]{%
\State \textbf{Initialize:}
\Statex \hspace*{\algorithmicindent}\parbox[t]{.8\linewidth}{\raggedright #1}
}
\begin{document}
\begin{algorithm}
\caption{Boosted $K$ Nearest Neighbour}
\begin{algorithmic}[1]
\Inputs{$S={s_i}=\left(x_i,y_i\right)$}
\Initialize{\strut$w_i^0 \gets 0$, $i=1,\ldots,n$ \\ $S_0 \gets S$}
\For{t = 1 to T}
\State $S_t \gets S_{t-1}$
\For{$s_q \in S_t$}
\State $N_q \gets$ k nearest neighbors
\State of $s_q$ using $D(s_q,s_i)$
\State label($s_q$)$=argmax\sum_{s_i \in N_q}D(s_q,s_i)$;
\If{label($s_q$)$\ne y_q$}
\For{$s_i \in N_i$}
\If{$y_i \ne y_q$}
\State $w_{i}^{t} \gets w_{i}^{t} - \lambda/d(x_q,x_i)$;
\Else
\State $w_{i}^{t} \gets w_{i}^{t} + \lambda/d(x_q,x_i)$;
\EndIf
\EndFor
\EndIf
\EndFor
\If{label($s_q$)$=y_q \forall_{s_q}$}
\State break
\EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
A newly-defined \Inputs
and (equivalent) \Initialize
takes one argument that sets its contents in a \Statex
with an indent of \algorithmicindent
- the default - to align with the other algorithm elements. The contents is stored in a t
op-aligned \parbox
that fits across 80% of the line. I'm sure you won't need more, but in case you do, that can also be fixed to not cause overfull boxes.
Best Answer
The package
algorithm
accepts three options at loading time, in regards of the appearance of the algorithm:ruled
,boxed
andplain
. The default isruled
but I think you are looking for theplain
style.Example in
plain
style:Output:
Substituting
plain
withboxed
:And finally the default style (
ruled
):