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

How can I keep same indent for foreach's end bracket and foreach #379

Closed avatar-lavventura closed 2 years ago

avatar-lavventura commented 2 years ago

I want end bracket to be same indent with start of \foreach , example:

\foreach ... in {...} {
...
}  # same place where `\foreach` starts

yaml setting:

defaultIndent: "    "
verbatimEnvironments:
    longtable: 1
    tabularx: 1
    minted: 1
    item: 1
specialBeginEnd:
  If:
    begin: '\\If'
    middle:
        - '\\ElsIf'
        - '\\Else'
    end: '\\EndIf'
  specialBeforeCommand: 1

latexindent.pl keeps add a indent before the } and adds indent before the second line that follows up and also add 8 space indent inside the foreach block instead of 4 space indent.

\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
        \node at (4.5, -\i) {\j};
    }
    \node[PR] at (3.48, -0.5) {};
         \node[PR] at (3.48, -1.5) {};
    \node[PR] at (3.48, -3.5) {};
        \node[PR] at (3.48, -1.5) {};
\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
    \node at (4.5, -\i) {\j};  # 4 space instead of 8
}
\node[PR] at (3.48, -0.5) {};
\node[PR] at (3.48, -1.5) {};
\node[PR] at (3.48, -3.5) {};
\node[PR] at (3.48, -1.5) {};
cmhughes commented 2 years ago

Thanks for this, it was interesting!

solution

Put the following in your yaml settings, say latexindent.yaml

commandCodeBlocks:
    stringsAllowedBetweenArguments:
      - 'in'                  # allows 'in' between arguments
      - '\\[a-z]\/\\[a-z]'    # allows any letter followed by / followed by another letter between arguments
noAdditionalIndent:
    foreach: 
        body: 1

and then run

latexindent.pl -l myfile.tex

and you receive

\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
    \node at (4.5, -\i) {\j};
}
\node[PR] at (3.48, -0.5) {};
\node[PR] at (3.48, -1.5) {};
\node[PR] at (3.48, -3.5) {};
\node[PR] at (3.48, -1.5) {};

explanation

I hope this helps, let me know if you have anything else! :)

avatar-lavventura commented 2 years ago

Thank you for your depth answer. Please note that, I believe following cause was causing this output that I shared in my question.

specialBeginEnd:
  If:
    begin: '\\If'
    middle:
        - '\\ElsIf'
        - '\\Else'
    end: '\\EndIf'
  specialBeforeCommand: 1

Would it be possible to merge it with you answer? When I keep this along with your answer I still face with the same problem

When I also added your code and experiment with following code:

        \foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
            \node at (4.5, -\i) {\j};
                 }
        \node[PR] at (3.48, -0.5) {\footnotesize $F^{};
            \node[PR] at (3.48, -1.5) {\footnotesize $F^{};
        \node[PR] at (3.48, -3.5) {\footnotesize $F^{};
        \node[PR] at (3.48, -0.5) {\footnotesize $F^{};
            \node[PR] at (3.48, -1.5) {\footnotesize $F^{};
        \node[PR] at (3.48, -3.5) {\footnotesize $F^{};

converted into :

\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
    \node at (4.5, -\i) {\j};
}
\node[PR] at (3.48, -0.5) {\footnotesize $F^{};
    \node[PR] at (3.48, -1.5) {\footnotesize $F^{};
\node[PR] at (3.48, -3.5) {\footnotesize $F^{};
    \node[PR] at (3.48, -0.5) {\footnotesize $F^{};
\node[PR] at (3.48, -1.5) {\footnotesize $F^{};
    \node[PR] at (3.48, -3.5) {\footnotesize $F^{};

wanted output:

\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
    \node at (4.5, -\i) {\j};
}
\node[PR] at (3.48, -0.5) {\footnotesize $F^{};
\node[PR] at (3.48, -1.5) {\footnotesize $F^{};
\node[PR] at (3.48, -3.5) {\footnotesize $F^{};
\node[PR] at (3.48, -0.5) {\footnotesize $F^{};
\node[PR] at (3.48, -1.5) {\footnotesize $F^{};
\node[PR] at (3.48, -3.5) {\footnotesize $F^{};

Thank you for your valuable time and help.

cmhughes commented 2 years ago

Happy to help :)

In your code, I see {\footnotesize $F^{}; and I think you're missing a few closing braces across a few lines.

Can you fix these and try again?

avatar-lavventura commented 2 years ago

Sorry I gave you the wrong code, I was trying to simplified the code but accidently deleted \\. Can you please have a look at to this: Having \\ as a new line in between the { } is the main cause I believe

        \foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
            \node at (4.5, -\i) {\j};
        }
        \node[PR] at (3.48, -0.5) {alper\\$\mathsf{J}$,$A$,...};
        \node[PR] at (3.48, -1.5) {\footnotesize $A$};
        \node[PR] at (3.48, -1.5) {\footnotesize $B$};

output:

\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
    \node at (4.5, -\i) {\j};
}
\node[PR] at (3.48, -0.5) {alper\\$\mathsf{J}$,$A$,...};
        \node[PR] at (3.48, -1.5) {\footnotesize $A$};
        \node[PR] at (3.48, -1.5) {\footnotesize $B$};
cmhughes commented 2 years ago

When I use the YAML I gave above, and run

latexindent.pl -l myfile.tex

then I receive

\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
    \node at (4.5, -\i) {\j};
}
\node[PR] at (3.48, -0.5) {alper\\$\mathsf{J}$,$A$,...};
\node[PR] at (3.48, -1.5) {\footnotesize $A$};
\node[PR] at (3.48, -1.5) {\footnotesize $B$};

This looks like your desired output.

To make a minimal example, make a copy of your code, and strip it down until the problem is replicated.

avatar-lavventura commented 2 years ago

Interesting I am getting completely different output with following configuration, where I merge the one I give in my question with your answer:

defaultIndent: "    "
verbatimEnvironments:
    longtable: 1
    tabularx: 1
    minted: 1
    item: 1
specialBeginEnd:
  If:
    begin: '\\If'
    middle:
        - '\\ElsIf'
        - '\\Else'
    end: '\\EndIf'
  specialBeforeCommand: 1
commandCodeBlocks:
    stringsAllowedBetweenArguments:
      - 'in'                  # allows 'in' between arguments
      - '\\[a-z]\/\\[a-z]'    # allows any letter followed by / followed by another letter between arguments
noAdditionalIndent:
    foreach:
        body: 1

When I deleted following section seems like everything works fine:

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

Interesting!

The problem part was the combination of

  specialBeforeCommand: 1

and the default settings for inlineMath:

inlineMath:
         begin: '(?<!\$)(?<!\\)\$(?!\$)'
         end: '(?<!\\)\$(?!\$)'
         lookForThis: 1

which says that $ can not be preceeded by a backslash.

We can change this by using

specialBeginEnd:
    specialBeforeCommand: 1
    inlineMath:
        begin: '(?:\\\\\$)|(?:(?<!\$)(?<!\\)\$(?!\$))'     # <!------------ NEW BIT
        end: '(?<!\\)\$(?!\$)'
        lookForThis: 1
commandCodeBlocks:
    stringsAllowedBetweenArguments:
      - 'in'                  # allows 'in' between arguments
      - '\\[a-z]\/\\[a-z]'    # allows any letter followed by / followed by another letter between arguments

This says that inlineMath can begin with

This gives the output

\foreach \i/\j in {0.5/$H_{0}$, 1.5/$H_{0}$ } {
    \node at (4.5, -\i) {\j};
}
\node[PR] at (3.48, -0.5) {alper\\$\mathsf{J}$,$A$,...};
\node[PR] at (3.48, -1.5) {\footnotesize $A$};
\node[PR] at (3.48, -1.5) {\footnotesize $B$};
avatar-lavventura commented 2 years ago

Thank you again and sorry for extending the issue with additional questions :-)

cmhughes commented 2 years ago

No worries, questions are welcome :) glad I could help!

cmhughes commented 2 years ago

Happy to help.

Can you give a complete example that demonstrates the problem?

On Sun, 21 Aug 2022, 10:31 Alper Alimoglu, @.***> wrote:

Thank you for your depth answer.

Please note that

specialBeginEnd: If: begin: '\If' middle:

  • '\ElsIf'
  • '\Else' end: '\EndIf' specialBeforeCommand: 1

was also causing this output that I shared in my question. Would it be possible to merge it with you answer?

— Reply to this email directly, view it on GitHub https://github.com/cmhughes/latexindent.pl/issues/379#issuecomment-1221507966, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYD3DJ3IEFOYNDDHE2LV2HZNRANCNFSM57DBWHJA . You are receiving this because you commented.Message ID: @.***>