Text FormattingWrite Algorithm with certain Style

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

Write Algorithm with certain Style

Post by GommaFleX »

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 modifications 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
algorithm-style.png (34.59 KiB) Viewed 10253 times
Last edited by GommaFleX on Fri Nov 11, 2011 12:34 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Write Algorithm with certain Style

Post by localghost »

The algorithms bundle provides structures which support writing algorithms. Your example translated into code.

Code: Select all

\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
GommaFleX
Posts: 10
Joined: Tue Aug 16, 2011 12:19 pm

Re: Write Algorithm with certain Style

Post by GommaFleX »

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
Post Reply