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 [Develop branch] #498

Closed Saltsmart closed 9 months ago

Saltsmart commented 9 months ago

Reopen of another pull request #496, but from the dev branch. Here is the improvement for this pull request:

It looks you might have branched from an earlier version; can you rebase from V3.23.4? you'll see that, for example, Version.pm is out of date on your branch

Here is the dev branch from V3.23.4.

the lookForThis: 1 is not necessary, so can you remove it in each occurrence?

Thanks for your advice! I've removed it.

can you add dedicated test cases that demonstrate these to test-cases/ specials? this is really important so that when new features are added/developed, I can ensure that nothing breaks.

I've added a file test-cases/specials/algorithmic-and-algpseudocodex.tex. Please review below.

This line shouldn't be the default. Please leave this value at 0

I have tried specialBeforeCommand: 0 and found this couldn't format the code. Here is the discussion.

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!

cmhughes commented 9 months ago

Thanks this is looking good. A couple more comments from me:

Saltsmart commented 9 months ago

Sorry for not familiar with GitHub Actions! I could see some failed tests (even they are still running now). Are they related with my pull request? Please let me know if I could help improve these tests.

cmhughes commented 9 months ago

Thanks for this.

Thinking about this in more detail, I don't get why it needs to be part of the default settings. I can see why some users want it as part of their individual settings, in which case, they should add it to their individual global settings, but having it as part of default does not feel appropriate.

Can I recommend that you change this so that it simply adds a test case?

As another option, if you'd like to add an example to the documentation, please feel free.

My apologies for the back & forth.

Saltsmart commented 9 months ago

Thanks for this.

Thinking about this in more detail, I don't get why it needs to be part of the default settings. I can see why some users want it as part of their individual settings, in which case, they should add it to their individual global settings, but having it as part of default does not feel appropriate.

Can I recommend that you change this so that it simply adds a test case?

As another option, if you'd like to add an example to the documentation, please feel free.

My apologies for the back & forth.

I feel very grateful for your patience. Here is a new pull request to add the example to the documentation #500. ♥