Multiline math in Algorithm environment

algorithmsmath-mode

I am wondering is there a way to have multiline math in an algorithm to prevent something like this:
enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath,amssymb}
\usepackage{amsthm}
\usepackage{mathrsfs}

\usepackage{tikz}
\usetikzlibrary{positioning,calc,arrows.meta}%arrows is deprecated

\usepackage{float}
\usepackage{framed}
\usepackage{blindtext}

\newfloat{Code}{htbp}{lop} % <-- run this *before* loading `cleveref`
\usepackage[nameinlink]{cleveref} % load this package *last*
\crefname{Code}{code}{codes} % <-- new
\Crefname{Code}{Code}{Codes} % <-- new

\algnewcommand{\IIf}[1]{\algorithmicif\ #1\ \algorithmicthen}
\algnewcommand{\IElse}{\unskip\ \algorithmicelse \ }
\algnewcommand{\IEndIf}{\unskip\ \algorithmicend\ \algorithmicif}

\pagenumbering{gobble}

\begin{document}


\begin{algorithm}[htbp]
    \caption{Simple greedy visit sequence evaluator for classical AG}\label{alg:visit-sequence-evaluator}
\begin{algorithmic}
\Procedure{\texttt{VISIT\_SEQUENCE\_EVAL}}{$P, R, \hat{\mathscr{D}}, \;p: X \rightarrow \alpha, (X, i) , \; \mathscr{V}, \;\mathit{Val} = \texttt{dict()}$}
    \State{$\mathit{visited} \gets \emptyset$}
    \State{$\pi \gets\big\{ \;\Pi_I(X) \cup \Pi_S(X) \mid v \in V(p) \wedge v \equiv X.a \big\} $} \Comment{All \emph{half-sets} for production $p$}
    \For{$\mathit{visit} \gets \mathscr{V}(p)$}
        \For{$\hat{V} \gets \mathit{visit}$} 
            \If{$\hat{V} \not \in \pi$} \Comment{Ensure $\hat{V}$ is not a \emph{half-set}}
                \State \Comment{Finding child non-terminal instance used in partition $V$}
                \State{$X' \gets \exists v \in \hat{V}, \exists p' \in P \; (v \equiv X'.a' \wedge p': X \rightarrow \alpha X' \beta  )$}
                \State{$p', j \gets \exists j \in [i +1 , \dots,  |\hat{\mathscr{D}}|], \; \exists p: X' \rightarrow \delta \in P \; ( \alpha_j \rightarrow \alpha_{j+1} \equiv \hat{\mathscr{D}}[j] \wedge   \alpha_j \equiv \hat{\alpha}(X',j)\hat{\beta} \wedge  \alpha_{j+1} \equiv  \hat{\alpha}\hat{\delta}\hat{\beta} )$} 
                \For{$r \gets (R(p) - \mathit{visited})$}
                    \If{$\texttt{LHS($r$)} \in \hat{V}$}
                        \State{$\texttt{AG\_EVAL}(r, \mathit{Val})$}
                        \State{$\mathit{visited} \gets r$}
                    \EndIf
                \EndFor
                \State{$\texttt{VISIT\_SEQUENCE\_EVAL}(P, R, \hat{\mathscr{D}}, p', \; (X', j), \; \mathscr{V}, \mathit{Val}    )$}  
                \For{$r \gets (R(p) - \mathit{visited})$}
                    \If{$\texttt{RHS($r$)} \cap \hat{V} \not = \emptyset$}
                        \State{$\texttt{AG\_EVAL}(r, \mathit{Val} )$}
                        \State{$\mathit{visited} \gets r$}
                    \EndIf
                \EndFor
            \EndIf
        
        \EndFor
    \EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Best Answer

Set the corresponding \Statement inside a \parbox of appropriate width. The width here would be \linewidth-\ALG@lm+\algorithmicindent. Additionally, I've set that \parbox using \raggedright (without it you'll get some unnecessary stretching to fit the width) with a \algorithmicindent-sized hanging indent:

enter image description here

\documentclass{article}

\usepackage[a4paper, total={6in, 8in}]{geometry}
\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath,amssymb}
\usepackage{mathrsfs}

\algnewcommand{\IIf}[1]{\algorithmicif\ #1\ \algorithmicthen}
\algnewcommand{\IElse}{\unskip\ \algorithmicelse \ }
\algnewcommand{\IEndIf}{\unskip\ \algorithmicend\ \algorithmicif}

\begin{document}

\begin{algorithm}[htbp]
  \caption{Simple greedy visit sequence evaluator for classical AG}\label{alg:visit-sequence-evaluator}

  \begin{algorithmic}
  \Procedure{\texttt{VISIT\_SEQUENCE\_EVAL}}{$P, R, \hat{\mathscr{D}}, \;p: X \rightarrow \alpha, (X, i) , \; \mathscr{V}, \;\mathit{Val} = \texttt{dict()}$}
    \State{$\mathit{visited} \gets \emptyset$}
    \State{$\pi \gets\big\{ \;\Pi_I(X) \cup \Pi_S(X) \mid v \in V(p) \wedge v \equiv X.a \big\} $} \Comment{All \emph{half-sets} for production $p$}
    \For{$\mathit{visit} \gets \mathscr{V}(p)$}
      \For{$\hat{V} \gets \mathit{visit}$} 
        \If{$\hat{V} \not \in \pi$} \Comment{Ensure $\hat{V}$ is not a \emph{half-set}}
          \State \Comment{Finding child non-terminal instance used in partition $V$}
          \State $X' \gets \exists v \in \hat{V}, \exists p' \in P \; (v \equiv X'.a' \wedge p': X \rightarrow \alpha X' \beta )$
          \State \parbox[t]{\dimexpr\linewidth-\csname ALG@tlm\endcsname+\algorithmicindent}{\raggedright\hangindent\algorithmicindent%
            $p', j \gets \exists j \in [i + 1 , \dots,  |\hat{\mathscr{D}}|], \; \exists p: X' \rightarrow \delta \in P \; ( \alpha_j \rightarrow \alpha_{j+1} \equiv \hat{\mathscr{D}}[j] \wedge   \alpha_j \equiv \hat{\alpha}(X',j)\hat{\beta} \wedge  \alpha_{j+1} \equiv  \hat{\alpha}\hat{\delta}\hat{\beta} )$}
          \For{$r \gets (R(p) - \mathit{visited})$}
            \If{$\texttt{LHS($r$)} \in \hat{V}$}
              \State $\texttt{AG\_EVAL}(r, \mathit{Val})$
              \State $\mathit{visited} \gets r$
            \EndIf
          \EndFor
          \State{$\texttt{VISIT\_SEQUENCE\_EVAL}(P, R, \hat{\mathscr{D}}, p', \; (X', j), \; \mathscr{V}, \mathit{Val} )$}  
          \For{$r \gets (R(p) - \mathit{visited})$}
            \If{$\texttt{RHS($r$)} \cap \hat{V} \not = \emptyset$}
              \State $\texttt{AG\_EVAL}(r, \mathit{Val} )$
              \State $\mathit{visited} \gets r$
            \EndIf
          \EndFor
        \EndIf
      \EndFor
    \EndFor
  \EndProcedure
\end{algorithmic}

\end{algorithm}

\end{document}

Since you aren't numbering the lines within the algorithm, you could just have well have set two separate \States:

          \State $p', j \gets \exists j \in [i + 1 , \dots,  |\hat{\mathscr{D}}|], \; \exists p: X' \rightarrow \delta \in P \; ( \alpha_j \rightarrow \alpha_{j+1} \equiv \hat{\mathscr{D}}[j] \wedge   \alpha_j \equiv {}$
          \State \hspace*{\algorithmicindent}$\hat{\alpha}(X',j)\hat{\beta} \wedge  \alpha_{j+1} \equiv  \hat{\alpha}\hat{\delta}\hat{\beta} )$}

The final {} in the first \State ensures appropriate spacing around the math relation \equiv.

Related Question