算法中的文本

算法中的文本

我需要创建一个算法,在 for 循环中,我必须使用一些单词和数学符号,即算法中的第 6 行和第 7 行。我使用 mbox 来放置文本,但现在我无法使用符号,我如何才能同时使用文本和符号。换句话说,在第 6 行和第 7 行中,我想使用 $,而不是使用 \textit{}

\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}

答案1

\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}

相关内容