如何修复乳胶中算法的对齐方式

How to fix alignment of algorithm in latex

我需要修复此 LaTeX 代码的对齐方式。

当我编译它时,输出的 ifforelse 语句对齐错误。

\begin{algorithm}[H]
       \caption{:The $ASP\_Tree$ algorithm}     
       \SetKwInOut{Input}{Input}
       \SetKwInOut{Output}{Output}   
       \Input{TV database and min\_sup }
         \Output{Bla bla bla}
         \KwData{bla bla bla} 
         \Procedure{Tree}{$TV_i, min\_sup$}
        {
            \State {$[Root] \leftarrow [NULL]$} \newline
            \For {$i=1$ to $N$} {\label{forins} \newline
                {
                %\Statex \Comment {\%Comments: insert TV values into the tree based on rules}
                        \State {$Root \leftarrow TV_1$}
                        \State {$i \leftarrow i+1$} \newline                        
                        \If {TV_i \textgreater Root}
                    \State Root \gets TV_i
                        \ELSIF {TV_i\leq Root}
                    \State i\gets i+1
                        \ELSE
                              \State do something interesting
                        \ENDIF
                        }
              \EndFor   
                            }
            \EndProcedure       
            \end{algorithm}     

你的问题在于你混合了不同包的符号,这在本质上是不兼容的。您应该选择或者 algorithm2e or algorithmicx(提供algpseudocodealgcompatible),而不是两者都选择。

这是您可能想要的 algorithm2e 实现:

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

\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output\,}
\SetKwInOut{Data}{Data}
\SetKwProg{Tree}{Tree}{}{EndTree}

\begin{document}

\begin{algorithm}[H]
  \caption{The ASP\_Tree algorithm}
  \Input{TV database and min\_sup}
  \Output{Bla bla bla}
  \Data{bla bla bla}
  \Tree{$TV_i, min\_sup$}{
    $[\text{Root}] \gets [\text{NULL}]$\;
    \For {$i = 1$ to $N$} {
      $\text{Root} \gets TV_1$\;
      $i \gets i + 1$\;
      \uIf {$TV_i > \textup{Root}$}{
        $\text{Root} \gets TV_i$\;}
      \uElseIf{$TV_i \leq \textup{Root}$}{
        $i \gets i+1$\;}
      \Else{
        do something interesting\;
      }
    }
  }
\end{algorithm}

\end{document}