0

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}

How it currently looks

5
  • Welcome to TeX.SX! You should probably either load algorithm or algorithm2e. Dec 9, 2021 at 13:30
  • Thanks, I have both loaded..
    – badAtMaths
    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?
    – badAtMaths
    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

1 Answer 1

2

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:

enter image description here

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

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.