![为什么使用 algorithm2e 时我的“末端”没有对齐?](https://linux22.com/image/413345/%E4%B8%BA%E4%BB%80%E4%B9%88%E4%BD%BF%E7%94%A8%20algorithm2e%20%E6%97%B6%E6%88%91%E7%9A%84%E2%80%9C%E6%9C%AB%E7%AB%AF%E2%80%9D%E6%B2%A1%E6%9C%89%E5%AF%B9%E9%BD%90%EF%BC%9F.png)
我正在使用算法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}