Algorithmic: Put If, Else and EndIf into the same line

algorithmic

I am reading this post how \If{cond} \State{statement} \EndIf can be placed in the same line. Could you please someone suggest a way on how \If{cond} \State{statement} \Else \State{statement} \EndIf can be placed in the same line?

Best Answer

Add

\algnewcommand{\ElseIIf}[1]{\algorithmicelse\ #1}

then input

\IIf{$x_1 = 0$} $x_z = 10$ \ElseIIf{$x_z = -1$}\EndIIf

will generate output

enter image description here

Full example, based on Werner's answer 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}

enter image description here

Related Question