[Tex/LaTex] Change line spacing in algorithm2e

algorithm2eline-spacing

I'm using the algorithm2e package.

In one algorithm with several math formulae in consecutive lines I would like to increase line spacing.

I've already searched algorithm2e's documentation but could not figure out how to change line spacing. Does anybody know?

Best Answer

I haven't found any algorithm2e command to do that.

But this can be easily achieved with the help of the setspace package.

You can simply add the line (adjust 1.35 to your needs)

\setstretch{1.35}

just after the algorithm environment has begun.

Example (notice the difference between the first and the second algorithm, algorithm code taken from algorithm2e documentation):

\documentclass{article}
\usepackage{algorithm2e,setspace}

\begin{document}

\begin{algorithm}
\setstretch{1.35}
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

\bigskip

\begin{algorithm}
\SetAlgoLined
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

\end{document} 

Output

enter image description here


If you want this behavior to apply globally to algorithm environments, add the following lines to your preamble:

\usepackage{etoolbox}
\AtBeginEnvironment{algorithm}{\setstretch{1.35}}

instead of using every time \setstretch{1.35} inside the environments.

Also note that, if you don't want to use the setspace package, you can substitute \setstretch{1.35} with \linespread{1.35}\selectfont.