I am a beginner in writing algorithm in LaTeX using algorithm2e. I am trying to write a simple algorithm (code is attached herewith). However, I am not getting correct output. I want to see line numbers. The output is also attached herewith.

Please note that i included the following command in LaTeX preamble too see line numbers


There must be something wrong in my code. Is there anyone who can help me execute it correctly?

Output produced

\begin{algorithm} [H]
\caption{QL algorithm}
Initialize Q-table values (Q(s, a)) arbitrarily\
Initialize a state(s_t)\
Repeat Step 4 to 6 until learning period ends\
Choose an action (a_t) for the current state (s_t) using an exploratory policy\
Take action (a_t) and observe the new state (s_t+1) and reward (r_t+1)\
Update Q-value\
  • 1
    Welcome to TeX.SE! Please extend your code fragment to complete small document, which begin with \documentclass and end with \end{document}
    – Zarko
    Dec 23, 2019 at 1:36
  • 1
    The problem might only come from the subscripts, which should probably be in math mode.
    – Vincent
    Dec 23, 2019 at 2:44

1 Answer 1


This is how you would set things up using algorithm2e. Specifically, you need line-ends to be denoted by \;. Also consider using labels (\nllabel) and that you can \reference.

enter image description here




  \caption{QL algorithm}
  Initialize $Q$-table values $(Q(s, a))$ arbitrarily\;
  Initialize a state $(s_t)$\;
  Repeat Steps~\ref{alg:step_4} to~\ref{alg:step_6} until learning period ends\;
  Choose an action $(a_t)$ for the current state $(s_t)$ using an exploratory policy\; \nllabel{alg:step_4}
  Take action $(a_t)$ and observe the new state $(s_t + 1)$ and reward $(r_t + 1)$\;
  Update $Q$-value\; \nllabel{alg:step_6}


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .