## LaTeX forum ⇒ Text Formatting ⇒ Write Algorithm with certain Style

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
GommaFleX
Posts: 10
Joined: Tue Aug 16, 2011 12:19 pm

### Write Algorithm with certain Style

Hi all, I'm trying to write an algorithm with a style as shown in the attachment. Unfortunately line breaks don't work well. The manual says:
Warning For users of earlier versions of algorithmic this construct is a cause of an incompatibility. In the earlier version, instead of starting simple statements with the \STATE command, simple statements were entered as free text and terminated with \\ command. Unfortunately, this simpler method failed to survive the modiﬁcations necessary for statement numbering. However,the \\ command can still be used to force a line break within a simple statement.

Ok..it doesn't work.
if i write
\STATE text
\IF {cond}
\STATE other text
..
it doesn't line break resulting in
5: textif cond then

what can I do?
Attachments
algorithm-style.png (34.59 KiB) Viewed 7038 times
Last edited by GommaFleX on Fri Nov 11, 2011 12:34 pm, edited 1 time in total.

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
The algorithms bundle provides structures which support writing algorithms. Your example translated into code.
\documentclass[11pt]{article}\usepackage[T1]{fontenc}\usepackage{algorithm,algorithmic} \begin{document}   \begin{algorithm}     \caption{Calculate $Q_{ij}$}\label{alg:calcqij}     \begin{algorithmic}[1]       \REQUIRE $\tau>0,A\ne\emptyset,N=|A|$       \ENSURE $Q_{ij}(\Delta t)\sum_{t\in A} e^{-\frac{|t-\delta t|}{\tau}}, \forall t\in A$       \STATE $\mathbf{A}\Leftarrow\mathrm{sort}(A)\;\{O(N\log N)\}$       \STATE $Q^-(1)\Leftarrow 1$       \STATE $Q^+(N)\Leftarrow 0$       \FOR{$k=1$ \TO $N-1$}       \STATE  $ed(k)\Leftarrow e^{-\frac{\mathbf{A}(k+1)-\mathbf{A}(k)}{\tau}}$       \ENDFOR       \FOR{$k=1$ \TO $N-1$}       \STATE $Q^-(k+1)\Leftarrow 1+Q^-(k)\cdot ed(k)$       \STATE $Q^+(N-k)\Leftarrow (Q^+(N-k+1)+1)\cdot ed(N-k)$       \ENDFOR       \FOR{$k=1$ \TO $N$}       \STATE $Q_{ij}(A(k))\Leftarrow Q^+(k)+Q^-(k)$       \ENDFOR     \end{algorithmic}   \end{algorithm}\end{document}

Thorsten
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

GommaFleX
Posts: 10
Joined: Tue Aug 16, 2011 12:19 pm
thank you for the response. Anyway i found the solution. There was an error with missing brackets in the line above. TeXnicCenter produced the output without line break, but TeXmaker forced me to correct the error before producing output