3

I am trying to add an algorithm in pseudocode within LyX in version 2.3.2 as described here.

  1. Add the \usepackage{algorithm,algpseudocode} into the preamble
  2. Insert a algorithm block (Insert --> Floating --> Algorithm)
  3. Insert the tex code in the algorithm block (Insert --> Tex Code)
  4. Add the following latex code

    \begin{algorithmic}[1]
    \Require{$\rho \geq 1$}
    \Ensure{$X_k$}
    \While{not converged}
    \State{Solve $X_{k+1}=\min_{X} L(X,Y_k, \mu_k)$}
    \State{$Y_{k+1}=Y_k+\mu_k h(X_{k+1})$}
    \State{$\mu_{k+1}=\rho \mu_k$}
    \EndWhile
    \end{algorithmic}
    

Which delivers the following result:

enter image description here

According to other answers I have read, the algorithmic[1] command should number every line and not start at line 3 within the pseudocode.

How can I fix this and number all lines from the beginning e.g. starting from \Require{$\rho \geq 1$} in this example?

2
  • Technically the syntax is \State abc, not \State{abc}. Similarly for \Require and \Ensure.
    – Werner
    Commented Mar 19, 2019 at 15:26
  • Thans for clarification! Commented Mar 19, 2019 at 16:02

1 Answer 1

3

They're not meant to have line numbers, but you can change that by adding the following to your Document > Settings... > LaTeX Preamble:

\algrenewcommand\Require{\State \algorithmicrequire~}%
\algrenewcommand\Ensure{\State \algorithmicensure~}%

The above updates \Require and \Ensure to be regular \States and therefore set with a line number.

enter image description here

1
  • Thanks a lot for your help! Commented Mar 19, 2019 at 16:02

You must log in to answer this question.

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