![算法中未编号的行](https://linux22.com/image/266026/%E7%AE%97%E6%B3%95%E4%B8%AD%E6%9C%AA%E7%BC%96%E5%8F%B7%E7%9A%84%E8%A1%8C.png)
我的问题类似于这个,我需要跳过算法中某些行的数字。但是该问题的解决方案对我来说不起作用。如何使用包禁用某些行的数字algorithmic
?
这是我尝试过的:
\documentclass{article}
\usepackage{algorithmic}
\usepackage{algorithm}
\def\NoNumber#1{{\def\alglinenumber##1{}\State #1}\addtocounter{ALG@line}{-1}}
\begin{document}
\begin{algorithmic}[1]
\STATE Compute $Pr(X_i < x)$ and $Pr(x < X_i)$ for all $i = 1, \dots, M$ and
\FOR {$i = 1, \dots, n$}
\STATE with a number
\NoNumber{ without number}
\ENDFOR
\end{algorithmic}
\end{document}
答案1
一个选择是使用更通用的algorithmicx
包,及其algcompatible
格式(允许您使用algorithmic
语法);您可以 \STATEx
对未编号的行使用:
\documentclass{article}
\usepackage{algcompatible}
\begin{document}
\begin{algorithmic}[1]
\IF{some condition is true}
\STATE do some processing
\ELSIF{some other condition is true}
\STATEx do some different processing
\ELSIF{some even more bizarre condition is met}
\STATEx do something else
\ELSE
\STATE do the default actions
\ENDIF
\end{algorithmic}
\end{document}
当然,仅当您已经使用语法编写了算法,并且想要在不造成重大创伤的情况下切换到时algcompatible
,才建议使用;如果您刚开始编写算法,那么从一开始就使用格式:algorithmic
algorithmicx
algpseudocode
\documentclass{article}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithmic}[1]
\If{some condition is true}
\State do some processing
\ElsIf{some other condition is true}
\Statex do some different processing
\ElsIf{some even more bizarre condition is met}
\Statex do something else
\Else
\State do the default actions
\EndIf
\end{algorithmic}
\end{document}
答案2
添加一个空行作为换行符,然后在下一行写入您想要的任何内容:
\documentclass{article}
\usepackage{algorithmic}
\usepackage{algorithm}
\def\NoNumber#1{{\def\alglinenumber##1{}\State #1}\addtocounter{ALG@line}{-1}}
\begin{document}
\begin{algorithmic}[1]
\STATE Compute $Pr(X_i < x)$ and $Pr(x < X_i)$ for all $i = 1, \dots, M$ and
\FOR {$i = 1, \dots, n$}
\STATE with a number
without number
\ENDFOR
\end{algorithmic}
\end{document}