算法左边距和评论自定义

Algorithm left margin and Comment Customizing

我正在尝试保存 space,减少 1 中算法的两个边距,并在第 1 行的“do”之后添加注释(请参阅红色问题)。我所有的尝试都失败了。我只能将注释放在“SemCompositeIndex”和“do”之间。 其可编译代码如下:

\documentclass{article}
\usepackage{mdframed}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}

\makeatletter
 %Remove right hand margin in algorithm
\patchcmd{\@algocf@start}% <cmd>
  {-1.5em}% <search>
  {0pt}% <replace>
{}{}% <success><failure>
\makeatother

\begin{document}

    \begin{algorithm}
    \SetAlgoLined
    \SetKwInOut{Input}{input}
    \SetKwInOut{Output}{output}
    \Input{SemCompositeIndex \tcp*[f]{XXX}(a)}
    \Output{CSemCompositeIndex \tcp*[f]{YYY}(b)}
    
    \ForEach{entry $\in$ SemCompositeIndex \tcp*[f]{XXX}}{
        CSemCompositeIndex.put(entry.compositeKey, compressMatchCounter(entry.matchCounter)) \tcp*[f]{ZZZ}}
    
     \caption{Compress}
     \label{alg:compress}
    \end{algorithm}

\end{document}

我是新来的。让我知道是否需要提供更多详细信息!

非常感谢您的关注和参与。

祝大家新年快乐!

这是使用algorithm2e时算法的默认布局:

\documentclass{article}

\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}

%\setlength{\algomargin}{0pt}
\begin{document}

\begin{algorithm}[H]
  \SetAlgoLined
  \KwData{this text}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

您可以使用 \setlength{\algomargin}{<len>} 更改页边距。例如,这是 \setlength{\algomargin}{0pt} 的样子:

默认值(上图第一张)为 \leftskip + \parindent

要在 for 循环的条件之后放置注释,可以使用 \ForEach(\tcp*[f]{XXX}){...}{....}

\documentclass{article}
\usepackage{mdframed}
\usepackage[noend,linesnumbered,ruled,vlined]{algorithm2e}

\setlength{\algomargin}{15pt} %@Werner Solution for left margin

\makeatletter
 %Remove right hand margin in algorithm
\patchcmd{\@algocf@start}% <cmd>
  {-1.5em}% <search>
  {0pt}% <replace>
{}{}% <success><failure>
\makeatother

\begin{document}

    \begin{algorithm}
    \SetAlgoLined
    \SetKwInOut{Input}{input}
    \SetKwInOut{Output}{output}
    \Input{SemCompositeIndex \tcp*[f]{XXX}(a)}
    \Output{CSemCompositeIndex \tcp*[f]{YYY}(b)}
    
    \ForEach(\tcp*[f]{XXX}){entry $\in$ SemCompositeIndex }{
        CSemCompositeIndex.put(entry.compositeKey, compressMatchCounter(entry.matchCounter)) \tcp*[f]{ZZZ}}
    
     \caption{Compress}
     \label{alg:compress}
    \end{algorithm}

\end{document}