[Tex/LaTex] undefined control sequences for algorithm2e package

algorithm2ealgorithmicalgorithms

I have some major and some minor issues while using algorithm2e.

For major issues, although the package itself defined some controls such as \input , \Fn etc., but when it comes to use them, I receive and error indicating these controls are not defined.

For minor issues, taking into account the following MWE, first of all the if clause does not have and ending which I wonder how I should fix it and secondly, for each line of comment, there is a weird ; at the beginning of the line:

    \documentclass{article}

\usepackage{algorithm2e}
%\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{pifont}
\usepackage{program}
\begin{document}
\IncMargin{1em}
\begin{algorithm}[H]

%\Input{A bitmap $Im$ of size $w\times l$}
%\Output{A partition of the bitmap}

  \uIf{domain.equals(keyword) AND id=0}{
  \tcc*[r]{occurrence at the root node (first layer)}
      setAttribute(D,PN)\; 
   }\uElseIf{domain.equals(keyword) AND $id\not=0$}{ \tcc*[r]{occurrence at the second layer}
        setAttribute(D,SL)\;
  }
  \uElseIf{path.equals(keyword) AND id=0}{ \tcc*[r]{occurrence at path of root node}
    setAttribute($P_a$, PN)\;
    }

 \caption{How to write algorithms}
\end{algorithm}
\end{document} 

Best Answer

In its documentation, package algorithm2e uses macro \SetKwInOut to define \Input and \Output. The following example also uses \If instead of \uIf to get the end markers (not sure, whether I have the question correctly). Also normal comments are used to get rid of the semicolon.

\documentclass{article}
\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{pifont}
\usepackage{program}
\begin{document}
\IncMargin{1em}
\begin{algorithm}[H]
  \SetKwInOut{Input}{input}
  \SetKwInOut{Output}{output}

  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \BlankLine

  \uIf{domain.equals(keyword) AND id=0}{
    \tcc{occurrence at the root node (first layer)}
    setAttribute(D,PN)\;
   }\uElseIf{domain.equals(keyword) AND $id\not=0$}{
      \tcc{occurrence at the second layer}
      setAttribute(D,SL)\;
  }
  \ElseIf{path.equals(keyword) AND id=0}{
    \tcc{occurrence at path of root node}
    setAttribute($P_a$, PN)\;
    }

 \caption{How to write algorithms}
\end{algorithm}
\end{document}

Result

Right aligned comments are not my taste. The indentation of source code provides information about the structure, this is destroyed by comments moved to the right.

The example inserts \hspace{0pt plus 1filll} to move the comments.

\documentclass{article}
\usepackage[linesnumbered,lined,boxed,commentsnumbered]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{pifont}
\usepackage{program}
\newcommand\HFilll{\hspace{0pt plus 1filll}}
\begin{document}
\IncMargin{1em}
\begin{algorithm}[H]
  \SetKwInOut{Input}{input}
  \SetKwInOut{Output}{output}

  \Input{A bitmap $Im$ of size $w\times l$}
  \Output{A partition of the bitmap}
  \BlankLine

  \uIf{domain.equals(keyword) AND id=0}{
    \HFilll\tcc{occurrence at the root node (first layer)}
    setAttribute(D,PN)\;
   }\uElseIf{domain.equals(keyword) AND $id\not=0$}{
      \HFilll\tcc{occurrence at the second layer}
      setAttribute(D,SL)\;
  }
  \ElseIf{path.equals(keyword) AND id=0}{
    \HFilll\tcc{occurrence at path of root node}
    setAttribute($P_a$, PN)\;
    }

 \caption{How to write algorithms}
\end{algorithm}
\end{document}

Result