我在读这发布如何\If{cond} \State{statement} \EndIf
将其放在同一行。您能建议一下如何\If{cond} \State{statement} \Else \State{statement} \EndIf
将其放在同一行吗?
答案1
添加
\algnewcommand{\ElseIIf}[1]{\algorithmicelse\ #1}
然后输入
\IIf{$x_1 = 0$} $x_z = 10$ \ElseIIf{$x_z = -1$}\EndIIf
将产生输出
完整示例,基于 Werner 的回答https://tex.stackexchange.com/a/184228
\documentclass{article}
\usepackage{algpseudocode}
\usepackage[Algorithmus]{algorithm}
\algnewcommand{\IIf}[1]{\State\algorithmicif\ #1\ \algorithmicthen}
\algnewcommand{\ElseIIf}[1]{\algorithmicelse\ #1} % <<< added
\algnewcommand{\EndIIf}{\unskip\ \algorithmicend\ \algorithmicif}
\begin{document}
\begin{algorithm}
\caption{Inline if-then-else}
\begin{algorithmic}[1]
\IIf{$x_1 = 0$} $x_z = 10$ \ElseIIf{$x_z = -1$}\EndIIf
\end{algorithmic}
\end{algorithm}
\end{document}