如何在 Latex 伪代码中使用 if else。以下是代码
\documentclass{article}
\usepackage[utf8]{inputenc}
\renewcommand{\algorithmicrequire}{\textbf{Input:}} % Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm
\usepackage{algorithm}
\usepackage{algpseudocode}
\begin{document}
\begin{algorithm}[htb]
\caption{}
\label{alg:algorithm1}
\begin{algorithmic}[1]
\If{$R != Null$}
\For{each $R_i\in R$}
\State{Score(i) = Evaluation($R_i$)}
\Else{111}
%\State{$D_n$ = $R$}
\EndIf\\
\Return{$D_n$}
\end{algorithmic}
\end{algorithm}
\end{document}
我想让“else”显示在伪代码中。并且它应该具有与“if”相同的类。目前,我得到的只是 111,而不是 else 111。如何优化它?
答案1
正如我的评论所建议的,正确的代码是:
\documentclass{article}
\usepackage{algorithm}
\usepackage{algpseudocode}
\renewcommand{\algorithmicrequire}{\textbf{Input:}}
\renewcommand{\algorithmicensure}{\textbf{Output:}}
\begin{document}
\begin{algorithm}[htb]
\caption{}
\label{alg:algorithm1}
\begin{algorithmic}
\If{$R \neq Null$}
\For{each $R_i\in R$}
\State{Score(i) = Evaluation($R_i$)}
\EndFor
\ElsIf{111}
\State{$D_n$ = $R$}
\EndIf
\State{\Return{$D_n$}}
\end{algorithmic}
\end{algorithm}
\end{document}