[Tex/LaTex] Algorithmicx package giving “Some blocks are not closed” among other errors

algorithmicxerrors

I am trying to write a simple pseudocode for min-conflicts to solve the n-queens problem. The following MWE shows all the used packages but fails to reproduce the errors that I am getting… Somehow.

    \documentclass{article}
    \usepackage{amsmath}
    \usepackage{graphics}
    \usepackage[noend]{algpseudocode}
        \begin{document}
        \begin{algorithmic}[1]
            \Procedure{Min-Conflicts}{$n$}
            \State {board $\gets$ queens on unique row and column} \Comment{\textit{The initial state that approximates a solution. Only diagonal attacks are conflicts.}}
                \For {$t$ in max\_steps} \Comment{\textit{If constant, algorithm runs in $\mathcal{O}(1)$ time}}
                    \For {a random row in board} \Comment{\textit{A random subspace.}}
                    \State queen $\gets$ position in row with least conflicts \Comment{\textit{Greedily selects best state for subspace.}}
                    \EndFor
                \EndFor
            \If {\textsc{Is\_Solution}(board)}:
                \State \Return board
            \Else:
                \State \Return \textsc{Min-Conflicts}($n$)
            \EndProcedure
        \end{algorithmic}
        \end{document}

I would like to show an example of the output but I accidentally ESC'd my pdf preview (which would update before ESCing the preview). I am using a full MIKTEX install and TEXSTUDIO for my IDE.

These are my following errors:

Missing \endcsname inserted. \Procedure (first line with \Procedure on it)

Package algorithmicx Error: Some blocks are not closed!!!. \end{algorithmic} (Next line)

Missing \endcsname inserted. \Procedure (5 lines later)

Extra \endcsname. \Procedure (same line)

Missing number, treated as zero. \EndProcedure (11 lines later)

Package algorithmicx Error: Some blocks are not closed!!!. \end{algorithmic} (next line)

I have NO IDEA why these are coming up. I have even just modified existing algorithms that I have written other places with no issues.

Best Answer

Your final \If...\Else condition does not have an accompanying \EndIf. Despite using the noend option, you still need an \EndIf in your code:

enter image description here

\documentclass{article}

\usepackage[noend]{algpseudocode}

\begin{document}

\begin{algorithmic}[1]
  \Procedure{Min-Conflicts}{$n$}
    \State {board $\gets$ queens on unique row and column} \Comment{\textit{The initial state that approximates a solution. Only diagonal attacks are conflicts.}}
      \For {$t$ in max\_steps} \Comment{\textit{If constant, algorithm runs in $\mathcal{O}(1)$ time}}
        \For {a random row in board} \Comment{\textit{A random subspace.}}
          \State queen $\gets$ position in row with least conflicts \Comment{\textit{Greedily selects best state for subspace.}}
        \EndFor
      \EndFor
    \If {\textsc{Is\_Solution}(board)}:
      \State \Return board
    \Else:
      \State \Return \textsc{Min-Conflicts}($n$)
    \EndIf
  \EndProcedure
\end{algorithmic}

\end{document}
Related Question