我正在尝试创建一个if elseif else
算法,但它却抛出了错误。
错误提示:!Argument of \algosec@Elseselfstrip has an extra }.<inserted text>
但看不到我}
在任何地方插入了额外的内容
软件包:
\usepackage[ruled]{algorithm2e}
乳胶代码:
\begin{algorithm}[H]
\While{true:}
{
pl = Poll() \\
sd = PollSd() \\
\eIf{time}{
ok;
}
\ElseIf{response}{
ok
}{
instructions1\;
instructions2\;
}
}
\caption{Value}
\end{algorithm}
答案1
您使用的是 elsed if,正如名称\eIf
所示。因此它需要一个 else 语句
\documentclass[]{article}
\usepackage[ruled]{algorithm2e}
\begin{document}
\begin{algorithm}[H]
\While{true:}
{
pl = Poll() \\
sd = PollSd() \\
\eIf{time}{
ok;
} %<----- Here it expects an else block, basically anything
\ElseIf{response}{
ok
}{
instructions1\;
instructions2\;
}
}
\caption{Value}
\end{algorithm}
\end{document}
您可以使用不同的宏,例如\uIf
等,使其成为单例,如果后面跟着\ElseIf
答案2
\begin{algorithm}[H]
\SetAlgoLined
\LinesNumbered
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}
\Input{Input}
\Output{Output}
\uIf{this first condition is true}{
we do that\;
}
\uElseIf{this other condition is true}{
this is done\tcc*[r]{else if}
}
\Else{
in other case, we do this\tcc*[r]{else}
}
\caption{Algorithm}
\end{algorithm}