[Tex/LaTex] Forcing a newline in the algpseudocode package

algorithmicxline-breaking

I have written the following pseudocode:

\begin{algorithm}
\caption{Check if any player has won.}

\begin{algorithmic}[1]
\Procedure{isWin}{$Game$}
\ForAll{$seq \in \Call{getWinSeqs}{Game}$}
  \For{$i \gets 0, length(seq) - 1$}
    \If{$\not seq[i] = seq[i + 1]$}
      break
    \EndIf
  \EndFor
  \If{$seq[i + 1] = `X'$}
    \Return `X'
  \ElsIf{$seq[i + 1] = `O'$}
    \Return `O'
  \EndIf
\EndFor
\Return null
\EndProcedure
\end{algorithmic}
\end{algorithm}

which renders to the following image:

rendering of algorithm

As seen in the image, the final return is placed directly after end for. How can I force a newline after return?

Best Answer

You're using algpseudocode rather than algorithmicx; redefine \algorithmicreturn to issue \State

I've also made some refinements to your code. Note, in particular, that the negation symbol is \lnot, while \not is a different command.

\documentclass{article}
\usepackage{algpseudocode}

\newcommand{\var}[1]{\mathit{#1}}
\newcommand{\qt}[1]{\mbox{`#1'}}
\algrenewcommand{\algorithmicreturn}{\State \textbf{return}}

\begin{document}
\begin{algorithmic}[1]            
\Procedure{isWin}{$\var{Game}$}
\ForAll{$\var{seq} \in \Call{getWinSeqs}{\var{Game}}$}
  \For{$i \gets 0, \mathrm{length}(\var{seq}) - 1$}
    \If{$\lnot \var{seq}[i] = \var{seq}[i + 1]$}
      break
    \EndIf
  \EndFor
  \If{$\var{seq}[i + 1] = \qt{X}$}
    \Return $\qt{X}$
  \ElsIf{$seq[i + 1] = \qt{O}$}
    \Return $\qt{O}$
  \EndIf
\EndFor
\Return null
\EndProcedure
\end{algorithmic}
\end{document}

enter image description here