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

bug: comment recursively added #425

Closed tdegeus closed 1 year ago

tdegeus commented 1 year ago

Please provide the following when posting an issue:

original .tex code

This is a\footnote{
    My footnote
}
sentence.

This is a sentence\footnote{
    My footnote
}.

yaml settings

defaultIndent: '    '

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    multipleSpacesToSingle: 1
  mandatoryArguments:
    footnote:
      MandArgBodyStartsOnOwnLine: 1
      RCuBStartsOnOwnLine: 1
      RCuBFinishesWithLineBreak: 2

actual/given output

Running once (output as expected):

This is a\footnote{
    My footnote
}%
sentence.

This is a sentence\footnote{
    My footnote
}%
.

Running twice (comments recursively added):

This is a\footnote{
    My footnote
}%
sentence.
%

This is a sentence\footnote{
    My footnote
}%
.
%

Running three times (comments recursively added):

This is a\footnote{
    My footnote
}%
sentence.
%
%

This is a sentence\footnote{
    My footnote
}%
.
%
%

desired or expected output

```tex
This is a\footnote{
    My footnote
}%
sentence.

This is a sentence\footnote{
    My footnote
}%
.

anything else

Following https://github.com/cmhughes/latexindent.pl/issues/414

cmhughes commented 1 year ago

This isn't a bug. I'll explain when I get time :)

cmhughes commented 1 year ago

To understand this, we reference https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#onesentenceperline-modifying-line-breaks-for-sentences which details that the oneSentencePerLine routine happens before the code blocks have been found.

run 1

So both of the sentences in the following are put onto one line:

This is a\footnote{
    My footnote
}
sentence.

This is a sentence\footnote{
    My footnote
}.

and will become

This is a\footnote{My footnote} sentence.

This is a sentence\footnote{My footnote}.

Then, after the oneSentencePerLine routine has completed, the main code blocks are found, which is when the poly-switches are activated, and you receive

This is a\footnote{
    My footnote
}%
sentence.

This is a sentence\footnote{
    My footnote
}%
.

run 2

As previously, the oneSentencePerLine routine activates first and will give


This is a\footnote{My footnote} sentence. 
%

This is a sentence\footnote{My footnote}.
%

Then, as previously, the main code block routine activates, including the poly-switches. The value of 2 means check if there is a line break or a comment, and if there isn't one, then add one, and then a line break. It does not find a line break or a comment, so it adds a % and then a line break.

If you want an option for oneSentencePerLine to remove all comments within sentences, that could be a thing.