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

`DBSFinishesWithLineBreak: 1` confused #402

Closed tdegeus closed 1 year ago

tdegeus commented 1 year ago

original .tex code

This is some text. That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\
            c = d
        \end{split}
    \end{equation}
}
that ends here.

yaml settings

onlyOneBackUp: 1
defaultIndent: '    '
removeTrailingWhitespace: 1

lookForAlignDelims:
  align: 0

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    textWrapSentences: 1
    multipleSpacesToSingle: 1
  items:
    ItemFinishesWithLineBreak: 1
  environments:
    BeginStartsOnOwnLine: 1
    BodyStartsOnOwnLine: 1
    EndStartsOnOwnLine: 1
    EndFinishesWithLineBreak: 1
    DBSFinishesWithLineBreak: 1
indentRules:
  item: '    '
fineTuning:
  namedGroupingBracesBrackets:
    name: '[0-9\.a-zA-Z@\*><_]+?'

# remove recurrent spaces
replacements:
- substitution: s/([a-zA-Z0-9.}{])\h{2,}([a-zA-Z0-9.}{])/$1 $2/sg
  when: before
- substitution: s/([a-zA-Z0-9.()}{])\h{2,}([a-zA-Z0-9.()}{]|(?:\\(?!\\)))/$1 $2/sg
  when: after

actual/given output

This is some text.
That needs formatting \detail{
    \begin{equation}
        \begin{split}
            a = b \\ c = d
        \end{split}
    \end{equation}
} that ends here.

desired or expected output

This is some text.
That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\
            c = d
        \end{split}
    \end{equation}
}
that ends here.
cmhughes commented 1 year ago

Please use the issue template, then I'll look at it.

tdegeus commented 1 year ago

ok. edited.

cmhughes commented 1 year ago

Starting with

This is some text. That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\
            c = d
        \end{split}
    \end{equation}
}
that ends here.

and using

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
  environments:
    BeginStartsOnOwnLine: 1
    BodyStartsOnOwnLine: 1
    EndStartsOnOwnLine: 1
    EndFinishesWithLineBreak: 1
    DBSFinishesWithLineBreak: 1
  commands:                        # <!--- NEW BIT
    detail:                        # <!--- NEW BIT
      CommandStartsOnOwnLine: 1    # <!--- NEW BIT
  mandatoryArguments:              # <!--- NEW BIT
    detail:                        # <!--- NEW BIT
      RCuBFinishesWithLineBreak: 1 # <!--- NEW BIT

gives

This is some text.
That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\ c = d
        \end{split}
    \end{equation}
}
that ends here.
cmhughes commented 1 year ago

Oh, and if you want the DBSFinishesWithLineBreak to activate, then you need to use

lookForAlignDelims:
  split: 1

Here's the complete YAML

lookForAlignDelims:
  split: 1

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
  environments:
    BeginStartsOnOwnLine: 1
    BodyStartsOnOwnLine: 1
    EndStartsOnOwnLine: 1
    EndFinishesWithLineBreak: 1
    DBSFinishesWithLineBreak: 1
  commands:                        # <!--- NEW BIT
    detail:                        # <!--- NEW BIT
      CommandStartsOnOwnLine: 1    # <!--- NEW BIT
  mandatoryArguments:              # <!--- NEW BIT
    detail:                        # <!--- NEW BIT
      RCuBFinishesWithLineBreak: 1 # <!--- NEW BIT

then you receive

This is some text.
That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\
            c = d
        \end{split}
    \end{equation}
}
that ends here.
tdegeus commented 1 year ago

Thanks. However, it does not work for me:

onlyOneBackUp: 1
defaultIndent: '    '
removeTrailingWhitespace: 1

lookForAlignDelims:
  align: 0

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    textWrapSentences: 1
    multipleSpacesToSingle: 1
  mandatoryArguments:
    footnote:
      MandArgBodyStartsOnOwnLine: 1
      RCuBFinishesWithLineBreak: 1
    detail:
      MandArgBodyStartsOnOwnLine: 1
      RCuBFinishesWithLineBreak: 1
  commands:
    label:
      CommandStartsOnOwnLine: 1
    detail:
      CommandStartsOnOwnLine: 1
  items:
    ItemFinishesWithLineBreak: 1
  environments:
    BeginStartsOnOwnLine: 1
    BodyStartsOnOwnLine: 1
    EndStartsOnOwnLine: 1
    EndFinishesWithLineBreak: 1
    DBSFinishesWithLineBreak: 1
indentRules:
  item: '    '
fineTuning:
  namedGroupingBracesBrackets:
    name: '[0-9\.a-zA-Z@\*><_]+?'

# remove recurrent spaces
replacements:
- substitution: s/([a-zA-Z0-9.}{])\h{2,}([a-zA-Z0-9.}{])/$1 $2/sg
  when: before
- substitution: s/([a-zA-Z0-9.()}{])\h{2,}([a-zA-Z0-9.()}{]|(?:\\(?!\\)))/$1 $2/sg
  when: after

formats to

This is some text.
That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\ c = d
        \end{split}
    \end{equation}
}
that ends here.
cmhughes commented 1 year ago

You need this bit

lookForAlignDelims:
  split: 1

to give

This is some text.
That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\
            c = d
        \end{split}
    \end{equation}
}
that ends here.
tdegeus commented 1 year ago

I see, thanks. It is not exactly what I hoped to have, that is that

This is some text. That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b + c \\
            d = e \\
            f = g
        \end{split}
    \end{equation}
}
that ends here.

would not be formatted with

lookForAlignDelims:
  align: 0
  split: 0

modifyLineBreaks:
  environments:
    DBSFinishesWithLineBreak: 1

both split and equation are an environment, so I would expect the double backslash to be forced to have a line break. Instead, it is formatted to

This is some text.
That needs formatting
\begin{equation}
    \begin{split}
        a = b + c \\ d = e \\ f = g
    \end{split}
\end{equation}
that ends here.

which I would say to contradict the DBSFinishesWithLineBreak: 1.


What does work as expected (though not what I wanted), is that with

lookForAlignDelims:
  align: 1
  split: 1

modifyLineBreaks:
  environments:
    DBSFinishesWithLineBreak: 1

the text is formatted to:


This is some text.
That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b + c \\
            d = e     \\
            f = g
        \end{split}
    \end{equation}
}
that ends here.
tdegeus commented 1 year ago

Ok I got it:

lookForAlignDelims:
  align:
    delims: 1
    alignDoubleBackSlash: 0
  split:
    delims: 1
    alignDoubleBackSlash: 0

but I'm still a bit confused that DBSFinishesWithLineBreak did not do it.

cmhughes commented 1 year ago

Great, thanks.

I'm going to consider this a feature request: see if it's possible to enable DBS poly-switches can work without requiring lookForAlignDelims.

This came up in https://github.com/cmhughes/latexindent.pl/issues/393 from @ankitp94. I'd hope this won't take too long, but I'm currently working on text wrapping.

cmhughes commented 1 year ago

As of https://github.com/cmhughes/latexindent.pl/commit/3ef192c9a08ff79ede947f21508f68739e0b3d87, this is implemented. Specifically, there is now no longer a need to specify a code block within lookForAlignDelims.

This will be part of the next release (early 2023), let's leave this open until then. Demonstrations below.

demonstration 1

starting with

This is some text. That needs formatting
\detail{
    \begin{equation}
        \begin{split}
            a = b \\
            c = d
        \end{split}
    \end{equation}
}
that ends here.

and using

defaultIndent: '    '

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    textWrapSentences: 1
  environments:
    BeginStartsOnOwnLine: 1
    BodyStartsOnOwnLine: 1
    EndStartsOnOwnLine: 1
    EndFinishesWithLineBreak: 1
    DBSFinishesWithLineBreak: 1

gives

This is some text.
That needs formatting \detail{
    \begin{equation}
        \begin{split}
            a = b \\
            c = d
        \end{split}
    \end{equation}
} that ends here.

demonstration 2

starting with

This is some text.
That needs formatting
\[
        a = b + c \\ d = e \\ f = g
\]
that ends here.

and using

defaultIndent: '    '

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    textWrapSentences: 1
  specialBeginEnd:
    DBSFinishesWithLineBreak: 2
    SpecialBeginStartsOnOwnLine: 1
    SpecialBodyStartsOnOwnLine: 1
    SpecialEndStartsOnOwnLine: 1
    SpecialEndFinishesWithLineBreak: 1

gives

This is some text.
That needs formatting
\[
    a = b + c \\%
    d = e \\%
    f = g
\]
that ends here.

demonstration 3

starting with

This is some text.
That needs formatting
\cmh{
        a = b + c \\ d = e \\ f = g
      }
that ends here.

and using

defaultIndent: '    '

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    textWrapSentences: 1
  mandatoryArguments:                     
      MandArgBodyStartsOnOwnLine: 1
      RCuBStartsOnOwnLine: 1
      RCuBFinishesWithLineBreak: 1
      DBSFinishesWithLineBreak: 2

gives

This is some text.
That needs formatting \cmh{
    a = b + c \\%
    d = e \\%
    f = g
}
that ends here.
tdegeus commented 1 year ago

That's amazing, and makes a lot of sense. Thanks a lot!

cmhughes commented 1 year ago

This is part of V3.20 at https://github.com/cmhughes/latexindent.pl/releases/tag/V3.20. I've uploaded it to ctan. Many thanks!