如何在 algorithmicx 中对齐长状态

如何在 algorithmicx 中对齐长状态

我想重新编辑论文中的算法。但是当我在 中有较长的状态时,语句无法停留在正确的位置algorithmicx

我希望它与纸张对齐。有什么简单的解决方案吗?

我的代码是

\documentclass{IEEEtran}
\usepackage{algorithm} 
\usepackage{algpseudocode} 


\begin{document} 
\begin{algorithm}
    \caption{Funcation scaleUpResources()} 
    \begin{algorithmic}[1]
        \State Select a kind of $newVm$ satisfying the $r_{new}\left(t_{i}\right)+t s_{i} / P_{new}+delay<d_{i}$;
        \State Sort $H_a$ in a decreasing order by the remaining MIPS;
        \For {$h_k$ in $H_a$}
            \If {$h_k$ can accommodate $newVm$}
                \State create $newVm$ on $h_k$;
                \State return $newVm$
            \EndIf
        \EndFor
        \For {$h_s$ in $H_s$}
            \State{
                Migrate the VM with minimal MIPS to other hosts with the fault-tolerant requirements: 1. The primary and the backup of one task can not allocate on the same host 2. $t_i^P$ and $t_j^P$ can not  on the same host if $t_i^B$ and $t_j^B$ overlap.
            } 
            \If{$h_s$ can accommodate $newVm$}
                \State Create $newVm$ on $h_s$;
                \State return $newVm$
            \EndIf
        \EndFor
        \State Turn on a host on $h_new$ in $H_s$;
        \If{the MIPS of $h_{new}$ satisfies $newVm$}
            \State Create $newVm$ on $h_new$
            \State return $newVm$
        \Else
            \State return $NULL$
        \EndIf
    \end{algorithmic} 
\end{algorithm}

\end{document}

我的 LaTeX 中的视图:

在此处输入图片描述

论文中的观点:

在此处输入图片描述

答案1

您可以将整个措辞放入适当宽度的[t]操作对齐中( ):\parbox\linewidth-\algorithmicindent

在此处输入图片描述

\documentclass{IEEEtran}

\usepackage{algorithm,algpseudocode}

\begin{document}

\begin{algorithm}
  \caption{Function scaleUpResources()}
  \begin{algorithmic}[1]
    \State Select a kind of $\mathrm{newVm}$ satisfying the 
      $r_{\mathrm{new}}(t_{i}) + t s_{i} / P_{\mathrm{new}} + \mathrm{delay} < d_{i}$;
    \State Sort $H_a$ in a decreasing order by the remaining MIPS;
    \For {$h_k$ in $H_a$}
      \If {$h_k$ can accommodate $\mathrm{newVm}$}
        \State create $\mathrm{newVm}$ on $h_k$;
        \State return $\mathrm{newVm}$
      \EndIf
    \EndFor
    \For {$h_s$ in $H_s$}
      \State
        \parbox[t]{\dimexpr\linewidth-\algorithmicindent}{%
          Migrate the VM with minimal MIPS to other hosts with the fault-tolerant requirements: 
          1. The primary and the backup of one task can not allocate on the same host 
          2. $t_i^P$ and $t_j^P$ can not  on the same host if $t_i^B$ and $t_j^B$ overlap.%
        }
      \If{$h_s$ can accommodate $\mathrm{newVm}$}
        \State Create $\mathrm{newVm}$ on $h_s$;
        \State return $\mathrm{newVm}$
      \EndIf
    \EndFor
    \State Turn on a host on $h_new$ in $H_s$;
    \If{the MIPS of $h_{\mathrm{new}}$ satisfies $\mathrm{newVm}$}
      \State Create $\mathrm{newVm}$ on $h_{\mathrm{new}}$
      \State return $\mathrm{newVm}$
    \Else
      \State return $\mathrm{NULL}$
    \EndIf
  \end{algorithmic} 
\end{algorithm}

\end{document}

相关内容