1

I have the following code:

\documentclass{scrbook}


\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}

 AAAA

 BBBB

 \begin{algorithm}
 \vspace*{-1cm}
 \caption{My Caption\ldots}
 My Code
 \end{algorithm}

\end{document} 

which does not move the "My Code" part with the caption "My Caption" upwards - but it works for figure environement - any suggestions how this can be done for the algorithms?

NOTE: I know a way of specifying the \vpsace* above the \begin{algorithm} but that won't work if you have a text arround it - it will mess up the text, and not move the algorithm.

2
  • To squeeze in one page :) No, your suggestion will not work if you have a lot of text before and after your algorithm - the vpsace*{-5mm} will affect the text only :/
    – schmi
    Commented Feb 2, 2015 at 15:23
  • Works for the example I wrote here, but does not work in reality - it just moves upwards the text :/ Isn't there a similar way as with figures/images?
    – schmi
    Commented Feb 2, 2015 at 15:31

1 Answer 1

2

You are talking about algorithms that have to stay in place.

So you have two possibilities.

  1. Use H as a floating specifier. In this way all the spacing around the algorithm environment is removed.

    \documentclass{scrbook}
    
    \usepackage[linesnumbered,ruled]{algorithm2e}
    
    \begin{document}
    
     AAAA
    
     BBBB
    
     \begin{algorithm}[H]
     \caption{My Caption\ldots}
     My Code
     \end{algorithm}
    
     CCCC
    
    \end{document} 
    

    enter image description here

  2. Use h as a floating specifier and redefine locally the length \intextsep (which represents the distance between floats and text when using h).

    \documentclass{scrbook}
    
    \usepackage[linesnumbered,ruled]{algorithm2e}
    
    \begin{document}
    
     AAAA
    
     BBBB
    
    {\setlength{\intextsep}{0pt}
     \begin{algorithm}[h]
     \caption{My Caption\ldots}
     My Code
     \end{algorithm}
    }
    
     CCCC
    
    \end{document} 
    

    enter image description here

You must log in to answer this question.

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