如何向 ELSIF 添加评论?

如何向 ELSIF 添加评论?

我在显示注释时遇到问题,我希望注释显示在第二个和第三个 if 语句中的“then”的右边,而我得到的结果却是这样的。

在此处输入图片描述

    \documentclass[sigconf,review]{acmart}
\usepackage{algorithmic}
\usepackage{fixltx2e}
\usepackage{algorithm}
\usepackage{enumitem}
\usepackage{booktabs, makecell, multirow, tabularx}
\newcommand{\mh}[1]{
  {\color{green!70!black} MH: #1}}
 \newcommand{\cmd}[1]{
  {\color{blue} CMD: #1}} 
  \newcommand{\rev}[1]{
  {\color{purple} REV: #1}} 

\newcommand\mytab[1]{\begin{tabular}[t]{@{}c@{}} #1 \end{tabular}}
\newcommand\mc[2]{\multicolumn{#1}{c}{#2}}
 \renewcommand{\algorithmiccomment}[1]{//#1}
%%
%% \BibTeX command to typeset BibTeX logo in the docs


%% Rights management information.  This information is sent to you
%% when you complete the rights form.  These commands have SAMPLE
%% values in them; it is your responsibility as an author to replace
%% the commands and values with those provided to you when you
%% complete the rights form.
%\setcopyright{acmcopyright}
%\copyrightyear{2018}
%\acmYear{2018}
%\acmDOI{10.1145/1122445.1122456}
%
%%% These commands are for a PROCEEDINGS abstract or paper.
%\acmConference[Woodstock '18]{Woodstock '18: ACM Symposium on Neural
%  Gaze Detection}{June 03--05, 2018}{Woodstock, NY}
%\acmBooktitle{Woodstock '18: ACM Symposium on Neural Gaze Detection,
%  June 03--05, 2018, Woodstock, NY}
%\acmPrice{15.00}
%\acmISBN{978-1-4503-9999-9/18/06}


%%
%% Submission ID.
%% Use this when submitting an article to a sponsored event. You'll
%% receive a unique submission ID from the organizers
%% of the event, and this ID should be used as the parameter to this command.
%%\acmSubmissionID{123-A56-BU3}

%%
%% The majority of ACM publications use numbered citations and
%% references.  The command \citestyle{authoryear} switches to the
%% "author year" style.
%%
%% If you are preparing content for an event
%% sponsored by ACM SIGGRAPH, you must use the "author year" style of
%% citations and references.
%% Uncommenting
%% the next command will enable that style.
%%\citestyle{acmauthoryear}

%%
\usepackage{titlesec}
\titlespacing{\subsection}{0pc}{0.5pc}{0pc}
\titlespacing{\subsubsection}{0pc}{0pc}{0pc}

\begin{document}  

\begin{algorithm}
\caption{Step 4-T Propagation}

\begin{algorithmic}[1]

    \raggedright



    \STATE \textbf{do}
    \FORALL{r in requirements}   
    \FORALL{m in methods}   
        \IF {rtm[m,r]==U}
    \IF {
        m.callees.isEmpty() \hfill\COMMENT{\textit{Leaf}}
        \newline
        \AND atLeast1T(rtm[m.callers,r])
    }
    \STATE rtm[m,r]=T

    \ELSIF {allTs(rtm[m.callers,r]))\hfill\COMMENT{\textit{Default}}}

    \STATE rtm[m,r]=T 
    \ELSIF {allTs(rtm[m.callees,r]))\hfill\COMMENT{\textit{Default}}}

    \STATE rtm[m,r]=T 
    \ENDIF 
\ENDIF 
    \ENDFOR 
    \ENDFOR 
    \STATE ~\textbf{until no more predictions are made}
\end{algorithmic}
\label{step4}
\end{algorithm}


\end{document}

答案1

algorithmic's\ELSIF[<comment>]{<condition>}有两个参数,第一个是可选的。下面我已更新\algorithmiccomment以符合您建议的风格,并且还使用了\REPEAT...\UNTIL{<condition>}子句来模拟您的逐字...直到子句。前者提供了正确的缩进,而您的却没有。

在此处输入图片描述

\documentclass[sigconf,review]{acmart}

\usepackage{algorithm,algorithmic}
\renewcommand{\algorithmiccomment}[1]{\hfill // \textit{#1}}

\newcommand{\rtm}[1][m,r]{\text{rtm\textsubscript{m}[\text{#1}]}}

\begin{document}

\begin{algorithm}
  \caption{Step 4-T Propagation}
  \begin{algorithmic}[1]
    \raggedright
    \REPEAT
      \FORALL{r in requirements}
        \FORALL{m in methods}
          \IF{$\rtm = U$}
            \IF{
              (m.callees.isEmpty() \COMMENT{Leaf}
              \newline
              \AND atLeast1T(\rtm[m.callers,r]))
            }
              \STATE $\rtm = T$
            \ELSIF[First comment]{allTs(\rtm[m.callers,r]))}
              \STATE $\rtm = T$
            \ELSIF[Second comment]{allTs(\rtm[m.callees,r]))}
              \STATE $\rtm = T$
            \ENDIF
          \ENDIF
        \ENDFOR
      \ENDFOR
    \UNTIL{no more predictions are made}
  \end{algorithmic}
\end{algorithm}

\end{document}

相关内容