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
881 stars 83 forks source link

helping oneSentencePerLine manually #409

Closed tdegeus closed 1 year ago

tdegeus commented 1 year ago

Please provide the following when posting an issue:

original .tex code

\emph{First sentence?} Second (third?) sentence? \emph{Third sentence?} Fourth sentence?

yaml settings

onlyOneBackUp: 1
defaultIndent: '    '
removeTrailingWhitespace: 1

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

actual/given output

\emph{First sentence?}
Second (third?
) sentence? \emph{Third sentence?}Fourth sentence?

desired or expected output

\emph{First sentence?} 
Second (third?) sentence? 
\emph{Third sentence?} 
Fourth sentence?

anything else

As observed, the output of this example is quite a mess. The second sentence is broken where is grammatically should not have been, whereas the third and fourth sentences are not broken and even merged.

There is the solution of https://github.com/cmhughes/latexindent.pl/issues/406 to have

onlyOneBackUp: 1
defaultIndent: '    '
removeTrailingWhitespace: 1

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    removeSentenceLineBreaks: 1
    multipleSpacesToSingle: 1
    sentencesEndWith:
        questionMark: 0         # 0/1
        other: |-
          (?x)
          \?                    # ?
          (?!                   # not *followed by*
            (?:                 #
              \)                # )
              |                 #
              \}                # }
            )                   #
          )                     #

which formats the original example somewhat better

\emph{First sentence?}
Second (third?) sentence?
\emph{Third sentence?}Fourth sentence?

but still not what I desire above.

Then I thought. Well ok, there is some difficulty, let's manually help latexindent. But there is no such way without switching off formatting in the entire block. What would be ideal that in certain edge cases one could

\emph{First sentence?}
Second (third?) sentence?
\emph{Third sentence?} % noqa: oneSentencePerLine
Fourth sentence?

(or so)

cmhughes commented 1 year ago

Thanks for using the issue template :)

solution

Using the settings

defaultIndent: '    '

modifyLineBreaks:
  oneSentencePerLine:
    manipulateSentences: 1
    sentencesBeginWith:            
        other: |-
          (?x)
          \\emph\{[A-Z]
    sentencesEndWith:
        questionMark: 0         # 0/1
        other: |-
          (?x)
          (?<!                  # not *preceeded by*
            (?:                 #
              [A-Z]             #
            )                   #
          )
          \?                    # ?
          (?!                   # NOT *followed by*          ---|
            (?:                 #                               |
              \)                # )                             |
              |                 #                               |
              \}                # }                             |
            )                   #                               |
            \h*                 # possible horizontal space     |
            [a-z]               # a-z                        ---
          )                     #
          [})]?                 # possible } or )

gives

\emph{First sentence?}
Second (third?) sentence?
\emph{Third sentence?}
Fourth sentence?

I've incorporated the settings from https://github.com/cmhughes/latexindent.pl/issues/408 and https://github.com/cmhughes/latexindent.pl/issues/406 as I'm assuming they're related... delete if not.

explanation

Let's start with the easiest part:

    sentencesBeginWith:            
        other: |-
          (?x)
          \\emph\{[A-Z]

This says that sentences can begin with \emph{ and then a capital letter. Adjust as necessary.

The harder part is:

    sentencesEndWith:
        questionMark: 0         # 0/1
        other: |-
          (?x)
          (?<!                  # not *preceeded by*
            (?:                 #
              [A-Z]             #
            )                   #
          )
          \?                    # ?
          (?!                   # NOT *followed by*          ---|
            (?:                 #                               |
              \)                # )                             |
              |                 #                               |
              \}                # }                             |
            )                   #                               |
            \h*                 # possible horizontal space     |
            [a-z]               # a-z                        ---
          )                     #
          [})]?                 # possible } or )

We first of all need to turn off the 'basic' question mark.

Then we need to ensure that:

I hope this helps, let me know if not.

cmhughes commented 1 year ago

I'm assuming this is resolved, let me know if not!

tdegeus commented 1 year ago

Thanks. What was a bit hidden at the at of my question is if there is a way to manually help latexindent for some edge-cases. Like for example in Python one can add a comment to the end of the line with noqa

cmhughes commented 1 year ago

As always, please provide before and after

cmhughes commented 1 year ago

Any update on before /after?

tdegeus commented 1 year ago

Hi. Sorry for the radio silence. My perspective is that:

  1. It should be great to have a switch to turn-off grouping comments.

  2. If that is unwanted, if would still be good to have a way to manually format a line if you want to. So in that case for example the following example should not be formatted (before == after):

    \emph{First sentence?}
    Second (third?) sentence?
    \emph{Third sentence?} % noqa: oneSentencePerLine
    Fourth sentence?
cmhughes commented 1 year ago

So you want oneSentencePerLine to leave comments in place? And I'm assuming you want to text wrapping to obey all of this as well?

tdegeus commented 1 year ago

Indeed, I would like the option to leave the comments where they are

cmhughes commented 1 year ago

OK, so comments staying on same line at the end of sentence.

Keeping them exactly where they are won't happen.

On Fri, 6 Jan 2023, 15:01 Tom de Geus, @.***> wrote:

Indeed, I would like the option to leave the comments where they are

— Reply to this email directly, view it on GitHub https://github.com/cmhughes/latexindent.pl/issues/409#issuecomment-1373741336, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYHP7ZE4N5J7WGV3L3DWRAXTLANCNFSM6AAAAAATOWZFO4 . You are receiving this because you modified the open/close state.Message ID: @.***>

tdegeus commented 1 year ago

I would say that the 'issue' here is fixed. Not to mix things, I have started a general discussion in https://github.com/cmhughes/latexindent.pl/issues/418 .