如何在算法中添加评论

如何在算法中添加评论

如何根据包在以下算法中添加注释algorithmic

 \documentclass[10pt,conference]{IEEEtran} 
 \IEEEoverridecommandlockouts

 \usepackage{cite}
 \usepackage{amsmath,amssymb,amsfonts}
 \usepackage{algorithmic}
 \usepackage{algorithm}
 \usepackage{graphicx}
 \usepackage{changepage}
 \usepackage{lipsum}
 \usepackage{ctable}% http://ctan.org/pkg/ctable
 \usepackage{color, colortbl}
 \newcommand*\ita[1]{\textit{#1}}
 \usepackage{xcolor}                             % changed
 \usepackage{array, booktabs, makecell, multirow}% new
 \definecolor{Gray}{gray}{0.9}
 \usepackage{textcomp}
\usepackage{xcolor}

\usepackage{xcolor}                      % changed
\usepackage{array, booktabs, makecell, multirow}% new
\usepackage{multirow}
\newcommand{\var}{\textit}
\newcommand{\proc}{\textbf}
\newcommand{\prop}{\texttt}
\newcommand{\plusplus}{{+}{+}}% Other options: 
 \begin{document}
\begin{algorithm}
\caption{Step 2-N Propagation}

\begin{algorithmic}[1]

    \raggedright
    \STATE boolean modified=true
    \WHILE{modified==true}
    \STATE modified=false

    \FORALL{r in Requirements}   
    \FORALL{m in Methods}   

    \IF {m.trace[r]==E \AND  
        (m.interfaces.trace[r].AllNs()  \OR 
 m.implementations.trace[r].AllNs() \OR m.parents.trace[r].AllNs() \OR
        m.children.trace[r].AllNs())
    }
    %\STATE\proc{//Inheritance}
    \STATE m.trace[r]=N
    \STATE modified=true




    \ENDIF 
    \ENDFOR 
    \ENDFOR 

    \FORALL{r in Requirements}   
    \FORALL{m in Methods}   

    \IF {m.trace[r]==E \AND  
        %!m.XCallers.isEmpty()  
        %\AND
         m.XCallers.trace[r].AllNs())
    }
%   \STATE\proc{//All XCallers N}
    \STATE m.Trace[r]=N
    \STATE modified=true




    \ENDIF 
    \ENDFOR 
    \ENDFOR 

    \FORALL{r in Requirements}   
    \FORALL{m in Methods}   

    \IF {m.trace[r]==E \AND  
        %!m.XCallees.isEmpty()  
        %\AND 
        m.XCallees.trace[r].AllNs())
    }
%   \STATE\proc{//All XCallees N}
    \STATE m.trace[r]=N
    \STATE modified=true




    \ENDIF 
    \ENDFOR 
    \ENDFOR 
    \ENDWHILE
\end{algorithmic}
\label{step2}
\end{algorithm}
\end{document}

我希望我的算法看起来像我想插入语句//Inheritance旁边的\if那样。我希望我的算法看起来像这样:

在此处输入图片描述

答案1

好吧,使用您使用的包,您可以使用命令\COMMENT在行中添加注释,如果您希望注释在右侧结束,请使用\hfill\COMMENT

请看此处的示例(请注意,我最小化了给定的算法,并将调用的包减少到此问题所需的包):

\documentclass[10pt,conference]{IEEEtran} 
\IEEEoverridecommandlockouts

\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithmic}
\usepackage{algorithm}
\usepackage[table]{xcolor} % <==========================================


\begin{document}

\begin{algorithm}
\caption{Step 2-N Propagation}

\begin{algorithmic}[1]
\raggedright
\STATE boolean modified=\TRUE          \hfill\COMMENT{this is a comment}
\WHILE{modified==\TRUE}
  \STATE modified=\FALSE
  \FORALL{r in Requirements}
    \FORALL{m in Methods}
      \IF {m.trace[r]==E \AND              \hfill\COMMENT{//Inheritance}
          (m.interfaces.trace[r].AllNs()  \OR 
           m.implementations.trace[r].AllNs() \OR m.parents.trace[r].AllNs() \OR
           m.children.trace[r].AllNs())
      }
        \STATE m.trace[r]=N
        \STATE modified=\TRUE
      \ENDIF 
    \ENDFOR 
  \ENDFOR 
\ENDWHILE
\end{algorithmic}
\label{step2}
\end{algorithm}

\end{document}

及其结果:

结果一

根据您所显示的图像,您可以使用包algorithmicxalgpseudocode获得与您所显示的图像类似的东西。

查看新代码

\documentclass[10pt,conference]{IEEEtran} 
\IEEEoverridecommandlockouts

\usepackage{amsmath,amssymb,amsfonts}
\usepackage{algorithm}
\usepackage{algorithmicx} % <===========================================
\usepackage{algpseudocode} % <==========================================

\usepackage[table]{xcolor} % <==========================================


\begin{document}

\begin{algorithm}
\flushleft
  \caption{Step 2-N Propagation, Version 2}
  \label{alg:version2}
  \begin{algorithmic}[1] % The number tells where the line numbering should start
    \State $modified \gets true$             \Comment{this is a comment}
    \While{$modified == true$}               \Comment{this is a comment}
      \State $modified \gets false$
      \For{$r \in Requirements$}
        \If{$m.trace[r]==E$  
          \textbf{and} \hfill\Comment{//Inheritance} 
                      $m.interfaces.trace[r].AllNs()$ 
          \textbf{or} $m.implementations.trace[r].AllNs()$} 
          \State $m.trace[r] \gets N$
          \State $modified \gets true$
        \EndIf
      \EndFor
    \EndWhile
  \end{algorithmic}
\end{algorithm}
\end{document}

及其结果:

结果二

请查看插入的带有命令的注释\hfill\Comment(红色箭头,标记为 1)并查看不同的缩进(红色箭头 2)。请注意,您必须为第二个代码完全重写算法!请在texdoc algorithmicx您的终端上阅读相应的文档...

答案2

有一个用于评论的宏:\COMMENT{...}

 \documentclass[10pt,conference]{IEEEtran} 
   \IEEEoverridecommandlockouts

  \usepackage{cite}
  \usepackage{amsmath,amssymb,amsfonts}
  \usepackage{algorithmic}
  \usepackage{algorithm}
  \usepackage{graphicx}
  \usepackage{changepage}
 \usepackage{lipsum}
 \usepackage{ctable}% http://ctan.org/pkg/ctable
 \usepackage{color, colortbl}
 \newcommand*\ita[1]{\textit{#1}}
 \usepackage{xcolor}                             % changed
 \usepackage{array, booktabs, makecell, multirow}% new
 \definecolor{Gray}{gray}{0.9}
 \usepackage{textcomp}
\usepackage{xcolor}

\usepackage{xcolor}                      % changed
\usepackage{array, booktabs, makecell, multirow}% new
\usepackage{multirow}
\newcommand{\var}{\textit}
\newcommand{\proc}{\textbf}
\newcommand{\prop}{\texttt}
\newcommand{\plusplus}{{+}{+}}% Other options: 

 \renewcommand{\algorithmiccomment}[1]{//#1}

 \begin{document}
\begin{algorithm}
\caption{Step 2-N Propagation}

\begin{algorithmic}[1]

    \raggedright
    \STATE boolean modified=true \COMMENT{this is a comment}
    \WHILE{modified==true}
    \STATE modified=false

    \FORALL{r in Requirements}   
    \FORALL{m in Methods}   

    \IF {m.trace[r]==E \AND  
        (m.interfaces.trace[r].AllNs()  \OR 
 m.implementations.trace[r].AllNs() \OR m.parents.trace[r].AllNs() \OR
        m.children.trace[r].AllNs())
    }
    %\STATE\proc{//Inheritance}
    \STATE m.trace[r]=N
    \STATE modified=true




    \ENDIF 
    \ENDFOR 
    \ENDFOR 

    \FORALL{r in Requirements}   
    \FORALL{m in Methods}   

    \IF {m.trace[r]==E \AND  
        %!m.XCallers.isEmpty()  
        %\AND
         m.XCallers.trace[r].AllNs())
    }
%   \STATE\proc{//All XCallers N}
    \STATE m.Trace[r]=N
    \STATE modified=true




    \ENDIF 
    \ENDFOR 
    \ENDFOR 

    \FORALL{r in Requirements}   
    \FORALL{m in Methods}   

    \IF {m.trace[r]==E \AND  
        %!m.XCallees.isEmpty()  
        %\AND 
        m.XCallees.trace[r].AllNs())
    }
%   \STATE\proc{//All XCallees N}
    \STATE m.trace[r]=N
    \STATE modified=true




    \ENDIF 
    \ENDFOR 
    \ENDFOR 
    \ENDWHILE
\end{algorithmic}
\label{step2}
\end{algorithm}
\end{document}

在此处输入图片描述

相关内容