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

Erroneous linebreak? #394

Closed dpo closed 1 year ago

dpo commented 1 year ago

Please provide the following when posting an issue:

original .tex code

-    By construction, \(\phi_{k,i}(x) = \phi_{k,i}^>(x) + \phi_{k,i}^<(x)\), \(\{\phi_{k,i}^>(x)\} \downarrow 0\) and \(\{\phi_{k,i}^<(x)\} \uparrow 0\) as \(k \to \infty\) for all \(x > 0\).
+    By construction, \(\phi_{k,i}
+    (x) = \phi_{k,i}^>(x) + \phi_{k,i}^<(x)\), \(\{\phi_{k,i}^>(x)\} \downarrow 0\) and \(\{\phi_{k,i}^<(x)\} \uparrow 0\) as \(k \to \infty\) for all \(x > 0\).

yaml settings

indentPreamble: 1
defaultIndent: "  "

indentAfterHeadings:
    part:
       indentAfterThisHeading: 0
       level: 1
    chapter:
       indentAfterThisHeading: 0
       level: 2
    section:
       indentAfterThisHeading: 1
       level: 3
    subsection:
       indentAfterThisHeading: 1
       level: 4
    subsection*:
       indentAfterThisHeading: 1
       level: 4
    subsubsection:
       indentAfterThisHeading: 1
       level: 5
    paragraph:
       indentAfterThisHeading: 1
       level: 6
    subparagraph:
       indentAfterThisHeading: 1
       level: 7

noAdditionalIndent:
    document: 0

indentRules:
   chapter: " "
   section: "  "
   subsection: "  "
   paragraph: "  "
   item: "  "

dos2unixlinebreaks: 1

modifyLineBreaks:
    oneSentencePerLine:
        manipulateSentences: 1
        sentencesBeginWith:
            other: "\$|\\("
    # poly-switches below here
    environments:
        DBSStartsOnOwnLine: 0
        DBSFinishesWithLineBreak: 1
        BeginStartsOnOwnLine: 1
        BodyStartsOnOwnLine: 2
        EndStartsOnOwnLine: 1
        EndFinishesWithLineBreak: 1
        equation*:
            BeginStartsOnOwnLine: 1
            BodyStartsOnOwnLine: 1
            EndStartsOnOwnLine: 1
            EndFinishesWithLineBreak: 1
    specialBeginEnd:
        displayMath:
          SpecialBeginStartsOnOwnLine: 1
          SpecialBodyStartsOnOwnLine: 1
          SpecialEndStartsOnOwnLine: 1
          SpecialEndFinishesWithLineBreak: 1

actual/given output

See diff above.

desired or expected output

I would not expect a linebreak before (x).

cmhughes commented 1 year ago

Can you strip the yaml down to minimal, please?

dpo commented 1 year ago

Sorry. I modified the post above to reflect my non-default settings.

dpo commented 1 year ago

The culprit seems to be

modifyLineBreaks:
    oneSentencePerLine:
        manipulateSentences: 1
        sentencesBeginWith:
            other: "\$|\\("
cmhughes commented 1 year ago

When I start with

By construction, \(\phi_{k,i}(x) = \phi_{k,i}^>(x) + \phi_{k,i}^<(x)\), \(\{\phi_{k,i}^>(x)\} \downarrow 0\) and \(\{\phi_{k,i}^<(x)\} \uparrow 0\) as \(k \to \infty\) for all \(x > 0\).

and the settings

modifyLineBreaks:
    oneSentencePerLine:
        manipulateSentences: 1
        sentencesBeginWith:
            other: "\$|\\("

then I receive

By construction, \(\phi_{k,i}(x) = \phi_{k,i}^>(x) + \phi_{k,i}^<(x)\), \(\{\phi_{k,i}^>(x)\} \downarrow 0\) and \(\{\phi_{k,i}^<(x)\} \uparrow 0\) as \(k \to \infty\) for all \(x > 0\).

Your example does not demonstrate the problem. Please see I've just been asked to write a minimal working example (MWE), what is that?] and ensure that code you post is minimal and demonstrates the problem.

dpo commented 1 year ago

Sorry. It seems the behavior occurs because of surrounding LaTeX code.

I boiled down the LaTeX code to this:

\begin{proof}
    Define
    \[
        x = 1.
    \]
    By construction, \(\phi_{k,i}(x) = \phi_{k,i}^>(x) + \phi_{k,i}^<(x)\).
\end{proof}

With

modifyLineBreaks:
    oneSentencePerLine:
        manipulateSentences: 1
        sentencesBeginWith:
            other: "\$|\\("

I obtain

\begin{proof}
    Define \[ x = 1.
    \]
    By construction, \(\phi_{k,i}
    (x) = \phi_{k,i}^>(x) + \phi_{k,i}^<(x)\).
\end{proof}
cmhughes commented 1 year ago

Thanks for more details.

solution

Using

modifyLineBreaks:
    oneSentencePerLine:
        manipulateSentences: 1
        sentencesBeginWith:
            other: "\$|\\\\\("

gives

\begin{proof}
    Define \[ x = 1.
    \]
    By construction, \(\phi_{k,i}(x) = \phi_{k,i}^>(x) + \phi_{k,i}^<(x)\).
\end{proof}

I hope this as you would like.

explanation

In your YAML you've specified

 other: "\$|\\("

This is translated to mean "either $ or (". Note that "\(" translates to "(".

We modify it to be

other: "\$|\\\\\("

which is translated to mean "either $ or \(". Note that "\\" translates to "\" so when followed by "(" it translates literally into "\(".

dpo commented 1 year ago

Thank you!