[Tex/LaTex] How to write sentences in an algorithm in latex

algorithmicalgorithmsformatting

Hi I am trying to write an algorithm which looks something like the image below:

Algorithm

I was wondering how to write the "Step 1 (Update)" and also the rest of the line which says "When an edge e…." should all that be inside the math symbols i.e.. \[\] ??

I am kinda new to latex. Thanks for the help!

Also I was wondering what is the difference between algorithm and algorithmic?

Best Answer

Here you go:

\documentclass{article}
\usepackage{graphicx}
\newcommand\smallarrowover[1]{\scalebox{.5}{$\overrightarrow{#1}$}}

\begin{document}

\noindent
\underline{\sf Step 1 (Update):} When an edge $e=\{u,v\}\in E[G]$ arrives, update each $Z_{\smallarrowover{ab}}$ w.r.t.
\begin{equation}
Z_{\smallarrowover{ab}}(G)\leftarrow Z_{\smallarrowover{ab}}+\mathcal M_{\smallarrowover{ab}}(u,v)+\mathcal M_{\smallarrowover{ab}}(v,u).
\end{equation}

\end{document}

This is the result:

enter image description here

Initially I used \vec{ab} for the arrow over ab, but that did not stretch, and putting \overrightarrow{ab} in the subscripts did not make it smaller, as you want. So I defined a new command, which needs the \graphicx package. Also, you might not need the \noindent I have at the beginning, depending on what you have before it. I put it there so the whole thing would fit on one line.

P.S. I have no experience with the algorithm or algorithmic environments.