[Tex/LaTex] Text in algorithm

algorithms

I need to create an algorithm, inside the for cycle, I a have to use some words and mathematical notations, lines 6 and 7 in the algorithm. I used mbox in order to place the text, but now I am not able to use the notations, how can I use text and notations at the same time. In other words instead of using \textit{} in lines 6 and 7 I want to use $

\documentclass[oneside]{book}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}

\section{Example Algorithm}

\begin{algorithm}
\caption{Algorithm}\label{alg:allreduce}
\begin{algorithmic}[1]
\State $left \gets (me-1)\bmod p$
\State $right \gets (me+1)\bmod p$
\State $result \gets M_{me}$
\State $M \gets result$
\For{$ k = 1, 2, ...,p-1$}
 \State \mbox{Send} \textit{M} from \textit{right}  \Comment{The "send" is assumed to be non-blocking}
 \State \mbox{Receive}  from \textit{left} \Comment{Lines 6-7 can be implemented via MPI}
 \State $result\gets result     \cup M$
\EndFor 
\State \textbf{end for}
\end{algorithmic}
\end{algorithm}
\end{document}

Best Answer

\documentclass[oneside]{book}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\section{Example Algorithm}

\begin{algorithm}
  \caption{Algorithm}\label{alg:allreduce}
  \begin{algorithmic}[1]
    \State $\mathit{left} \gets (me-1) \bmod p$
    \State $\mathit{right} \gets (me+1) \bmod p$
    \State $\mathit{result} \gets M_{me}$
    \State $M \gets \mathit{result}$
    \For{$ k = 1, 2, \dots, p-1$}
      \State Send $M$ from $\mathit{right}$  \Comment{The "send" is
        assumed to be non-blocking} 
      \State Receive from $\mathit{left}$ \Comment{Lines 6-7 can be
        implemented via MPI} 
      \State $\mathit{result} \gets \mathit{result} \cup M$
    \EndFor 
  \end{algorithmic}
\end{algorithm}

\end{document}
Related Question