# How to insert an algorithm inline with an item in itemize/enumerate

I am using an itemize to list several algorithms, and I want to use the algorithm package to display the algorithm.

However, when I use the \begin{algorithm} within one of the items, the algorithm stretches across the entire width of the page, whereas I want it to be in line with the start of the item.

How do I do this?

For reference, this is what my incorrect latex looks like:

\begin{itemize}
\item this algorithm .....
\begin{algorithm}
\caption{Algorithm 1}\label{alg:cap}
\begin{algorithmic}
\state{algorithm step}
\end{algorithmic}
\end{algorithm}
\item ...
\end{itemize}

• Welcome to TeX SX! Maybe nest it in a minipage of width \linewidth (which is different from \textwidth in a standard list environment). Feb 25 at 9:59

You are using a floating environment although you want the algorithm to be fixed in a list. You can therefore omit the algorithm environment:

\documentclass{article}
\usepackage{algorithm, algpseudocodex}
\usepackage{enumerate}
\usepackage{caption}

\begin{document}
\begin{itemize}
\item this algorithm .....
\item Algorithm 1:\label{alg:cap}
\begin{algorithmic}
\State $i \gets 10$
\If{$i\geq 5$}
\State $i \gets i-1$
\Else
\If{$i\leq 3$}
\State $i \gets i+2$
\EndIf
\EndIf
\end{algorithmic}
\item ...
\end{itemize}
\end{document}


Edit: Some lines above and below:

\documentclass{article}
\usepackage{algorithm, algpseudocodex}
\usepackage{enumitem}
\usepackage{caption}

\begin{document}
\begin{itemize}
\item this algorithm .....
\item Algorithm 1:\label{alg:cap}
\item[] elegant line above
\begin{algorithmic}
\State $i \gets 10$
\If{$i\geq 5$}
\State $i \gets i-1$
\Else
\If{$i\leq 3$}
\State $i \gets i+2$
\EndIf
\EndIf
\end{algorithmic}
\item[] elegant line below
\item ...
\end{itemize}
\end{document}


If you want to tweak linespacing etc. see the documentation of enumitem

• Is there an elegant way to add the lines above and below the algorithm? Feb 25 at 20:45
• I added some lines above and below, maybe they are elegant
– DG'
Feb 26 at 22:30