2

I'm trying to save space, reduce both margins of the algorithm in 1, and add a comment after the "do" in line 1 (see the problem in red). All my tentatives failed. I only could place the comment between the "SemCompositeIndex" and the "do." Algorithm preview Its compilable code follows:

\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}

I'm new here. Let me know if I need to put more details!

Thank you so much for your attention and participation.

Happy new year to all of us!

Ana Souza
  • 65
  • 1
  • 7
  • Hi and happy new year! A [minimal code reproducing the above output](http://meta.tex.stackexchange.com/questions/228/ive-just-been-asked-to-write-a-minimal-example-what-is-that) to then be modified would be great! – MattAllegro Jan 01 '21 at 10:15
  • 1
    @Ana In addition to the comment above, please make sure to post your code as text and not as image. – samcarter_is_at_topanswers.xyz Jan 01 '21 at 12:51
  • Hi, @MattAllegro, I did as you asked me! Sorry for the delay! – Ana Souza Jan 02 '21 at 16:20
  • @AnaSouza Please don't post only code fragments. You would make it much easier to help you if you would post a small, but compilable document. This way people could directly start to work on a solution for you without having to guess the necessary packages etc. to compile your fragment. – samcarter_is_at_topanswers.xyz Jan 02 '21 at 17:14
  • I can't reproduce the left margin problem if I guess a couple of things to compile your code fragment, there must be more going on. – samcarter_is_at_topanswers.xyz Jan 02 '21 at 17:19
  • Sorry, @samcarter_is_at_topanswers.xyz! Now the code is compilable (I'm terrible using overleaf). Werner solved the margin, but the comment still doesn't work! – Ana Souza Jan 03 '21 at 21:28

2 Answers2

2

To place a comment after the condition of the for loop, you can use \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}

enter image description here

  • The comment works perfectly. Thank you for your patience and guidance. I'll be a better user in Stackoverflow after this experience. Best regards. – Ana Souza Jan 08 '21 at 03:33
1

This is the default layout of an algorithm when using 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}

You can use \setlength{\algomargin}{<len>} to change the margins. For example, this is what \setlength{\algomargin}{0pt} looks like:

enter image description here

The default (first image above) is \leftskip + \parindent.

Werner
  • 14,324
  • 7
  • 55
  • 77