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

Add option for hard character limit with sentence wrapping #306

Closed danrr closed 2 years ago

danrr commented 2 years ago

original .tex code

\begin{document}
\begin{enumerate}[label=(\alph*)]
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit, cras imperdiet viv ris et molestie. Nulla molestie diam quam, ac molestie velit rhoncus placerat.
\begin{enumerate}[label=(\roman*)]
\item Lorem ipsum dolor sit amet, consectetur adipiscing elit, cras imperdiet viv ris et molestie. Nulla molestie diam quam, ac molestie velit rhoncus placerat.
\end{enumerate}
\end{enumerate}
\end{document}

yaml settings

modifyLineBreaks:
    textWrapOptions:
        columns: 80
        perCodeBlockBasis: 0
        beforeFindingChildCodeBlocks: 0
        all: 1
    removeParagraphLineBreaks:
        items: 1
    oneSentencePerLine:
        manipulateSentences: 1
        sentenceIndent: "  "
        textWrapSentences: 1
        sentencesFollow:
            other: "\item"

actual/given output

\begin{document}
\begin{enumerate}[label=(\alph*)]
    \item
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, cras imperdiet viv ris
            et molestie.
          Nulla molestie diam quam, ac molestie velit rhoncus placerat.
          \begin{enumerate}[label=(\roman*)]
              \item
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, cras imperdiet viv ris
                        et molestie.
                    Nulla molestie diam quam, ac molestie velit rhoncus placerat.
          \end{enumerate}
\end{enumerate}
\end{document}

desired or expected output

\begin{document}
\begin{enumerate}[label=(\alph*)]
    \item
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, cras imperdiet 
            viv ris et molestie.
          Nulla molestie diam quam, ac molestie velit rhoncus placerat.
          \begin{enumerate}[label=(\roman*)]
              \item
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit, cras 
                        imperdiet viv ris et molestie.
                    Nulla molestie diam quam, ac molestie velit rhoncus placerat.
          \end{enumerate}
\end{enumerate}
\end{document}

-- It appears that there is no way to configure latexindent to have a hard character limit per line while also using the oneSentencePerLine option. Despite setting modifyLineBreaks:textWrapOptions:perCodeBlockBasis: 0, I'm still seeing lines that are processed as if perCodeBlockBasis was set to 1.

Is there a way to get a hard limit of 80 characters, including indentation, while keeping the sentence wrapping options above?

cmhughes commented 2 years ago

Thanks for this.

I'm afraid there's no way to set a hard limit of 80 characters.

danrr commented 2 years ago

Thank you for the quick reply.

That's good to know, I was thinking I was missing something obvious.

Would it be OK if I edit the original post to turn this into a feature request?

cmhughes commented 2 years ago

Feel free :)

BUT, you should know that I don't think this is possible. The text wrap routine happens before the indentation routine, so it doesn't know anything about leading space.

Text wrapping while accounting for leading space is not going to be easy. It may not be possible with latexindent.

You'll also see that there are a few issues ahead of this, and I'm quite behind for various reasons. If you feel like taking it on, pull requests to develop are welcome :)

danrr commented 2 years ago

I've edited the example to remove some extraneous options. Even if it's not possible to implement, it will make it easier for anyone else who may find themselves looking for a similar option, I hope.

You'll also see that there are a few issues ahead of this, and I'm quite behind for various reasons.

Of course, no worries, I did not mean to imply any level of expectations :). Thank you!

cmhughes commented 2 years ago

I'm sorry, but with all of the text wrapping issues that are coming up, I don't have capacity to consider this. If anything, I'm getting increasingly close to removing text wrap rather than enhancing it. My apologies.

cmhughes commented 1 year ago

Good news :)

As of https://github.com/cmhughes/latexindent.pl/commit/a54b7be45606d07518dd86308212852111710991 the text wrap routine has been upgraded.

Using the settings

defaultIndent: '    '

modifyLineBreaks:
    textWrapOptions:
        columns: 100
        when: after                          # <!---- NEW BIT

gives the output

\begin{document}
\begin{enumerate}[label=(\alph*)]
    \item Lorem ipsum dolor sit amet, consectetur adipiscing elit, cras
          imperdiet viv ris et molestie. Nulla molestie diam quam, ac molestie
          velit rhoncus placerat.
          \begin{enumerate}[label=(\roman*)]
              \item Lorem ipsum dolor sit amet, consectetur adipiscing elit,
                    cras imperdiet viv ris et molestie. Nulla molestie diam
                    quam, ac molestie velit rhoncus placerat.
          \end{enumerate}
\end{enumerate}
\end{document}
----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|----|
   5   10   15   20   25   30   35   40   45   50   55   60   65   70   75   80   85   90   95   100   105   110

This will be part of the next release, hopefully in early 2023 :)