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 following code for reporting algorithms.

\usepackage{caption}
\floatstyle{ruled}
\newfloat{Algorithm}{thp}{lop}
\floatname{Algorithm}{Algorithm}
\newcommand{\listofAlgorithms}{\listof{Algorithm}{Algo List}}
.....


Here my explanation 1

\begin{Algorithm}[!ht]
\begin{verbatim}
here my algo 1
\end{verbatim}
\caption{Encrypt}
\end{Algorithm}

Here my explaination2

\begin{Algorithm}[!ht]
\begin{verbatim}
here my algo 2
\end{verbatim}
\caption{Encrypt2}
\end{Algorithm}

However sometimes the represented algorithm is not placed at the same place than in the code. What I mean is that it is going on the next page, leaving the text above.

I would like to keep the same code but with algorithms at the exact place. That is to say that breaking algorithm between would be possible 2 pages is possible. How can I do it?

Indeed, on my example, it is displayed as such on the pdf:

 Here my explanation1
 Here my explanation2
 here my algo1
 here my algo2

I do not want to refer the algorithm to a special place.

share|improve this question
    
See How to influence the position of float environments like figure and table in LaTeX? and [Keeping tables/figures close to where they are mentioned]. (tex.stackexchange.com/q/2275). Algorithm is just another type of float, so the above apply. –  egreg Jan 12 '13 at 17:53
    
@egreg: I tried with [!htbp] but sadly it's not working. IMO the algorithm can not be split, therefore going on the other page. –  user23710 Jan 12 '13 at 18:02
1  
No floating object can be split across pages. –  egreg Jan 12 '13 at 18:03
    
Ok then how can I set the text at the right place? Because if I type some text between 2 algorithm, the text is not displayed betwen them, but before. –  user23710 Jan 12 '13 at 18:05
    
@user23710 You have obviously not read the threads where egreg pointed you to. The algorithm is placed at the top or bottom, and it has a number so that you can refer to it by this number (using \label and \ref). –  tohecz Jan 12 '13 at 18:08
show 1 more comment

1 Answer

up vote 1 down vote accepted

Since you are using the float package, use the H placement specifier to force the floats to appear exactly "Here":

\documentclass{article}
\usepackage{caption}
\usepackage{float}

\floatstyle{ruled}
\newfloat{Algorithm}{thp}{lop}
\floatname{Algorithm}{Algorithm}
\newcommand{\listofAlgorithms}{\listof{Algorithm}{Algo List}}

\begin{document}

\begin{Algorithm}[H]
\begin{verbatim}
here my algo
\end{verbatim}
\caption{First}
\end{Algorithm}
Some text between the two algorithms
\begin{Algorithm}[H]
\begin{verbatim}
here my algo
\end{verbatim}
\caption{Second}
\end{Algorithm}

\end{document}

enter image description here

If you want this behaviour for all of the Algorithm environments, add to the preamble:

\floatplacement{Algorithm}{H}

As a side note, there's a number of existing packages designed to facilitate the typesetting of algorithms; depending on the style of algorithms you want to typeset (using pseudo-code or code), you could be interested in algorithms, algorithmicx, minted or listings.

share|improve this answer
    
Thanks, it's working fine to me. –  user23710 Jan 12 '13 at 18:41
add comment

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.