15

Is there any way to add a blank line in algorithm2e?

I have the following document:

\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\begin{document}
\begin{algorithm}
\SetAlgoLined
i = 1 + j\\
% I would like to add a \linebreak here
j = 1 + i\\
\end{algorithm}
\end{document}

This outputs:

enter image description here

I would like to have:

enter image description here

If I add \\ or \linebreak between i = 1 + j\\ and j = 1 + i\\ I get the following error message:

enter image description here

2
  • 2
    As a workaround you can add \mbox{}\\ or may be easier to type (but less clean) ~\\.
    – Manuel
    Jul 20, 2014 at 22:53
  • @Manuel Indeed \mbox{}\\ and ~\\ do the trick (I had tried ~`, forgot to add another ` :/). Thanks! Jul 20, 2014 at 22:54

2 Answers 2

8

algorithm2e's line-ending macro is \;. From the algorithm2e documentation:

9.1 global code typesetting commands

\; marks the end of a line. Don’t forget it! By default, it prints a ‘;’. You can change this with \DontPrintSemicolon.

enter image description here

\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\DontPrintSemicolon 
\begin{document}
\begin{algorithm}
  $i = 1 + j$\;
  \;
  $j = 1 + i$
\end{algorithm}
\end{document}
31

Instead of the \DontPrintSemicolon trick you should use \BlankLine.

Sample output

\documentclass{article}
\usepackage[linesnumbered]{algorithm2e}% http://ctan.org/pkg/algorithm2e
\DontPrintSemicolon 
\begin{document}
\begin{algorithm}
  $i = 1 + j$\;
  \BlankLine
  $j = 1 + i$
\end{algorithm}
\end{document}
6
  • 1
    Welcome to TeX.SX! Could you add a screen shot in order to 'prove' that your solution works?
    – user31729
    Feb 22, 2015 at 15:49
  • Running you code I get the image added to you post - thus this does not seem to answer the question posed. Feb 22, 2015 at 15:57
  • 5
    Upvoting for \BlankLine since it plays nice with line numbering.
    – qweruiop
    Jun 1, 2016 at 2:14
  • 1
    This definitely looks better.
    – lonesome
    Aug 6, 2017 at 3:07
  • 2
    Is there some way to edit the size of \BlankLine? Could not find anything about it in the CTAN docs. I'm using now multiple \BlankLines to achieve the desired result—does not look nice, though.
    – Patrick
    May 25, 2020 at 17:27

You must log in to answer this question.

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