0

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}
1
  • 1
    Welcome to TeX SX! Maybe nest it in a minipage of width \linewidth (which is different from \textwidth in a standard list environment).
    – Bernard
    Feb 25 at 9:59

1 Answer 1

1

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}

enter image description here


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}

enter image description here

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.