我想知道是否有一种方法可以在算法中进行多行数学运算以防止出现这样的情况:
\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}
答案1
\State
在适当宽度的内设置相应的\parbox
。这里的宽度为\linewidth-\ALG@lm+\algorithmicindent
。此外,我已\parbox
使用\raggedright
(如果没有它,您将得到一些不必要的拉伸以适应宽度) 和\algorithmicindent
-sizedhang
来设置indent
:
\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}
由于您没有对算法中的行进行编号,因此您可以设置两个单独的\State
行:
\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} )$}
{}
第一个中的最后一个\State
确保数学关系周围有适当的间距\equiv
。