Indent text but not numbers using latex

So, basically, what I'm trying to do is have an ordered list describing pseudocode. But I want the numbers of each step to be aligned but the text I want indented.

I'm using R-studio and am very new to Latex, so I'm not sure how to solve the problem.

Below is an example of the code I'm using:

\begin{algorithm}[H]

{\textbf{Algorithm} {EXAMPLE}}
\begin{enumerate}

\item Line 1
\item line 2
\setlength\itemindent{25pt} \item line 3
\item line 4
\setlength\itemindent{55pt} \item  line 5
\item line 6
\setlength\itemindent{25pt} \item line 7
\item line 8
\setlength\itemindent{0pt} \item line 9
\item line 10

\end{enumerate}
\end{algorithm}


This produces an output like this:

However, I would like to have all the numbers aligned but keep the text indented.

Any suggestions as to how I'd solve this?

• Welcome to TeX.SX! So you want line numbering instead of an ordered list? – TeXnician Oct 23 '18 at 14:54
• I'm not sure... I literally started learning Latex today... so I don't know any of the terms yet – Electrino Oct 23 '18 at 14:57
• Line numbering is no TeX term but is a concept which involves having a number n at the beginning of the line where n of the first line is (mostly) 1 and n of the successor of this line is n+1. Do you want to start each line with a line number? – TeXnician Oct 23 '18 at 15:04
• Yes, that sounds like what I'm trying to do. – Electrino Oct 23 '18 at 15:18
• Have you seen tex.stackexchange.com/questions/18111/… – TeXnician Oct 23 '18 at 15:20

\documentclass{article}
\usepackage{float}
\usepackage{xcolor}
\usepackage{minted}
\floatstyle{ruled}
\newfloat{algorithm}{tbp}{loa}
\floatname{algorithm}{Algorithm}
\begin{document}
\begin{algorithm}
\caption{The \LaTeX\ preamble of this example.}
\begin{minted}[linenos,bgcolor=orange!05]{latex}
\documentclass{article}
\usepackage{float}
\usepackage{xcolor}
\usepackage{minted}
\floatstyle{ruled}
\newfloat{algorithm}{tbp}{loa}
\floatname{algorithm}{Algorithm}
\end{minted}
\end{algorithm}
\end{document}


Perhaps you're after something like this:

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\let\oldState\State
\renewcommand{\State}[1][0]{%
\oldState\hspace*{#1\dimexpr\algorithmicindent\relax}%
}

\begin{document}

\begin{algorithm}
\caption{Example}
\begin{algorithmic}[1]
\State Line 1
\State Line 2
\State[1] Line 3
\State[1] Line 4
\State[2] Line 5
\State[2] Line 6
\State[1] Line 7
\State[1] Line 8
\State Line 9
\State Line 10
\end{algorithmic}
\end{algorithm}

\end{document}


An update to the \State macro makes it accept an option argument for indenting that line by a multiple of \algorithmicindent. This won't look acceptable if the line of code is longer than the text width though.

• This is exactly what I was trying to achieve, however, I just can't get it to work for me. I'm using R-Markdown and knitting to a pdf. I've been following some guides on using Latex in R-markdown, but to no success. I'm very new to Latex and I'm not sure if I'm doing it correctly. I was putting this into the header --- title: "My Title" author: me output: pdf_document documentclass: article header-includes: - \usepackage{algorithm,algpseudocode,xfp} --- And then just pasting your code to see if I can get it to run... but no success – Electrino Oct 24 '18 at 11:30
• @egreg: You should try it... – Werner Oct 24 '18 at 15:13
• @Electrino: You need the redefinition of \State as well. For that, add the \let and \renewcommand components to a file (say) mymacros.tex, save it in your working forlder and add \input{mymacros} to your header-includes clause. – Werner Oct 24 '18 at 15:15
• @Werner You should use #1\dimexpr\algorithmicindent\relax – egreg Oct 24 '18 at 15:28