3

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?

3

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

2
  • 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? – wei2912 Oct 29 '14 at 15:47
  • 1
    @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. – egreg Oct 29 '14 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.