LaTeX forum ⇒ Text FormattingWrite 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

Postby GommaFleX » Fri Nov 11, 2011 12:28 am

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 7038 times
Last edited by GommaFleX on Fri Nov 11, 2011 12:34 pm, edited 1 time in total.

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

Postby localghost » Fri Nov 11, 2011 10:58 am

The algorithms bundle provides structures which support writing algorithms. Your example translated into code.
  1. \documentclass[11pt]{article}
  2. \usepackage[T1]{fontenc}
  3. \usepackage{algorithm,algorithmic}
  4.  
  5. \begin{document}
  6. \begin{algorithm}
  7. \caption{Calculate $Q_{ij}$}\label{alg:calcqij}
  8. \begin{algorithmic}[1]
  9. \REQUIRE $\tau>0,A\ne\emptyset,N=|A|$
  10. \ENSURE $Q_{ij}(\Delta t)\sum_{t\in A} e^{-\frac{|t-\delta t|}{\tau}}, \forall t\in A$
  11. \STATE $\mathbf{A}\Leftarrow\mathrm{sort}(A)\;\{O(N\log N)\}$
  12. \STATE $Q^-(1)\Leftarrow 1$
  13. \STATE $Q^+(N)\Leftarrow 0$
  14. \FOR{$k=1$ \TO $N-1$}
  15. \STATE $ed(k)\Leftarrow e^{-\frac{\mathbf{A}(k+1)-\mathbf{A}(k)}{\tau}}$
  16. \ENDFOR
  17. \FOR{$k=1$ \TO $N-1$}
  18. \STATE $Q^-(k+1)\Leftarrow 1+Q^-(k)\cdot ed(k)$
  19. \STATE $Q^+(N-k)\Leftarrow (Q^+(N-k+1)+1)\cdot ed(N-k)$
  20. \ENDFOR
  21. \FOR{$k=1$ \TO $N$}
  22. \STATE $Q_{ij}(A(k))\Leftarrow Q^+(k)+Q^-(k)$
  23. \ENDFOR
  24. \end{algorithmic}
  25. \end{algorithm}
  26. \end{document}



Thorsten
LaTeX Community Moderator
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ 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

Postby GommaFleX » Fri Nov 11, 2011 12:07 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


Return to “Text Formatting”

Who is online

Users browsing this forum: No registered users and 2 guests