Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using the algorithm environment to create a simple list of pseudo-code, but I don't like it that the first line is so close to the rule. Therefore, I would like to insert a single white line like \hfill into every algorithm I'm using. Is it easy to do that without typing \hfill in every algorithm block?

The code I have produced with \hfill looks like this:

% the important used packages
\usepackage{mdwlist}
\usepackage{algorithm}

\begin{algorithm}[H]
\hfill % I don't like this here, I would stylize this automatically in some way.
\begin{enumerate*}
    \item Do bla.
    \item Do bla.
    \item Do bla.
    \item Do bla.
\end{enumerate*}
\caption{My algo. \label{alg:1}}
\end{algorithm}
share|improve this question

3 Answers 3

up vote 4 down vote accepted

algorithm.sty uses the ruled style defined by the float package. If you only use this style for your algorithms, then you can simply redefine the command \fs@ruled which controls the ruled style; here's a simple example of such a redefinition, producing the desired vertical spacing:

\documentclass{article}
\usepackage{mdwlist}
\usepackage{algorithm}

\makeatletter
\renewcommand\fs@ruled{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@ruled
  \def\@fs@pre{\hrule height.8pt depth0pt \kern2pt}%
  \def\@fs@post{\kern2pt\hrule\relax}%
  \def\@fs@mid{\kern2pt\hrule\kern\baselineskip}% original: \kern2pt\hrule\kern2pt
  \let\@fs@iftopcapt\iftrue}
\makeatother

\begin{document}

\begin{algorithm}
\begin{enumerate*}
    \item Do bla.
    \item Do bla.
    \item Do bla.
    \item Do bla.
\end{enumerate*}
\caption{My algo. \label{alg:1}}
\end{algorithm}

\end{document}

enter image description here

share|improve this answer
    
Wow, that is something I will have to read about. Do you also have a reference on doing more of these style options or is it hacky to do this kind of redefinitions? –  Marnix Oct 21 '11 at 19:19
    
@Marnix: you can find the definition of the predefined float styles (including ruled) in the file float.sty. –  Gonzalo Medina Oct 21 '11 at 19:42

Don't see an official way, but adding this to the preamble works:

\let\OldAlgorithm\algorithm
\renewcommand{\algorithm}[1][]{\OldAlgorithm[#1]\hfill}
share|improve this answer
    
And there also isn't any way of starting the enumerate* with extra margin at the top? That would lead to the same result for me. I just want the extra margin of one line before starting the actual enumerate. –  Marnix Oct 21 '11 at 18:48
    
That should be a separate question, but check the enumitem package. And that solution would effect ALL enumerate, even outside of algorithm so may not be a good solution for this particular problem. –  Peter Grill Oct 21 '11 at 18:51
    
Yes I would only like to let it affect my algorithm, so that is probably not a good solution. I was thinking of some kind of nested query like css does. But maybe that is a wrong expectation. Thanks for the help. –  Marnix Oct 21 '11 at 18:55

There are alternatives to using enumerate within an algorithm environment for producing itemized/enumerated code segments. You could consider using algorithm2e or the functionality provided by algorithmx. Here is a short example that duplicates your current algorithm using the latter package:

Algorithm

\documentclass{article}
\usepackage{algorithm}% http://ctan.org/pkg/algorithm
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\BeforeBeginEnvironment{algorithmic}{\kern 0.5\baselineskip}% Space above algorithmic environment
\AfterEndEnvironment{algorithmic}{\kern 0.5\baselineskip}% Space below algorithmic environment
\begin{document}
\begin{algorithm}
  \begin{algorithmic}[1]
    \State Do bla.
    \State Do bla.
    \State Do bla.
    \State Do bla.
  \end{algorithmic}
\caption{My algo. \label{alg:1}}
\end{algorithm}
\end{document}

The etoolbox package was used to automate the procedure of adding vertical whitespace above and below the algorithmic environment - adding code \BeforeBeginEnvironment as well as \AfterEndEnvironment. If you would rather have regular-sized font for the line numbers and don't enjoy the : separator, using

\algrenewcommand\alglinenumber[1]{\normalsize #1.}%

would produce the desired result:

Algorithm with different line numbers

share|improve this answer
    
Funny that there are so many correct answers. I still don't know which one to choose as correct. They all seem to work. –  Marnix Oct 22 '11 at 16:07
    
It depends on many things, including (i) the other components of your document, (ii) how much code you want to add, (iii) how flexible the code is if you want small alterations and, of course, (iv) your preference. With regards to (i): Some packages are not compatible with others, making some options void. However, since this was not supplied in the MWE, more options may be provided to suit the OP's need. Although some balance is necessary, for me, having as little additional code as possible (or add to your own .sty), with as much flexibility leaves the code clean and easy-to-understand. –  Werner Oct 22 '11 at 16:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.