算法左边距和评论自定义

问题描述

我正在尝试节省空间,减少 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...