在引文中打印顶点/上标和下标

在引文中打印顶点/上标和下标

我正在尝试在伪代码中打印顶点和下标。我使用此代码编写了它,但它按原样打印它,而没有进行数学转换。

\begin{quote}\linenumbersep=-5pt
\resetlinenumber[1]
\begin{alltt}{\internallinenumbers

FLOYD-WARSHALL(W):
     n = W.righe
     D\(^{(0)}\) = W
     for k = 1 to n:
          sia D\(^{(k)}\) = d\(_{ij}^{(k)}\) una nuova matrice n x n
          for i = 1 to n:
          for j = 1 to n:
               d\(_{ij}^{(k)}\) = min(d\(_{ij}^{(k-1)}\),d\(_{ik}^{(k-1)}\)+d\(_{kj}^{(k-1)}\) )
     return D\(^{(n)}\) 

}
\end{alltt}
\end{quote}

我该如何修复它,以便 $D^{(0)}$ 变成 D^(0)(因为我们用它来表示算法中的步骤,例如“在步骤 k 时为 D”)?谢谢

答案1

好的,您必须使用\(...\)数学模式、\sp和。^\sb_

\documentclass{article}
% for the first rendering
\usepackage{lineno,alltt}

% for the second rendering
\usepackage[noend]{algpseudocode}

\renewcommand{\algorithmicdo}{\unskip:}

\begin{document}

\begin{quote}\linenumbersep=-5pt
\resetlinenumber[1]
\begin{alltt}{\internallinenumbers
FLOYD-WARSHALL(W):
     n = W.righe
     D\(\sp{(0)}\) = W
     for k = 1 to n:
          sia D(k) = d\(\sb{ij}\sp{(k)}\) una nuova matrice n x n
          for i = 1 to n:
          for j = 1 to n:
               d\(\sb{ij}\sp{(k)}\) = min(d\(\sb{ij}\sp{(k-1)}\),d\(\sb{ik}\sp{(k-1)}\) + d\(\sb{kj}\sp{(k-1)}\))
     return D(n) 
}
\end{alltt}
\end{quote}

\begin{algorithmic}[1]
\Procedure{FLOYD-WARSHALL$(W)$:}{}
  \State $n = W.\mathrm{righe}$
  \State $D^{(0)} = W$
  \For{$k = 1$ to $n$}
    \State sia $D(k) = d_{ij}^{(k)}$ una nuova matrice $n \times n$
    \For{$i = 1$ to $n$}
      \For{$j = 1$ to $n$}
        \State $d_{ij}^{(k)} = \min(d_{ij}^{(k-1)},d_{ik}^{(k-1)}+d_{kj}^{(k-1)})$
      \EndFor
    \EndFor
  \EndFor
  \State return $D(n)$
\EndProcedure
\end{algorithmic}


\end{document}

我添加了版本,algpseudocode并且我毫不怀疑我更喜欢哪一个。

在此处输入图片描述

相关内容