微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

算法左边距和评论自定义

如何解决算法左边距和评论自定义

我正在尝试节省空间,减少 1 中算法的两个边距,并在第 1 行的“do”之后添加注释(参见红色问题)。我所有的尝试都失败了。我只能将注释放在“SemCompositeIndex”和“do”之间。

Algorithm preview

其可编译代码如下:

\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 时算法的默认布局:

enter image description here

\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} 的样子:

enter image description here

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

,

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

\documentclass{article}
\usepackage{mdframed}
\usepackage[noend,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}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。