5

How can I add comments in the following algorithm based on package 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}

I would like my algoritm to look like I would like to insert //Inheritance next to the \if statement. I would like to have my algorithm look like this:

enter image description here

0

2 Answers 2

6

Well, with your used packages you can use the command \COMMENT to add an comment to a line, if you want the comment to end on the right side use \hfill\COMMENT.

See an example here (please note that I minimalized your given algorithm and I reduced the called packages to that one needed for this issue):

\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}

and its result:

result one

Based on your shown image you can use package algorithmicx and algpseudocode to get something simular to your shown image.

See the new code

\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}

and its result:

result two

Please see the inserted comment with commands \hfill\Comment (red arrow, marked with 1) and see the different indention (red arrow 2). Please see that you have to rewrite the algorithmn completly for the second code! Please read the corresponding documentation with texdoc algorithmicx on your terminal ...

3

There is a macro for comments: \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}

enter image description here

2
  • is there a way to remove these braces around the comment? I would like the comment to look like this //... as opposed to this {//...} May 8, 2019 at 6:14
  • @user3406764 Please see my edited answer
    – user188399
    May 8, 2019 at 8:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .