8

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

  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – jub0bs Jun 10 '14 at 12:53
  • Sorry for the unconvenience, the source is now compileable – thunfischtoast Jun 10 '14 at 12:56
10

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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.