0

I have used algorithm environment to write pseudo code just like the photo and get some really good result

enter image description here

and here is the corresponding latex code

\begin{algorithm}
\caption{Find the missing number (A, l, r)}
\begin{algorithmic}[1]
    \Function{ModifiedBinarySearch}{$A, r, l$}{
        \State{m$\gets\lfloor\frac{l+r}{2}\rfloor$}
        \If{$r = l$}
            \State\Return{m}
        \ElsIf{$A[m] = m$}
            \State\Call{ModifiedBinarySearch}{A, m + 1, r}
        \ElsIf{$A[m] = m - 1$} 
            \State\Call{ModifiedBinarySearch}{A, l, m \text{-} 1}
            \State\Call{ModifiedBinarySearch}{A, m + 1, r}
        \ElsIf{$A[m] = m - 2$}:
            \State\Call{ModifiedBinarySearch}{A, l, m \text{-} 1}
        \EndIf{}
    }
    \EndFunction{}
  \end{algorithmic}
\end{algorithm}

but it will encounter some question that it can not break if there is a new page, so I find something useful called breakablealgorithm and the environment setting show below

\newenvironment{breakablealgorithm}
  {% \begin{breakablealgorithm}
   \begin{center}
     \refstepcounter{algorithm}% New algorithm
     \hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled
     \renewcommand{\caption}[2][\relax]{% Make a new \caption
       {\raggedright\textbf{\fname@algorithm~\thealgorithm} ##2\par}%
       \ifx\relax##1\relax % #1 is \relax
        \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##2}%
       \else % #1 is not \relax
         \addcontentsline{loa}{algorithm}{\protect\numberline{\thealgorithm}##1}%
       \fi
       \kern2pt\hrule\kern2pt
     }
  }{% \end{breakablealgorithm}
     \kern2pt\hrule\relax% \@fs@post for \@fs@ruled
   \end{center}
  }
\makeatother

the break was good but there are some indent issue with it like the figure below enter image description here

the upper one is normal algorithm environment and the lower one is the breakablealgorithm environment how can I make it more consistent since the breakablealgorithm has a poor format that add some redundant indent

0 Answers0