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

Text wrapping doesn't work inside an environment #432

Closed Andrew15-5 closed 1 year ago

Andrew15-5 commented 1 year ago

original .tex code

\begin{anyenv}
  text text text text text text text text text text text text text text text text text text
\end{anyenv}

yaml settings

defaultIndent: "  "
modifyLineBreaks:
  textWrapOptions:
    columns: 81

actual/given output

\begin{anyenv}
  text text text text text text text text text text text text text text text text text text
\end{anyenv}

desired or expected output

\begin{anyenv}
  text text text text text text text text text text text text text text text
  text text text
\end{anyenv}

anything else

I'm sorry but the regex https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html?highlight=brackets#lst-tw-bf-myenv-yaml looks very confusing (aside from a massive size) and hard to understand (if I'm even referencing the right thing for the issue). Hence, I need help with the problem. I also don't understand if I have to do this long regex for each environment name (it will become even bigger).

I need to format paragraphs inside any environment (center, minipage, flushright etc.) — add linebreaks at 81st column. Well, not exactly any environment (exceptions are any verbatim environments and similar) but you know what I mean.

P.S. Sorry for so many issues, I hope I'm not irritating you or anything. :P
cmhughes commented 1 year ago

Yes, that's the one to start from.

modifyLineBreaks:
    textWrapOptions:
        blocksFollow:
           other: |-
             (?x)
                \\\]
                |
                \\item(?:\h|\[)
                |
                \\begin\{(?:minipage|flushright|myenv)\} # <--- new bit
Andrew15-5 commented 1 year ago

Could you please describe what this regex actually does/consists of? I know, for example, what \\item(?:\h|\[) or \\begin\{(?:minipage|flushright|myenv)\} mean (I think \\\] refers to \item[...]). But all the other bits (i.e., |-, (?x), |, linefeed \n, long indentations) I don't really understand. I'm guessing that | is the same as in (a|b|c) but there is no parenthesis and there are big indentations + new lines. Maybe it is something that is ignored and simply done for distinguishing between different bits of regex that are actually used by the formatter.

If the answer already exists somewhere, please provide a link.

cmhughes commented 1 year ago

Can you look at the details I give in the following :

https://latexindentpl.readthedocs.io/en/latest/sec-fine-tuning.html#fine-tuning

https://latexindentpl.readthedocs.io/en/latest/sec-replacements.html#lst-multi-line

On Thu, 9 Mar 2023, 13:00 Andrew Voynov, @.***> wrote:

Could you please describe what this regex actually does/consists of? I know, for example, what \item(?:\h|[) or \begin{(?:minipage|flushright|myenv)} mean (I think \] refers to \item[...]). But all the other bits (i.e., |-, (?x), |, linefeed \n, long indentations) I don't really understand. I'm guessing that | is the same as in (a|b|c) but there is no parenthesis and there are big indentations

  • new lines. Maybe it is something that is ignored and simply done for distinguishing between different bits of regex that are actually used by the formatter.

— Reply to this email directly, view it on GitHub https://github.com/cmhughes/latexindent.pl/issues/432#issuecomment-1462023223, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYFCBURJWZSTHNNZXZDW3HH5DANCNFSM6AAAAAAVUKJFIE . You are receiving this because you commented.Message ID: @.***>

Andrew15-5 commented 1 year ago

Thank you. Now I more understand than not understand.

I found a new issue where I can't wrap text inside \caption{...} and then changed blocksBeginWith:other: from "[а-яёА-ЯЁ]" to:

modifyLineBreaks:
  textWrapOptions:
    blocksBeginWith:
      other: |-
        (?x)
        [а-яёА-ЯЁ]
        |
        \\caption\{

And it works. But only outside any environment. If I have a simple:

\begin{table}
  \caption{text text text text text text text text text text text text text text text text}
\end{table}

It doesn't work. But then I added this:

modifyLineBreaks:
  textWrapOptions:
    blocksFollow:
      other: |-
        (?x)
        \\\]
        |
        \\item(?:\h|\[)
        |
        \\begin\{(?:table)\}(?:\[.*?\])?

And now it works, good. But it doesn't add the indent:

\begin{table}
  \caption{text text text text text text text text text text text text text text
  text text}
\end{table}

If I remove \\caption\{ then it will add the indent:

\begin{table}
  \caption{text text text text text text text text text text text text text text
    text text}
\end{table}

But if I straighten up the \caption{...} line (back into a single line), then it again wouldn't wrap the text.

So I have to

  1. add \\caption\{
  2. format
  3. remove \\caption\{
  4. format

to get the desirable formatting. Is there a way without modifying the config back and forth?

cmhughes commented 1 year ago

It looks like you want

modifyLineBreaks:
  textWrapOptions:
    blocksFollow:
      other: |-
        (?x)
        \\\]
        |
        \\item(?:\h|\[)
        |
        \\begin\{(?:table)\}(?:\[.*?\])
        |
        \\caption\{
Andrew15-5 commented 1 year ago

Almost. This is an output:

\begin{table}
  \caption{text text text text text text text text text text text text text text
         text text}
\end{table}

So it's not 2 spaces, and it's not after the \caption{ (9 spaces). Maybe the latter would be better.

Andrew15-5 commented 1 year ago

If I change \\begin\{(?:table)\}(?:\[.*?\])? to \\begin\{(?:table)\}(?:\[.*?\])?\h then it works:

\begin{table}
  \caption{text text text text text text text text text text text text text text
           text text}
\end{table}

But I added \h in the "wrong" place. What if I don't want to add the same formatting for a different macro (e.g., \notACaption{...})? In other words, I want low coupling between table environment and caption macro (i.e., add \h to \caption instead of table). Maybe I don't something (maybe it is the right way).

cmhughes commented 1 year ago

You need to specify blocksBeginWith and blocksFollow to suit your needs.

Andrew15-5 commented 1 year ago

I admit I'm so lost I don't even really know the difference sometime, but in the end I think I found a solution (I don't know if it is a correct one):

modifyLineBreaks:
  textWrapOptions:
    blocksBeginWith:
      other: |-
        (?x)
        [а-яёА-ЯЁ]
        |
        \\begin\{(?:table)\}(?:\[.*?\])?
    blocksFollow:
      other: |-
        (?x)
        \\\]
        |
        \\item(?:\h|\[)
        |
        \\begin\{(?:center|flushleft|flushright|minipage)\}
        |
        \\(?:caption)\{