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

feature request: `oneSentencePerLine` ignore specific commands #419

Closed tdegeus closed 1 year ago

tdegeus commented 1 year ago

I like modifyLineBreaks a lot, but I do find that it is a bit too aggressive in some cases. I would like to be able to exclude specific commands from the line break manipulation. For example, the settings could be as follows:

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    multipleSpacesToSingle: 1
    ignoreCommands: ['\TG', '\section']
    ignoreEnvironments: ['verbatim', 'myenv']

original .tex code

\section{Foo? Bar?}

I do not\TG{?} want this to change.
But this
should change.

yaml settings

onlyOneBackUp: 1
defaultIndent: '    '
removeTrailingWhitespace: 1

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    multipleSpacesToSingle: 1

actual/given output

\section{Foo?
  Bar?
 }

I do not\TG{?
} want this to change.
But this should change.

desired or expected output

\section{Foo? Bar?}

I do not\TG{?} want this to change.
But this should change.

anything else

@cmhughes Sorry for the amount of issues. They have been accumulating for a while, as I had little time to prepare minimal reproducers.

cmhughes commented 1 year ago

This seems related to https://github.com/cmhughes/latexindent.pl/issues/418

tdegeus commented 1 year ago

Here I am suggesting that it would be very nice to have the option to have oneSentencePerLine simply act as if a command is not there altogether everywhere in the formatted file.

Instead, in https://github.com/cmhughes/latexindent.pl/issues/418 I am suggesting that it would be good to have the option to switch of (a part of) latexindent.pl on a specific line of code.

cmhughes commented 1 year ago

As of https://github.com/cmhughes/latexindent.pl/commit/167c88ef353ce5bdc2266eb4ff205b1d66715aef I've implemented a new feature sentencesDoNOTcontain.

default

The default is

        sentencesDoNOTcontain:
            other: \\begin                  # regex

which means that sentences do not contain \begin

demonstration 1

Starting with your file, and using

modifyLineBreaks:
    oneSentencePerLine:
        manipulateSentences: 1
        sentencesDoNOTcontain:
            other: \\TG

and the output is

\section{Foo?
  Bar?
 }

I do not\TG{?} want this to change.
But this should change.

demonstration 2

If we want the output that you've specified, then we can use

modifyLineBreaks:
    oneSentencePerLine:
        manipulateSentences: 1
        sentencesDoNOTcontain: 
            other: |-
              (?x)
              (?:
                \\begin
                |
                \\TG
              )
        sentencesFollow:
            questionMark: 0                 # 0/1
            other: |-
              (?x)
              \h+[a-zA-Z]+?
              \?

and then we receive

\section{Foo? Bar?}

I do not\TG{?} want this to change.
But this should change.

finally

I'll get this released soon.

cmhughes commented 1 year ago

Implemented at https://github.com/cmhughes/latexindent.pl/releases/tag/V3.23