2

I am making an algorithm with a nested for loop, and there are multiple in the loop. They are presented on the same line, but I want them to start on a newline. Any ideas how I can do this?

This is my code:

\begin{algorithm}
\begin{algorithmic}[1]
\State {Input $\pi$, the random policy}
\State {Initialize V(s)=0 for all s $\in$ S}
\State {Repeat}
\State {$\Delta$ \gets 0}

\For{for each s $\in$ S }:
\For{for each direction $\in$ directions }:
\For{for slide in slides}:
\State{v \gets V(s)}
\State{V(s) \gets \sum_{a} $\pi${(s,a)}\sum_{s'}P_{ss'}^{a}[R_{ss'}^{a}+\gamma V(s')]}
\State{$\Delta$ \gets max($\Delta$,\mid v-V(s)\mid)}
\State{Until $\Delta$ < $\Theta$ ($\Theta$ = 0,01}
\State{Output V \approx V^{$\pi$}}
\EndProcedure
\end{algorithmic}
\caption{Policy evaluation}
\end{algorithm}
2
  • Welcome to TeX SX! What exactly should be on a separate line?
    – Bernard
    Apr 28, 2019 at 20:33
  • Please see my added answer ...
    – Mensch
    Aug 31, 2019 at 7:50

1 Answer 1

1

Well, because you gave us no complete compilable TeX code (MWE) I have to guess. Based on your given code snippet I used packages algorithm and algpseudocode and added the missing \EndFor commands. And I corrected, added or moved some $ in your code ...

Please see the following MWE

\documentclass{article}

\usepackage{algorithm}% <=========================== \State
\usepackage{algpseudocode}


\begin{document}

\begin{algorithm}
\begin{algorithmic}[1]
\Procedure{test}{Output} % <============================================
\State {Input $\pi$, the random policy}
\State {Initialize V(s)=0 for all s $\in$ S}
\State {Repeat}
\State {$\Delta \gets 0$}
\For{for each s $\in$ S}
  \For{for each direction $\in$ directions}
    \For{for slide in slides}
      \State{v $\gets$ V(s)}
      \State{$V(s) \gets \sum_{a} \pi{(s,a)}\sum_{s'}P_{ss'}^{a}[R_{ss'}^{a}+\gamma V(s')]$}
      \State{$\Delta \gets max(\Delta,\mid v-V(s)\mid)$}
      \State{Until $\Delta$ < $\Theta$ ($\Theta$ = 0,01}
      \State{Output $V \approx V^{\pi}$}
    \EndFor% <==========================================================
  \EndFor% <============================================================
\EndFor% <==============================================================
\EndProcedure
\end{algorithmic}
\caption{Policy evaluation}
\end{algorithm}

\end{document}

and its result:

resulting pdf

If that is not what you want please edit your question, make your given code snippet complete and compilable, and add an screenshot of your result and mark there what you want to have changed. And please answer comments requesting more informations making your question clearer ...

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.