# How to override the numbers in an algorithm?

I have tried a few ways I found here, including This, and This, there are more but I can't remember them.

The issue I'm having is I'd like "Fifth Instruction" to start on 5, I know "Third Instruction" has the correct number by accident, but none of my attempts seem to work. I'm happy with the indented sections being automatically renumbered, but even if it was one numbering the whole way down (1-13) but indented I'd be happy - I can't seem to manage that either.

Any help is really appreciated!

\usepackage{algorithm,algpseudocode}
\usepackage{algorithm2e}
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
\State First Instruction
\State Second Instruction
\begin{algorithmic}[1]
\State Instruction 2.1
\State Instruction 2.2
\end{algorithmic}\\
Third Instruction\\
Fourth Instruction
\begin{algorithmic}[1]
\State Instruction 4.1
\State Instruction 4.2
\State Instruction 4.3
\end{algorithmic}\\
Fifth Instruction
\begin{algorithmic}[1]
\State Instruction 5.1
\State Instruction 5.2
\end{algorithmic}\\
Instruction 6
\end{algorithmic}
\end{algorithm}


• Welcome to TeX.SX! You should probably either load algorithm or algorithm2e. Dec 9, 2021 at 13:30
• Thanks, I have both loaded.. Dec 9, 2021 at 13:33
• This, I see very clearly. But when I try to compile your example, I get errors from loading both packages. Therefore, I would suggest that you chose just one of both packages since they seem to get into conflict with each other. Dec 9, 2021 at 13:40
• Sorry, yeah that makes sense. I have left it with algpseudocode and algorithm, but I'm still getting the same issue. I've tried again with \newcounter and \setcounter but it's still not changing them. Any ideas what else I'm doing wrong? Dec 9, 2021 at 13:42
• I guess, nesting algorithmic environments is not the way to go here. Take a look at this answer for a possible way to solve this. Dec 9, 2021 at 13:47

A simple way with continuous numbering is to use extra indents with \hspace. To be consistent with the default indentation of algorithmic you can use \algorithmicindent as the value for the extra indent (this is 1.5em by default).

MWE (note that I removed algorithm2e because it was causing an error):

\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\begin{algorithmic}[1]
\State First Instruction
\State Second Instruction
\State\hspace{\algorithmicindent} Instruction 2.1
\State\hspace{\algorithmicindent} Instruction 2.2
\State Third Instruction
\State Fourth Instruction
\State\hspace{\algorithmicindent} Instruction 4.1
\State\hspace{\algorithmicindent} Instruction 4.2
\State\hspace{\algorithmicindent} Instruction 4.3
\State Fifth Instruction
\State\hspace{\algorithmicindent} Instruction 5.1
\State\hspace{\algorithmicindent} Instruction 5.2
\State Instruction 6
\end{algorithmic}
\end{algorithm}
\end{document}


Result:

• Thank you, that's a huge help! Dec 9, 2021 at 13:50