1

Using algorithm2e currently when I do this

\If{ condition_1  OR condition_2 OR condition_3}

But if the stuff between {} is too long, it wraps around and does not look good since there is no alignment.

I looked at Multiple conditions in a "If" algorithm but did not see a solution there.

Is there a way to break each condition on its line, so it show up at end as follows

IF condition_1
      OR
   condition_2
      OR
   condition_3  THEN
   ....
END IF

Here is a MWE, showing 2 conditions only as an example.

\documentclass{article}
\usepackage{amsmath,amssymb}

\usepackage{algorithmic}
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}
\begin{algorithm}
    \DontPrintSemicolon 

    \KwIn{1st order ODE}
    \KwOut{solution}

    \eIf{ A}
    {
       stuff
       \eIf{  B}
       {
          stuff
       }
       {
           \uIf{ condition 1  \textbf{or} condition 2  \textbf{or}  condition 3 }
           {
                stuff
           }
           \uElseIf{ condition 1  \textbf{and} condition 2 \textbf{and} condition 3 }
           {
             stuff
           }
          \ElseIf{  condition 1  \textbf{or} condition 2 \textbf{and} condition 3  }
          {
              stuff
          }
       }
    }
    {
      stuff
    }
    \Return solution

    \caption{my algorithm}

\end{algorithm}

\end{document}

Compiled using lualatex, this is the output

enter image description here

btw, I could not find \Or , \And in algorithm2e so I am for now using \textbf{or} in place.

Trials

Tried manual fix, which almost works, like this

\documentclass{article}
\usepackage{amsmath,amssymb}

\usepackage{algorithmic}
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}
\begin{algorithm}
    \DontPrintSemicolon 

    \KwIn{1st order ODE}
    \KwOut{solution}

    \eIf{ A}
    {
       stuff
       \eIf{  B}
       {
          stuff
       }
       {
           \uIf{ condition 1 \; 
                \hspace{30pt}\textbf{or}\; 
                \hspace{15pt} condition 2\; 
                \hspace{30pt}\textbf{or}\;
                \hspace{15pt}condition 3 }
           {
                stuff
           }
           \uElseIf{ condition 1  \textbf{and} condition 2 \textbf{and} condition 3 }
           {
             stuff
           }
          \ElseIf{  condition 1  \textbf{or} condition 2 \textbf{and} condition 3  }
          {
              stuff
          }
       }
    }
    {
      stuff
    }
    \Return solution    
    \caption{my algorithm}    
\end{algorithm}    
\end{document}

Which now looks like

enter image description here

The vertical lines ruled now do not look good as not have gaps.

Any better solution to this? I could ofcourse just turn off the vertical lines using \SetAlgoNoLine and now it looks OK. But it is nice to have the vertical lines there if possible.

enter image description here

TL 2020

1

I am not familiar with algorithm2e enough, hence the following approach is not perfect. This attempt uses the macros in algorithm2e documentation, sec. 11.

Small example

\documentclass{article}
\usepackage{amsmath,amssymb}

\usepackage{algorithmic}
\usepackage[linesnumbered]{algorithm2e}

\begin{document}

\SetKw{Or}{\hspace{\algoskipindent}\itshape or\;}
\SetKw{And}{\hspace{\algoskipindent}\itshape and\;}
\SetKwBlock{Condition}{}{}

\begin{algorithm}
  \DontPrintSemicolon
  \If{condition 1 \Condition{\Or condition 2 \;\Or condition 3 \;\And condition 4}}
  {
    stuff
  } 
\end{algorithm}

\end{document}

enter image description here

Applied to your example

\documentclass{article}
\usepackage{amsmath,amssymb}

\usepackage{algorithmic}
\usepackage[linesnumbered,ruled]{algorithm2e}

\SetKw{Or}{\hspace{\algoskipindent}\itshape or\;}
\SetKw{And}{\hspace{\algoskipindent}\itshape and\;}
\SetKwBlock{Condition}{}{}

\begin{document}
\begin{algorithm}
    \DontPrintSemicolon 

    \KwIn{1st order ODE}
    \KwOut{solution}

    \eIf{ A}
    {
       stuff
       \eIf{  B}
       {
          stuff
       }
       {
           \uIf{condition 1 \Condition{\Or condition 2 \;\Or condition 3}}
           {
                stuff
           }
           \uElseIf( // the indent is not satisfying){condition 1 \Condition{\And condition 2 \;\And condition 3}}
           {
             stuff
           }
          \ElseIf{\Condition{condition 1  \;\Or condition 2 \;\And condition 3}}
          {
              stuff
          }
       }
    }
    {
      stuff
    }
    \Return solution

    \caption{my algorithm}
\end{algorithm}

\end{document}

enter image description here

| improve this answer | |

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.