为什么使用 algorithm2e 时我的“末端”没有对齐?

为什么使用 algorithm2e 时我的“末端”没有对齐?

我正在使用算法2e,但我的“结束”没有对齐。输出如下

\begin{algorithm}
    \caption{UpdateRobotLocation}
    \label{alg:r2p}
    $robotLocation{\longleftarrow} robotLocation + robotVelocity$

    \If{$robotLocation$ == $robotDestination$}{
        $robotDestination{\longleftarrow}$ $GenerateNextPathLocation()$\\
        $SetRobotVelocity()$
    }
    \While{obstacleIndex $<$ numObs}{

    obsDistance $\longleftarrow$$GetDistance$(robotLocation,...\\
        ...$ObsLocation(ObsIndex)$)\\

     \If{ObsDistance$<$robotRange}{

    obs_{path}{\longleftarrow}$PathFinder$(robotLocation,obsLocation)\\
    \If{ObstacleFree(Obs_{path})}{
    \If{IsPathBlocked(obsIndex)}{
    $Replan \longleftarrow $TRUE$ $ \\}
    SetObsVisible(obsIndex)\\
    }

     }


   }

\end{algorithm}

答案1

我已修复错误:永远不要忽视它们。

我还为多字母标识符添加了格式;也许您想区分变量和函数,但我无法在您的代码中区分它们。

\documentclass{article}
\usepackage{algorithm2e}
\usepackage{amsmath}

\newcommand{\var}[1]{\mathit{#1}}
%\newcommand{\fun}[1]{\mathrm{#1}}

\begin{document}

\begin{algorithm}
    \caption{UpdateRobotLocation}
    \label{alg:r2p}

    $\var{robotLocation}\longleftarrow \var{robotLocation} + \var{robotVelocity}$

    \If{$\var{robotLocation} == \var{robotDestination}$}{
        $\var{robotDestination}\longleftarrow\var{GenerateNextPathLocation()}$\\
        $\var{SetRobotVelocity()}$
    }
    \While{$\var{obstacleIndex} < \var{numObs}$}{

    $\var{obsDistance} \longleftarrow \var{GetDistance}(\var{robotLocation},\dotsc$ \\
        $\dots\var{ObsLocation}(\var{obsIndex}))$\\

     \If{$\var{ObsDistance} < \var{robotRange}$}{

    $\var{obs_{path}} \longleftarrow \var{PathFinder}(\var{robotLocation},\var{obsLocation})$\\
    \If{$\var{ObstacleFree}(\var{Obs_{path}})$}{
    \If{$\var{IsPathBlocked}(\var{obsIndex})$}{
    $\var{Replan} \longleftarrow \mathrm{TRUE} $}
    $\var{SetObsVisible}(\var{obsIndex})$}
    }
}

\end{algorithm}

\end{document}

在此处输入图片描述

相关内容