algorithm - 使用latex面对算法中ENDIF的问题

标签 algorithm latex ifs

我想在使用 LaTeX 的代码中使用 IF else。但我无法结束 if-else。我使用了 ENDIF 和类似 \newcommand\sIf[2]{\If{#1}#2\EndIf} 的命令。我想在 if-else 的末尾打印 END if 。以下是代码及其输出。

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}


\begin{algorithm}
\caption{Calculating Speed Using GPS}\label{alg1}
\begin{algorithmic}[1]
\State $distance\gets  0 $
\State $time\gets  0 $
\State $speedGPS  \gets  0 $


\If{$location.hasSpeed()$}
\State $speedGPS\gets location.getSpeed()$
\Else
\State $speedGPS \gets dist / time$
\EndIf


\State $counter  \gets (counter + 1) \% data_points$
\State $speedGPS  \gets speedGPS * 3.6d$
\end{algorithmic}
\end{algorithm}


\end{document}

enter image description here

最佳答案

删除要传递给 algpseudocodenoend 选项:

enter image description here

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\begin{document}

\begin{algorithm}
  \caption{Calculating Speed Using GPS}
  \begin{algorithmic}[1]
    \State $distance\gets  0 $
    \State $time\gets  0 $
    \State $speedGPS  \gets  0 $
    \If{$location.hasSpeed()$}
      \State $speedGPS\gets location.getSpeed()$
    \Else
      \State $speedGPS \gets dist / time$
    \EndIf
    \State $counter  \gets (counter + 1) \% data_points$
    \State $speedGPS  \gets speedGPS \times 3.6d$
  \end{algorithmic}
\end{algorithm}

\end{document}

关于algorithm - 使用latex面对算法中ENDIF的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53572845/

相关文章:

c - 使用 LD_PRELOAD 和 IFS 利用 SUID 文件

arrays - 使用逗号和单引号连接数组元素 - Bash

algorithm - 使用动态规划将字符串拆分为一串有效单词

sql - 尽可能少地排列相邻值的数组

latex - Vim 根据文件内容自动执行命令

r - 使用knitr在beamer中叠加动画

latex - 为什么我的 Latex 文本会自行更改理由?

bash - 可以在 Bash 函数中本地更改 IFS 吗?

algorithm - 非重叠子区间的区间

c# - 找出一组属性之间最相似的(mongodb)