大家好,我是 LaTeX 新手,我在算法环境中编写了一个算法,如下所示:
\documentclass{report}
\usepackage{amsmath}
\usepackage{algorithm}
%\usepackage[noend]{algpseudocode}
\usepackage{algpseudocode}
\begin{document}
\makeatletter
\def\BState{\State\hskip-\ALG@thistlm}
\makeatother
\begin{algorithm}
\caption{Reduct Construction Algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Reduct Construction}{}\newline
\textbf{Input:} Three matrices based on indiscernibility, discernibility and equal to one relations.\newline
\textbf{Output:} Reducts (sentences)
\State {{\text{\,\,\,\,\,} \textbf{for} i = 2 to n \text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,\,\,\,} \textbf{for} j = 1 to n-1 \text{\,\,\,\,\,\,\,\,}}
\State {\text{\,\,\,\,\,\,\,\,\,\,\,} \textbf{if}$M(i,j) \neq \phi$ \text{\,\,\,\,\,\,\,\,\,\,\,}\textbf{then}
\State {{\text{\,\,\,\,\,} \textbf{for} every non-empty element M(i`,j`) \in B \text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,\,\,\,} \textbf{if} $M(i`,j`)\subset M(i,j)$ \text{\,\,\,\,\,\,\,\,}\textbf{then}}
\State {{\text{\,\,\,\,\,\,\,\,\,\,\,\,\,\,} $M(i,j) = M(i`,j`)$ \text{\,\,\,\,\,\,\,\,\,\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} Divide M(i,j) into two parts\text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} \textbf{Select} an attribute a from M(i,j); \text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} $A = M(i,j)-\{a\}$ \text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} $M(i,j)=\{a\}$ \text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} Simplify every non empty element in B \text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} \textbf{if} {$a \in M(i`,j`)$} \text{\,\,\,\,\,}\textbf{then}
\State {{\text{\,\,\,\,\,\,\,\,\,\,\,} $M(i`,j`)=\{a\}$ \text{\,\,\,\,\,\,\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} \textbf{Else} \text{\,\,\,\,\,}}
\State {{\text{\,\,\,\,\,} $M(i`,j`) = M(i`,j`)- A$ \text{\,\,\,\,\,}}
\end{algorithmic}
\end{algorithm}
\end{document}
我使用的软件包是:
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
有很多错误,虽然可视化是正确的,但当我第一次运行时没有生成 pdf,这很可悲,错误是
Package algorithmicx Error: Some blocks are not closed!!!. \end{algorithmic}
Missing } inserted. \end{algorithmic}
Missing \endcsname inserted. \Procedure
Missing $ inserted. ...{for} every non-empty element M(i`,j`) \in
Extra }, or forgotten $. ... element M(i`,j`) \in B \text{\,\,\,\,\,}}
我尝试纠正它们但失败了,任何帮助纠正这些错误都会很有帮助,谢谢
答案1
您滥用了该工具:
\documentclass{article}
\usepackage{amsmath}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Reduct Construction Algorithm}\label{euclid}
\begin{algorithmic}[1]
\Procedure{Reduct Construction}{}\newline
\textbf{Input:} Three matrices based on indiscernibility, discernibility and equal to one relations.\newline
\textbf{Output:} Reducts (sentences)
\For{$i = 2$ to $n$}
\For{$j = 1$ to $n-1$}
\If{$M(i,j) \neq \emptyset$}
\For{every non-empty element $M(i',j') \in B$}
\If{$M(i',j')\subset M(i,j)$}
\State{$M(i,j) = M(i',j')$}
\EndIf
\EndFor
\EndIf
\State{Divide $M(i,j)$ into two parts}
\State{\textbf{Select} an attribute $a$ from $M(i,j)$;}
\State{$A = M(i,j)-\{a\}$}
\State{$M(i,j)=\{a\}$}
\State{Simplify every non empty element in $B$}
\If{$a \in M(i',j')$}
\State{$M(i',j')=\{a\}$}
\Else
\State {$M(i',j') = M(i',j')- A$}
\EndIf
\EndFor
\EndFor
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
我不确定实现是否正是你所想的,但应该很容易调整。
请注意,数学符号应始终位于$
符号之间。要为符号添加撇号,请使用撇号'
,而不是反引号`