Search code examples
latexoverleaf

Algorithm left margin and Comment Customizing


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!


Solution

  • 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