1

我试图节省空间,减少 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}

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

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

祝我们大家新年快乐!

4

2 回答 2

2

要在 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}

在此处输入图像描述

于 2021-01-03T21:43:52.910 回答
1

这是使用时算法的默认布局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

于 2021-01-02T05:06:28.460 回答