cmhughes / latexindent.pl

Perl script to add indentation (leading horizontal space) to LaTeX files. It can modify line breaks before, during and after code blocks; it can perform text wrapping and paragraph line break removal. It can also perform string-based and regex-based substitutions/replacements. The script is customisable through its YAML interface.
GNU General Public License v3.0
867 stars 84 forks source link

Add support for algorithm packages, such as algorithmic and algpseudocodex #496

Closed Saltsmart closed 9 months ago

Saltsmart commented 9 months ago

what is this pull request about?

This pull request adds the support for algorithm packages, such as algorithmic and algpseudocodex

does this relate to an existing issue?

Here is the link: issue 427

does this change any existing behaviour?

yes

what does this add?

Here is a minimal example:

\begin{algorithm}[ht]
    \caption{some caption.}
    \begin{algorithmic}[1]
        \Require{aaa}
        \Ensure{bbb}
        % for-loop
        \For{$n = 1, \dots, 10$}
        \State body
        \EndFor
        % nesting for-loop
        \FOR{for 1}
        \FOR{for 2}
        \FOR{for 3}
        \STATE{some statement.}
        \ENDFOR
        \ENDFOR
        \ENDFOR
        % while-loop in if-statement
        \If{$quality\ge 9$}
        \State $a\gets perfect$
        \ElsIf{$quality\ge 7$}
        \State $a\gets good$
        \ElsIf{$quality\ge 5$}
        \State $a\gets medium$
        \ElsIf{$quality\ge 3$}
        \State $a\gets bad$
        \Else
        \While{$i\le n$}
        \State $sum\gets sum+i$
        \State $i\gets i+1$
        \EndWhile
        \EndIf
        % nesting blocks
        \ForAll{$n \in \{1, \dots, 10\}$}
        \State body
        \Loop
        \State body
        \EndLoop
        \State $sum\gets 0$
        \State $i\gets 1$
        \Repeat
        \State $sum\gets sum+i$
        \State $i\gets i+1$
        \Until{$i>n$}
        \EndFor
        % fuction block
        \Function{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
        \State $r\gets a\bmod b$
        \While{$r\not=0$}\Comment{We have the answer if r is 0}
        \State $a\gets b$
        \State $b\gets r$
        \State $r\gets a\bmod b$
        \EndWhile
        \State \textbf{return} $b$\Comment{The gcd is b}
        \EndFunction
    \end{algorithmic}
\end{algorithm}

After merging the pull request, latexindent.pl could format the file as follows (with 4-space indents):

\begin{algorithm}[ht]
    \caption{some caption.}
    \begin{algorithmic}[1]
        \Require{aaa}
        \Ensure{bbb}
        % for-loop
        \For{$n = 1, \dots, 10$}
            \State body
        \EndFor
        % nesting for-loop
        \FOR{for 1}
            \FOR{for 2}
                \FOR{for 3}
                    \STATE{some statement.}
                \ENDFOR
            \ENDFOR
        \ENDFOR
        % while-loop in if-statement
        \If{$quality\ge 9$}
            \State $a\gets perfect$
        \ElsIf{$quality\ge 7$}
            \State $a\gets good$
        \ElsIf{$quality\ge 5$}
            \State $a\gets medium$
        \ElsIf{$quality\ge 3$}
            \State $a\gets bad$
        \Else
            \While{$i\le n$}
                \State $sum\gets sum+i$
                \State $i\gets i+1$
            \EndWhile
        \EndIf
        % nesting blocks
        \ForAll{$n \in \{1, \dots, 10\}$}
            \State body
            \Loop
                \State body
            \EndLoop
            \State $sum\gets 0$
            \State $i\gets 1$
            \Repeat
                \State $sum\gets sum+i$
                \State $i\gets i+1$
            \Until{$i>n$}
        \EndFor
        % fuction block
        \Function{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
            \State $r\gets a\bmod b$
            \While{$r\not=0$}\Comment{We have the answer if r is 0}
                \State $a\gets b$
                \State $b\gets r$
                \State $r\gets a\bmod b$
            \EndWhile
            \State \textbf{return} $b$\Comment{The gcd is b}
        \EndFunction
    \end{algorithmic}
\end{algorithm}

how do I test this?

You can test this as the above section depicts.

anything else?

No more further comments. Best wishes for you and your family!

github-actions[bot] commented 9 months ago

Thank you for your contribution! Please can you change this pull request so that it goes to the develop branch? Thank you

cmhughes commented 9 months ago

Thanks this looks like a good contribution!

A few comments to address, please

Thanks so much for your time on this!