2

Throughout the document, I use \renewcommand{\baselinestretch}{1.5} for the entire document. Now I want to set only the algorithm environment in \usepackage{algorithm2e} to the normal spacing, what should I do?

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}
%\renewcommand{\baselinestretch}{1.5}
\RestyleAlgo{ruled}
I want my main text to be baselinestretch 1.5. by using renewcommand{baselinestretch}{1.5}
\begin{algorithm}
\caption{Triple Rapid}\label{alg:three}
%\KwData{Write here the required data}
%\KwResult{Write here the expected result}
 Initialization: A list of distinct integers $x = \{x_1,x_2,\dots,x_n\}$\;
  \eIf{$n\leq 1$}{
   Return $x$
   }{
   Set Pivot $p = x$\;
   Extract list of elements $x'$ less than $p$\;
   Extract list of elements $x''$ greater than $p$\;
   Return (\text{TripleRapid}($x'$), $p$, \text{TripleRapid}($x''$))\;
  }
\end{algorithm}

\end{document}

If I use \renewcommand{\baselinestretch}{1.5}, the algorithm will gives something like this

enter image description here

However, I want something that looks like this

enter image description here

3
  • Welcome! Can you please show a small example of code to play with?
    – egreg
    Nov 10, 2022 at 23:47
  • @egreg I have editted the question, hope it is better!
    – Tab1e
    Nov 11, 2022 at 11:23
  • 1
    Not really. A small (but compilable) piece of code should be here
    – egreg
    Nov 11, 2022 at 11:39

1 Answer 1

1

Rather than changing \baselinestretch after \begin{document} you should use \linespread{1.5} before it.

Anyway, I recommend using setspace, if you really need enlarged interline space and its \onehalfspacing command.

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

\usepackage{lipsum} % for mock text

\RestyleAlgo{ruled}

\AtBeginEnvironment{algorithm}{%
  \singlespacing
  \renewcommand{\arraystretch}{1}%
}

\onehalfspacing
\renewcommand{\arraystretch}{0.8} % because \baselinestretch is 1.25

\begin{document}

\lipsum[3]

\begin{algorithm}
\caption{Triple Rapid}\label{alg:three}
%\KwData{Write here the required data}
%\KwResult{Write here the expected result}
 Initialization: A list of distinct integers $x = \{x_1,x_2,\dots,x_n\}$\;
  \eIf{$n\leq 1$}{
   Return $x$
   }{
   Set Pivot $p = x$\;
   Extract list of elements $x'$ less than $p$\;
   Extract list of elements $x''$ greater than $p$\;
   Return $(\mathrm{TripleRapid}(x'), p, \mathrm{TripleRapid}(x''))$\;
  }
\end{algorithm}

\lipsum[3]

\end{document}

The redefinitions of \arraystretch ensure that matrices will not be ruined by the enlarged \baselinestretch.

If you really want 1.5, say \setstretch{1.5} and instead of 0.8 use 0.66667.

enter image description here

Please, note the changes I made to the second “Return” line: you should have a single math formula.

1
  • Thank you very much! This is exactly what I needed!
    – Tab1e
    Nov 11, 2022 at 14:14

You must log in to answer this question.

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