# Ruled Algorithms in columns/minipages

I'm using Benoit Guillon's algo styles for Lyx (at http://bgu.perso.libertysurf.fr/) in 2.0 to get nice algorithms ruled with line numbers. This is just a wrapper for the algorithm and algorithmic packages in LaTex.

What I'd like to do is put two algorithms side-by-side in my single-column paper. Normally, I'd turn to floats, minipages, and sub-floats as one would for figures. However, (despite the fact that no sub-float is defined for algorithm) the ruled style of the encompassing float creates a rule across all subfloats/minipages and only one algorithm caption from inside is listed in the ruled title area. The latex export is at the bottom. A tight cropped clipping shows the output when lines 93-138 are removed:

Notice that the algorithm title is the right side alg--the left algorithm title and number is lost. Also, there's no gutter in the rule.

What I really want is just to make separate minipages, then set my algorithm floats in there, but I understand this is not supported in Lyx (according to the embeddable objects manual.) If I try, I get the latex error:

Underfull \hbox (badness 10000) in paragraph at lines 122--123
[]\T1/cmr/bx/n/10 Parameters:
[]

[1

{C:/ProgramData/MiKTeX/2.9/pdftex/config/pdftex.map}]

! LaTeX Error: Float(s) lost.


(The "underfull hbox" message shows up four times--lines 61, 80, 101, 122.)

So, I guess I'm left with four questions, in order of preference for an answer:

• Is there another trick to isolating the extent of a float and putting two floats side-by-side?

• Is there a slick way of defining a sub-float layout for the document that would create two rules and apply a local caption to each? I have no experience with sub-float definition.

• Is there a slick way to define the commands from Guillon's "article (algo)" class in a local layout? I'd be willing to do that, or even work up a candidate layout, though I'm a newbie at Lyx layouts/styles.

• If I'm forced to ERT, how would folks set this in plain LaTex?

Thanks.

%% LyX 2.0.4 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{color}
\usepackage{float}
\usepackage{amsmath}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\floatstyle{ruled}
\newfloat{algorithm}{tbp}{loa}
\providecommand{\algorithmname}{Algorithm}
\floatname{algorithm}{\protect\algorithmname}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Textclass specific LaTeX commands.
\floatstyle{ruled}
\newfloat{algorithm}{tbp}{loa}
\floatname{algorithm}{Algorithm}
\usepackage[noend]{algorithmic}
\newcommand{\forbody}[1]{ #1 \ENDFOR}
\newcommand{\ifbody}[1]{ #1  \ENDIF}
\newcommand{\whilebody}[1]{ #1  \ENDWHILE}
\renewcommand{\algorithmicprint}{\textbf{draw}}
\usepackage{algolyx}

\makeatother

\usepackage{babel}
\begin{document}

\section{Intro}

blah

\begin{algorithm}
\textbf{Parameters:} $a,b$
\begin{algor}[1]
\item [{while}] $b\neq0$

\begin{algor}[1]
\item [{.}] $t\gets b$
\item [{.}] $b\gets a\;\mod t$
\item [{.}] $a\gets t$
\end{algor}
\item [{endwhile}]~
\item [{.}] \textbf{return} $a$
\end{algor}
\caption{\textsc{\label{alg:Gcd1}Gcd1}}
\end{algorithm}

One float, two minipages inside (Gcd2 and 3):

\begin{algorithm}
\begin{minipage}[t]{0.48\columnwidth}%
\textbf{Parameters:} $a,b$
\begin{algor}[1]
\item [{while}] $b\neq0$

\begin{algor}[1]
\item [{.}] $t\gets b$
\item [{.}] $b\gets a\;\mod t$
\item [{.}] $a\gets t$
\end{algor}
\item [{endwhile}]~
\item [{.}] \textbf{return} $a$
\end{algor}
\caption{\label{alg:Gcd2}\textsc{GcdLeft}}
%
\end{minipage}\hfill{}%
\begin{minipage}[t]{0.48\columnwidth}%
\textbf{Parameters:} $a,b$
\begin{algor}[1]
\item [{while}] $b\neq0$

\begin{algor}[1]
\item [{.}] $t\gets b$
\item [{.}] $b\gets a\;\mod t$
\item [{.}] $a\gets t$
\end{algor}
\item [{endwhile}]~
\item [{.}] \textbf{return} $a$
\end{algor}
\caption{\label{alg:Gcd3}\textsc{GcdRight}}
%
\end{minipage}
\end{algorithm}

Just in minipages on a line

\begin{center}
\begin{minipage}[t]{0.48\columnwidth}%
\textbf{}
\begin{algorithm}
\textbf{Parameters:} $a,b$
\begin{algor}[1]
\item [{while}] $b\neq0$

\begin{algor}[1]
\item [{.}] $t\gets b$
\item [{.}] $b\gets a\;\mod t$
\item [{.}] $a\gets t$
\end{algor}
\item [{endwhile}]~
\item [{.}] \textbf{return} $a$
\end{algor}
\textbf{\caption{\textbf{\label{alg:Gcd4}}\textsc{GcdLeftPage}}
}

\end{algorithm}
%
\end{minipage}\hfill{}%
\begin{minipage}[t]{0.48\columnwidth}%
\textbf{}
\begin{algorithm}
\textbf{Parameters:} $a,b$
\begin{algor}[1]
\item [{while}] $b\neq0$

\begin{algor}[1]
\item [{.}] $t\gets b$
\item [{.}] $b\gets a\;\mod t$
\item [{.}] $a\gets t$
\end{algor}
\item [{endwhile}]~
\item [{.}] \textbf{return} $a$
\end{algor}
\textbf{\caption{\textbf{\label{alg:Gcd5}}\textsc{GcdRightPage}}
}

\end{algorithm}
%
\end{minipage}
\par\end{center}

end
\end{document}

-

Benoit Guillon's Algorithm-Style Package for LyX was written in the year 2001. It makes the LaTeX package algorithmic available to LyX in an interesting form. For writing algorithms, the potential of nested lists is used. (Because at that time LyX modules were not available there is the requirement of using a certain document class. But this could be revised...)

Now, how to print two algorithms side-by-side in a LaTeX document is shown in Diego’s CS research blog. This can be transformed into "pure" LyX almost completely.

1. For each algorithm-float do

• set its "Placement Options" (e.g., by right click) to "Here definitely" to stop it floating -- this is important otherwise an error occurs,

• enclose the float by a minipage whose width is a fraction of the column width (e.g., 45% of column width);

2. Remove all white spaces (including new lines) between the minipages and "Insert > Formatting > Horizontal Space..." "Horizontal Fill";

3. Add TeX code/ERT \begin{figure*} before the first minipage and \end{figure*} after the last minipage to make the block of algorithms floating again.

Here is a picture of a LyX document followed by the PDF output.

-
You're a genius. Can't thank you enough. This worked great, with the addition of \begin{figure*}[th!], otherwise the figure would get thrown to the end (after bib.) – jspencer Mar 5 '13 at 20:20