算法书写风格不符合要求

算法书写风格不符合要求

在此处输入图片描述

这是我想在 latex 中写下的算法。但写下一些代码后,它无法给出我想要的输出。

我的代码:

\begin{algorithm}
\caption{The GraphSlam Algorithm for the Full SLAM Problem with known Correspondence}\label{algo}
\begin{algorithmic}[1]
\State\textbf{{Algorithm GraphSLAM\textunderscore known\textunderscore
    correspondence}}
\State $\mu_{0:t}$ = GraphSLAM\textunderscore initialize(u_{1:t})
\State repeat
\State $\Omega$,$\xi$ = GraphSLAM\textunderscore linearize(u_{1:t},z_{1:t},c_{1:t},$\mu_{0:t}$)
\State $\bar\Omega$,$\bar\xi$ = GraphSLAM\textunderscore reduce($\Omega$,$\xi$)
\State $\mu$,$\Sigma_{0:t}$ = GraphSLAM\textunderscore solve($\bar\Omega$,$\bar\xi$,$\Omega$,$\xi$)
\State until convergence
\State return $\mu$

    \end{algorithmic}
\end{algorithm}

我得到这个输出

在此处输入图片描述

我将如何获得期望的输出?

答案1

以下是与您的布局相匹配的快速模型:

在此处输入图片描述

\documentclass{article}

\newenvironment{algorithm}[2][htbp]
  {\begin{table}[#1]
     \caption{#2}%
     \setcounter{algline}{0}% Reset algorithm line counter
     \renewcommand{\arraystretch}{1.2}%
     \itshape
     \begin{tabular}{| p{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth} |}
       \hline
  }
  {    \\
       \hline
     \end{tabular}
   \end{table}
  }

\newcounter{algline}
\newlength{\algnummargin}
\setlength{\algnummargin}{2em}
\newcommand{\algline}{%
  \refstepcounter{algline}%
  \makebox[\algnummargin][l]{\textup{\small\thealgline:}}%
}
\newcommand{\textbfup}[1]{\textbf{\textup{#1}}}

\begin{document}

\begin{algorithm}{The GraphSLAM Algorithm for the Full SLAM Problem with Known Correspondence}
  \algline \textbfup{Algorithm GraphSLAM\_known\_correspondence($u_1$, $z_1$, $c_1$):} \\[.5\normalbaselineskip]
  \algline $\mu_0 = \textbfup{GraphSLAM\_initialize($u_1$)}$ \\
  \algline repeat \\
  \algline \quad $\Omega, \xi = \textbfup{GraphSLAM\_linearize($u_1$, $z_1$, $c_1$, $\mu_1$)}$ \\
  \algline \quad $\bar{\Omega}, \bar{\xi} = \textbfup{GraphSLAM\_reduce($\Omega$, $\xi$)}$ \\
  \algline \quad $\mu, \Sigma_0 = \textbfup{GraphSLAM\_solve($\bar{\Omega}$, $\bar{\xi}$, $\Omega$, $\xi$)}$ \\
  \algline until convergence \\
  \algline return $\mu$
\end{algorithm}

\end{document}

算法标题被设置为algorithm环境的强制参数。

相关内容