3

I have an algorithm like the bellow code. How can I align line seven with its upper lines (please look at the uploaded picture)?

\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} 

enter image description here

2
  • I am getting an error when I compile your code inside a minimal example. Why is that?
    – Werner
    Commented Sep 30, 2019 at 4:26
  • Thanks for your attention. I have modified the minimal example so you can run it with no problem.
    – mdslt
    Commented Sep 30, 2019 at 14:11

2 Answers 2

3

You can make a top aligned \parbox and fix the interline space after it, by saving the depth of the last line.

\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} 

enter image description here

A different solution, using the current indentation, that's stored in \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} 

The output is the same.

3

You can use the definition of \brokenline below - it sets it's argument inside a [t]op-aligned \parbox of appropriate width. \struts are added to ensure that the baselines are also inline with content outside the \parbox.

enter image description here

\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} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .