# Forcing a newline in the algpseudocode package

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:

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

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}


• mirrors.ibiblio.org/CTAN/macros/latex/contrib/algorithmicx/… - as per the documentation, the command set algpsuedocode is defined in there. Thanks for the improvements made; could you also explain what \var and \qt is for? Oct 29, 2014 at 15:47
• @wei2912 A multiletter variable like seq shouldn't be typed just so in math mode, because the spacing would be wrong, so it's better to use \mathit around it and even better using a macro resolving to \mathit. I used \qt` for denoting a quoted string. Oct 29, 2014 at 15:51