[Tex/LaTex] Algorithmic: Put If and EndIf into same line

algorithmicalgorithms

I use the algorithmic package to create a pseudo-code-source in latex. I have a source with many short if-statements. The fact that the END (from EndIf) is put in a new line every time leads to a lengthy layout:

\documentclass[11pt,a4paper,leqno,fleqn]{article}
\usepackage{algpseudocode}
\usepackage[Algorithmus]{algorithm}
\begin{document}
\begin{algorithm}
\caption{WHILE-Modulo}
\begin{algorithmic}[1]
\Require f($x_1,x_2$)
\State $x_z := 1$
\While{$x_z \not = 0$}
    \If{$x_z = 1$} \If{$x_1 = 0$} $x_z = 10$ \EndIf \EndIf
    \If{$x_z = 2$} $x_3 := x_1 \dot{-}x_2$\EndIf
    \If{$x_z = 3$} $x_4 := x_2 \dot{-}x_1$\EndIf
    \If{$x_z = 4$} \If{$x_3 = 0$} $x_z = 7$\EndIf \EndIf
    \If{$x_z = 5$} $x_1 := x_3$\EndIf
    \If{$x_z = 6$} $x_z = 2$\EndIf
    \If{$x_z = 7$} \If{$x_4 = 0$} $x_z = 9$\EndIf \EndIf
    \If{$x_z = 8$} $x_z = 10$\EndIf
    \If{$x_z = 9$} $x_1 := x_1 \dot{-} x_2$\EndIf
    \If{$x_z = 10$} $x_z = 0$\EndIf
    \If{$x_z = 11$} $x_z = 0$\EndIf
\EndWhile
\end{algorithmic}
\end{algorithm}
\end{document}

leads to this output:

enter image description here

However, I want single If-Statements to be in one line, similar to this:

enter image description here

Do you have ideas? I am grateful for all input

Best Answer

You can create your own "Intermediate IF" (or one-liner IF) statement using the following:

enter image description here

\documentclass{article}
\usepackage{algpseudocode}
\usepackage[Algorithmus]{algorithm}
\algnewcommand{\IIf}[1]{\State\algorithmicif\ #1\ \algorithmicthen}
\algnewcommand{\EndIIf}{\unskip\ \algorithmicend\ \algorithmicif}
\begin{document}
\begin{algorithm}
  \caption{WHILE-Modulo}
  \begin{algorithmic}[1]
    \Require f($x_1,x_2$)
    \State $x_z := 1$
    \While{$x_z \not = 0$}
      \If{$x_z = 1$} \IIf{$x_1 = 0$} $x_z = 10$ \EndIIf \EndIf
      \IIf{$x_z = 2$} $x_3 := x_1 \dot{-}x_2$\EndIIf
      \IIf{$x_z = 3$} $x_4 := x_2 \dot{-}x_1$\EndIIf
      \If{$x_z = 4$} \IIf{$x_3 = 0$} $x_z = 7$\EndIIf \EndIf
      \IIf{$x_z = 5$} $x_1 := x_3$\EndIIf
      \IIf{$x_z = 6$} $x_z = 2$\EndIIf
      \If{$x_z = 7$} \IIf{$x_4 = 0$} $x_z = 9$\EndIIf \EndIf
      \IIf{$x_z = 8$} $x_z = 10$\EndIIf
      \IIf{$x_z = 9$} $x_1 := x_1 \dot{-} x_2$\EndIIf
      \IIf{$x_z = 10$} $x_z = 0$\EndIIf
      \IIf{$x_z = 11$} $x_z = 0$\EndIIf
    \EndWhile
  \end{algorithmic}
\end{algorithm}
\end{document}
Related Question