[Tex/LaTex] Problems with algorithm2e and \If

algorithm2e

I have tried to solve this problem following this tutorial, but with no success. I was using \If and \EndIf and now I have switched to \eIf but with no success. I'm using \; at the end of each line to avoid breaking the paragraph. What's wrong in my if?!

  1. http://ctan.mirror.garr.it/mirrors/CTAN/macros/latex/contrib/algorithm2e/doc/algorithm2e.pdf
  2. Misunderstood error with algorithm2e in LateX

     \begin{algorithm}
     \ForAll {Partition p}
        skip1=0
        \ForAll {Edges i=(a,b)}
            skip2=0
            \ForAll {bucket in p}
                j=0 % conta i caratteri
                 \ForAll{chr in bucket}
                    \eIf{ chr == a or chr == b}{
                        other2find = set(set((a,b,)) - set((chr,))) \; % give the missing element to find
                         skip1=1 \;
                         \ForAll {other char k} \;
                         \eIf{other2find \== k} {
                            unstableNumber=unstableNumber+1 \;
                             skip2=1 \;
                             break  \; % smette di cercare nei caratteri del bucket
                         \EndIf  \;
                        break  \; 
                      }
                    }{
                        j=j+1
                        }
                \EndFor         
                %\If{skip1=1} % se ho trovato almeno un nodo in un bucket, salto tutta la partizione
                %            % perche' tanto l'altro nodo sara da qualche parte ma non rendera di certo la partizione
                %            % non stabile
                %   break
                %tutto questo codice non serve perche' e incluso nel break dopo if(other2find)
            \EndFor 
        \EndFor 
        \If{skip2}
            break % non serve controllare tutti gli archi per sapere che una partizione non e' stabile
        \EndIf
     \If{!skip2}
        stablePartition.append(p)
        skip2=0
     \EndIf 
     \EndFor
     \end{algorithm}
    

Best Answer

You seem to be mixing your syntax between what is provided by algorithm2e and that of algorithmicx. The latter uses a \For{..} ... \EndFor and \If{..} ... \EndIf construction while the former uses \For{..}{...} and \If{..}{...} construction together with a \; line-terminating macro.

Here's an algorithm2e implementation:

enter image description here

\documentclass{article}
\usepackage{amsmath,algorithm2e}
\newcommand{\var}{\texttt}
\begin{document}

\begin{algorithm}
  \ForAll {Partition p}{%
    $\var{skip1} = 0$\;
    \ForAll {\text{edges} $i = (a,b)$}{%
      $\var{skip2} = 0$\;
      \ForAll {\text{bucket in $p$}}{%
        $j = 0$ % conta i caratteri
        \ForAll{\text{\var{chr} in bucket}}{%
          \eIf{\text{$\var{chr} = a$ or $\var{chr} = b$}}{
            $\var{other2find} = set(set((a,b,)) - set((chr,)))$\; % give the missing element to find
            $\var{skip1} = 1$\;
            \ForAll {\text{other char $k$}}{%
              \If{$\var{other2find} = k$}{%
                $\var{unstableNumber} = \var{unstableNumber} + 1$\;
                $\var{skip2} = 1$\;
                break\; % smette di cercare nei caratteri del bucket
              }
              break\;
            }
          }{
            $j = j + 1$\;
          }
        }
      }
    }
    \If{\var{skip2}}{%
      break\; % non serve controllare tutti gli archi per sapere che una partizione non e' stabile
    }
    \If{not \var{skip2}}{%
      stablePartition.append(p)\;
      $\var{skip2} = 0$\;
    }
  }
\end{algorithm}

\end{document}

Note that it would be advisable to define a macro for your variable definitions (like skip1, skip2, chr, ...) that you can use throughout your algorithms. You may want to do the same for function calls, or use \call. See the algorithm2e documentation for many more options/details.

Related Question