To Panitan - 10 months ago 88

LaTeX Question

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.

`\begin{addmargin}[10mm]{0mm}`

\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).

\]

\}

\end{addmargin}

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

Answer Source

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:

```
\documentclass{article}
\usepackage{amsmath}
\usepackage{algpseudocode}
\renewcommand{\algorithmicforall}{\textbf{for each}}
\begin{document}
\noindent Let $\mathcal{J} := \{1, ..., n\}$.
\begin{algorithmic}
\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)}$
\EndFor
\State {... update some convergence measure} \Comment{Describe convergence measure}
\EndWhile
\end{algorithmic}
\end{document}
```

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.