Placing pseudocode between paragraphs

algorithmspseudocode

I am trying to re-write a line of an algorithm's pseudocode, and I would like to be able to present it in-text rather than introducing another floating algorithm environment. How can I do this?

Here is an example of exactly what I am trying to achieve:

enter image description here

Edit: I have the following MWE. I'd like to remove the horizontal lines and include numbering like the above.

\documentclass{article}
\usepackage[utf8]{inputenc}

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

\begin{document}

...we can replace the update from Line 8 in Algorithm 3 with the following:

\begin{algorithm}
    \begin{algorithmic}
        \ForAll {$x \in X$}
            \State $y \leftarrow x$
        \EndFor
    \end{algorithmic} 
\end{algorithm}

This modification allows for...

\end{document}

Best Answer

You can use the following as a template-like approach to adjust components of your algorithm. The idea is to update the line numbering scheme (with a \ref prefix) to print in \alph rather than \arabic, and also indent to the appropriate depth so as to match the position (horizontally) in the original algorithm.

enter image description here

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}[H]
  \caption{Euclid's algorithm}\label{alg:euclid}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a, b$}\Comment{The g.c.d.\ of~$a$ and~$b$}
       \State $r \gets a \bmod b$
       \While{$r \neq 0$}\Comment{We have the answer if~$r$ is~$0$}
          \State $a \gets b$
          \State $b \gets r$\label{alg:b_r}
          \State $r \gets a \bmod b$
       \EndWhile
       \State \Return $b$\Comment{The gcd is~$b$}
    \EndProcedure
  \end{algorithmic}
\end{algorithm}

\ldots we can replace the update from Line~\ref{alg:b_r} in Algorithm~\ref{alg:euclid} with the following:

\begin{algorithm}[H]
  \begin{algorithmic}[1]
    \renewcommand\alglinenumber[1]{\footnotesize \ref{alg:b_r}\alph{ALG@line}:}% Update line number to be \alph with prefix
    \expandafter\addtolength\csname ALG@tlm\endcsname{2\dimexpr\algorithmicindent}% Update current indentation
    \ForAll {$x \in X$}
      \State $y \leftarrow x$
    \EndFor
  \end{algorithmic}
\end{algorithm}

This modification allows for\ldots

\end{document}

If you don't want the horizontal rules, then just drop the algorithm environment surrounding the algorithmic environment. Hopefully it doesn't float elsewhere. If that happens, one could insert the horizontal rules manually, unless you have a different algorithm float style.