增加算法环境中行前的水平间距

增加算法环境中行前的水平间距

我有一个类似以下代码的算法。我怎样才能将第七行与其上面的行对齐(请看上传的图片)?

\documentclass[a4paper,11pt]{article}

\usepackage[fleqn]{amsmath}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{geometry}
\newgeometry{vmargin={30mm}, hmargin={25mm,25mm}}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{amssymb}
\usepackage{academicons}

\begin{document}

\begin{algorithm}
\caption{The pseudo code of the proposed accelerated BDA}\label{Alg2}
\begin{algorithmic}[1]
\Statex Initiate the algorithm
\State $nPop \gets$ Size of population
\For {$n = 1:nPop$}
    \State $x_{ijk}$ and $l_{ijk} \gets 0$ \: \: $\forall i,j \in \mathcal{N}; k \in \mathcal{V}$
    \State $PC \gets$ All possible combinations of manufacturers and distributors
    \While {further retailer can be possibly assigned to vehicles}
        \State Randomly select a combination of manufacturer and distributor from $PC$
        \State $i \gets$ the selected manufacturer, $j \gets$ the selected distributor and
        \Statex $k \gets$ the first available vehicle of manufacturer $i$
        \If {}
        \EndIf
    \EndWhile
\EndFor
\end{algorithmic}
\end{algorithm}

\end{document} 

在此处输入图片描述

答案1

\parbox您可以通过保存最后一行的深度,使顶部对齐并在其后修复行间空间。

\documentclass[a4paper,11pt]{article}

\usepackage[fleqn]{amsmath}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{geometry}
\newgeometry{vmargin={30mm}, hmargin={25mm,25mm}}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{amssymb}
\usepackage{academicons}

\makeatletter
\newcommand{\SplitState}[1]{%
  \State
  \parbox[t]{\dimexpr\linewidth-\ALG@thistlm}{%
    #1\par\xdef\Split@prevdepth{\the\prevdepth}%
  }\par
  \prevdepth\Split@prevdepth
}
\makeatother

\begin{document}

\begin{algorithm}
\caption{The pseudo code of the proposed accelerated BDA}\label{Alg2}
\begin{algorithmic}[1]
\Statex Initiate the algorithm
\State $nPop \gets$ Size of population
\For {$n = 1:nPop$}
    \State $x_{ijk}$ and $l_{ijk} \gets 0$ \: \: $\forall i,j \in \mathcal{N}; k \in \mathcal{V}$
    \State $PC \gets$ All possible combinations of manufacturers and distributors
    \While {further retailer can be possibly assigned to vehicles}
        \State Randomly select a combination of manufacturer and distributor from $PC$
        \SplitState{$i \gets$ the selected manufacturer, $j \gets$ the selected distributor and \\
               $k \gets$ the first available vehicle of manufacturer $i$}
        \If {}
        \EndIf
    \EndWhile
\EndFor
\end{algorithmic}
\end{algorithm}

\end{document} 

在此处输入图片描述

另一种解决方案是使用当前缩进,存储在\ALG@thistlm

\documentclass[a4paper,11pt]{article}

\usepackage[fleqn]{amsmath}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{geometry}
\newgeometry{vmargin={30mm}, hmargin={25mm,25mm}}
\usepackage{algpseudocode}
\usepackage{algorithm}
\usepackage{amssymb}
\usepackage{academicons}

\makeatletter
\newcommand{\Statey}{\Statex\hspace*{\ALG@thistlm}}

\begin{document}

\begin{algorithm}
\caption{The pseudo code of the proposed accelerated BDA}\label{Alg2}
\begin{algorithmic}[1]
\Statex Initiate the algorithm
\State $nPop \gets$ Size of population
\For {$n = 1:nPop$}
    \State $x_{ijk}$ and $l_{ijk} \gets 0$ \: \: $\forall i,j \in \mathcal{N}; k \in \mathcal{V}$
    \State $PC \gets$ All possible combinations of manufacturers and distributors
    \While {further retailer can be possibly assigned to vehicles}
        \State Randomly select a combination of manufacturer and distributor from $PC$
        \State $i \gets$ the selected manufacturer, $j \gets$ the selected distributor and
        \Statey $k \gets$ the first available vehicle of manufacturer $i$
        \If {}
        \EndIf
    \EndWhile
\EndFor
\end{algorithmic}
\end{algorithm}

\end{document} 

输出是一样的。

答案2

您可以使用下面的定义- 它在适当宽度的op-aligned\brokenline内设置它的参数。添加 s 以确保基线也与 之外的内容一致。[t]\parbox\strut\parbox

在此处输入图片描述

\documentclass{article}

\usepackage{amsmath}
\usepackage{algpseudocode}
\usepackage{algorithm}

\makeatletter
\newcommand{\brokenline}[2][t]{\parbox[#1]{\dimexpr\linewidth-\ALG@thistlm}{\strut\raggedright #2\strut}}
\makeatother

\begin{document}

\begin{algorithm}
  \caption{The pseudo code of the proposed accelerated BDA}
  \begin{algorithmic}[1]
    \Statex Initiate the algorithm
    \State $\mathit{nPop} \gets$ Size of population
    \For {$n = 1:\mathit{nPop}$}
      \State $x_{ijk}$ and $l_{ijk} \gets 0$ \: \: $\forall i,j \in \mathcal{N}; k \in \mathcal{V}$
      \State $PC \gets$ All possible combinations of manufacturers and distributors
      \While {further retailer can be possibly assigned to vehicles}
        \State \brokenline{%
          Randomly select a combination of manufacturer and distributor from $PC$}
        \State \brokenline{%
          $i \gets$ the selected manufacturer, $j \gets$ the selected distributor and
          $k \gets$ the first available vehicle of manufacturer $i$}
        \If {}
        \EndIf
      \EndWhile
    \EndFor
  \end{algorithmic}
\end{algorithm}

\end{document} 

相关内容