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
884 stars 84 forks source link

[help] How can I align indents for condition statements in `algorithm` section? #324

Closed avatar-lavventura closed 2 years ago

avatar-lavventura commented 2 years ago

If possible, I want to align indents in condition statements like: \If, \Else, \For statements in between \begin{algorithmic} ... \end{algorithmic} section.

Wanted behaviour would be as following example:

\begin{algorithmic}[1]
\For{a for statement}                | for a for stament
    \If{some coding}                 |     if some coding    
        \State do something          |         do something  
    \EndIf                           |
\EndFor                              | 
\If{some coding}                     | if some coding
    \State do something              |      do something  
\EndIf 
\end{algorithmic}

Following example latex code taken from: https://tex.stackexchange.com/a/615852/127048. When I run latexindent.pl eblocbroker.tex it makes all the statements into same indent it aligns all if else statements in the same vertical line.

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Main D Algorithm}\label{alg:cap}
\begin{algorithmic}[1]
\For{a for statement}
    \If{some coding}
      \State do something
    \ElsIf{another condition}
        \For{some condition}
            \If{another statement}
                \State do something
            \EndIf
        \EndFor
    \ElsIf{problem is here}
        \State whatever
    \EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}

After running latexindent.pl eblocbroker.tex it makes all the statements into same indent, converted into:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Main D Algorithm}\label{alg:cap}
\begin{algorithmic}[1]
    \For{a for statement}
    \If{some coding}
    \State do something
    \ElsIf{another condition}
    \For{some condition}
    \If{another statement}
    \State do something
    \EndIf
    \EndFor
    \ElsIf{problem is here}
    \State whatever
    \EndIf
    \EndFor
\end{algorithmic}
\end{algorithm}
\end{document}

Wanted output should be, its same as the given input file where I organize the indents beforehand:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Main D Algorithm}\label{alg:cap}
\begin{algorithmic}[1]
\For{a for statement}
    \If{some coding}  
      \State do something
    \ElsIf{another condition}
        \For{some condition}
            \If{another statement}
                \State do something
            \EndIf
        \EndFor
    \ElsIf{problem is here}
        \State whatever
    \EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
cmhughes commented 2 years ago

Hello, Thanks for this. Could you look at https://github.com/cmhughes/latexindent.pl/issues/100 and see if it helps?

cmhughes commented 2 years ago
specialBeginEnd:
    If:
        begin: '\\If'
        middle: '\\ElsIf'
        end: '\\EndIf'
        lookForThis: 1
    specialBeforeCommand: 1
avatar-lavventura commented 2 years ago

@cmhughes Thanks works like magic! Is it possible to convert indents from TAB to (4 spaces)?

cmhughes commented 2 years ago

https://latexindentpl.readthedocs.io/en/latest/sec-default-user-local.html#indentation-and-horizontal-space