无法使用算法包关闭循环

无法使用算法包关闭循环

我有以下代码:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}

\begin{algorithm}
\caption{Main Ass}\label{alg:cap}
\begin{algorithmic}[1]
\State Create
\State Create 
\State Create 
\For{every $T_i$}
\If{$T_i$ is an }
    \If {$T_i$ is of}
    \If {$A \notin$}
    \State add A
    \EndIf
    \EndIf
\ElsIf {$T_i$ is a}
    \If {$T_i$ is}
    \If{A $\in$ DI\_L}
    \If{linked}
    \State Create a 
    \EndIf
    \State add t 
    \EndIf
\ElsIf{$T_i$ is a}
\If{$T_i$ is of $}
\If{A $\in$ }
\State add r
\State add A
\EndIf
\EndIf
\If{$T_i$}
\If{invalid}
\State add w
\ElsIf{$A$}
\State delete A
\EndIf
\EndIf
\If{$c_i$ is found } 
\State delete the
\ElsIf{$a_i$ is found}
\State delete
\EndIf
\EndIf
\Endfor //talking about this for loop not closing, that is line 47
\State Send F
\State Send 
\end{algorithmic} \\that is line 50
\end{algorithm}


\begin{algorithm}
\caption{Main Rec}\label{alg:cap2}
\begin{algorithmic}[1]
\For {each t} \\that is line 57
\State execute $T_i$

\end{algorithmic} \\that is line 60
\end{algorithm}

\end{document}

显然,我删除了大部分代码行以使事情变得更清晰,即使我关闭了 for 循环,视觉变化也没有反映出来,而且我在编译时遇到了多个错误,其余的在视觉上看起来很好

我得到以下信息(我注释了代码来标记每个错误所指的行):

Missing number, treated as zero.

‪main.tex, 47‬
<to be read again> 
                   \ALG@b@2@EndFor@0 
l.47 \EndFor
            
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)
Package algorithmicx Error: Some blocks are not closed!!!.

 
‪main.tex, 50‬
See the algorithmicx package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.50 \end{algorithmic}
                      
This error message was generated by an \errmessage
command, so I can't give any explicit help.
Pretend that you're Hercule Poirot: Examine all clues,
and deduce the truth by order and method.
Missing \endcsname inserted.

 
‪main.tex, 57‬
<to be read again> 
                   \ALG@currentblock@0 
l.57 \For
          {each transaction in the linked list}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.

! Extra \endcsname.
\ALG@makebeginrepeat ...\ALG@thisblock \endcsname 
                                                  \relax \def \ALG@thisblock...
l.57 \For
          {each transaction in the linked list}
I'm ignoring this, since I wasn't doing a \csname.
Package algorithmicx Error: Some blocks are not closed!!!.

 
‪main.tex, 60‬
See the algorithmicx package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.60 \end{algorithmic}
                      
(That was another \errmessage.)

任何帮助都将不胜感激,我已经尝试确保每个打开的块都是关闭的,而且看起来是这样,我怀疑使用包可能会让我有些困惑,但我似乎无法弄清楚

答案1

\If计算命令和相应命令的数量\EndIf。你会发现它们不匹配。

如果您正确缩进嵌套结构,您就会看到。

我不知道在哪里准确添加缺失的\EndIf。这是一种可能性,请修复它。

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}

\begin{algorithm}
\caption{Main Ass}\label{alg:cap}
\begin{algorithmic}[1]
\State Create
\State Create 
\State Create 
\For{every $T_i$}
  \If{$T_i$ is an }
    \If {$T_i$ is of}
      \If {$A \notin$}
        \State add A
      \EndIf
    \EndIf
  \ElsIf {$T_i$ is a}
    \If {$T_i$ is}
      \If{A $\in$ DI\_L}
        \If{linked}
          \State Create a 
        \EndIf
          \State add t 
      \EndIf
    \ElsIf{$T_i$ is a}
      \If{$T_i$ is of}
        \If{A $\in$ }
          \State add r
          \State add A
        \EndIf
      \EndIf
      \If{$T_i$}
        \If{invalid}
          \State add w
        \ElsIf{$A$}
          \State delete A
        \EndIf
      \EndIf
    \EndIf % <--- Here?
    \If{$c_i$ is found } 
      \State delete the
    \ElsIf{$a_i$ is found}
      \State delete
    \EndIf
  \EndIf
\EndFor
\State Send F
\State Send 
\end{algorithmic}
\end{algorithm}

\end{document}

相关内容