To Panitan To Panitan - 2 months ago 47
LaTeX Question

What is the right way to insert an equation within a pseudo code

I start writing my note in Latex and I am trying to write a pseudo code based on this equation:


Here is my code I have tried so far.

\textbf{Repeat until convergence:} \ \{
\theta_j := \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)} \quad (\text{for every} \ j).

I still think it is a bit messy. Do you have an easier way to write this?


This question should probably be moved to tex.stackexchange.

There exists several different neat Latex packages specifically with the purpose of writing pseudocode. One of the more simple ways to do it is by using the Algorithmicx package (package documentation here), specifically the algpseudocode part of it.

Here is an MWE for your pseudocode using Algoritmicx:


\renewcommand{\algorithmicforall}{\textbf{for each}}

\noindent Let $\mathcal{J} := \{1, ..., n\}$.

    \While {Some convergence criteria is not fulfilled ...}
        \ForAll {$j \in \mathcal{J}$}
            \State $\theta_j \gets \theta_j + \alpha \sum_{i=1}^m(y^{(i)}-h_\theta(x^{(i)})) \ x_j^{(i)}$
        \State {... update some convergence measure} \Comment{Describe convergence measure}


Note that, at the fourth non-empty line, I've overloaded the text of \ForAll command from algorithmicx (\algorithmicforall), to become "for ... each" rather than "for ... all".

Note also that, generally, only comments may contain text in pseudocode, so the sentences "Some convergence criteria is not fulfilled ..." and "... update some convergence measure" above should be replaced with however your define convergence of algorithm/equation.