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
864 stars 84 forks source link

modifyLineBreaks:environments:BodyStartsOnOwnLine:1 does not work with optional arguments #508

Closed Enivex closed 7 months ago

Enivex commented 7 months ago

Please provide the following when posting an issue:

original .tex code

\begin{theorem}[something] \label{something}
Theorem text
\end{theorem

yaml settings

-y=defaultIndent:'%INDENT%',modifyLineBreaks:environments:BeginStartsOnOwnLine:1;BodyStartsOnOwnLine:1;EndStartsOnOwnLine:1;EndFinishesWithLineBreak:1,modifyLineBreaks:specialBeginEnd:SpecialBeginStartsOnOwnLine:1;SpecialBodyStartsOnOwnLine:1;SpecialEndStartsOnOwnLine:1;SpecialEndFinishesWithLineBreak:1,specialBeginEnd:inlineMath:lookForThis:0,indentPreamble:1

actual/given output

\begin{theorem}[something] \label{something}
    Theorem text
\end{theorem

desired or expected output

\begin{theorem}[something]
    \label{something}
    Theorem text
\end{theorem

Please paste your desired/expected output here; that is, what do you want the output to look like

anything else

Please put any comments or anything else here :)

cmhughes commented 7 months ago

Thanks for this.

Using

modifyLineBreaks:
  commands:
    label: 
      CommandStartsOnOwnLine: 1

then we receive your desired output

\begin{theorem}[something]
\label{something}
Theorem text
\end{theorem
Enivex commented 7 months ago

Thanks for this.

Using

modifyLineBreaks:
  commands:
    label: 
      CommandStartsOnOwnLine: 1

then we receive your desired output

\begin{theorem}[something]
\label{something}
Theorem text
\end{theorem

Thanks for the workaround

Enivex commented 7 months ago

Regarding the closure of the issue, isn't this still a bug though?

cmhughes commented 7 months ago

This isn't a bug. If you want a command to start on its own line the command poly switch is the one to use.

If you want an optional argument to finish with a line break, then the optional argument poly switch is the one to use.

On Wed, 17 Jan 2024, 19:34 Enivex, @.***> wrote:

Regarding the closure of the issue, isn't this still a bug though?

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

Enivex commented 7 months ago

This isn't a bug. If you want a command to start on its own line the command poly switch is the one to use. If you want an optional argument to finish with a line break, then the optional argument poly switch is the one to use. On Wed, 17 Jan 2024, 19:34 Enivex, @.> wrote: Regarding the closure of the issue, isn't this still a bug though? — Reply to this email directly, view it on GitHub <#508 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYBWSXS6X4B757TS7WTYPARTRAVCNFSM6AAAAABB6UP5WOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJWGUZDCNJZHE . You are receiving this because you modified the open/close state.Message ID: @.>

The point is that modifyLineBreaks:environments:BodyStartsOnOwnLine:1 should make anything inside an environment start on the next line. This works as it should if there is no optional argument, i.e.

\begin{theorem} \label{something}
Theorem text
\end{theorem

will be formatted as

\begin{theorem}
    \label{something}
    Theorem text
\end{theorem

even without modifyLineBreaks:commands:label: CommandStartsOnOwnLine: 1.

Unless I'm missing something I don't understand why the presence of the optional argument to theorem should matter.

cmhughes commented 7 months ago

Have a look at the table at

https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#id69

It explains, visually, where each of the poly switches correspond.

On Wed, 17 Jan 2024, 20:44 Enivex, @.***> wrote:

This isn't a bug. If you want a command to start on its own line the command poly switch is the one to use. If you want an optional argument to finish with a line break, then the optional argument poly switch is the one to use. … <#m1470717740729905827> On Wed, 17 Jan 2024, 19:34 Enivex, @.> wrote: Regarding the closure of the issue, isn't this still a bug though? — Reply to this email directly, view it on GitHub <#508 (comment) https://github.com/cmhughes/latexindent.pl/issues/508#issuecomment-1896521599>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYBWSXS6X4B757TS7WTYPARTRAVCNFSM6AAAAABB6UP5WOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJWGUZDCNJZHE https://github.com/notifications/unsubscribe-auth/AAQ7CYBWSXS6X4B757TS7WTYPARTRAVCNFSM6AAAAABB6UP5WOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJWGUZDCNJZHE . You are receiving this because you modified the open/close state.Message ID: @.>

The point is that modifyLineBreaks:environments:BodyStartsOnOwnLine:1 should make anything inside an environment start on the next line. This works as it should if there is no optional argument, i.e.

\begin{theorem} \label{something} Theorem text \end{theorem

will be formatted as

\begin{theorem} \label{something} Theorem text \end{theorem

even without modifyLineBreaks:commands:label: CommandStartsOnOwnLine: 1.

Unless I'm missing something I don't understand why the presence of the optional argument to theorem should matter.

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

Enivex commented 7 months ago

Have a look at the table at https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#id69 It explains, visually, where each of the poly switches correspond. On Wed, 17 Jan 2024, 20:44 Enivex, @.> wrote: This isn't a bug. If you want a command to start on its own line the command poly switch is the one to use. If you want an optional argument to finish with a line break, then the optional argument poly switch is the one to use. … <#m1470717740729905827> On Wed, 17 Jan 2024, 19:34 Enivex, @.> wrote: Regarding the closure of the issue, isn't this still a bug though? — Reply to this email directly, view it on GitHub <#508 (comment) <#508 (comment)>>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYBWSXS6X4B757TS7WTYPARTRAVCNFSM6AAAAABB6UP5WOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJWGUZDCNJZHE https://github.com/notifications/unsubscribe-auth/AAQ7CYBWSXS6X4B757TS7WTYPARTRAVCNFSM6AAAAABB6UP5WOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJWGUZDCNJZHE . You are receiving this because you modified the open/close state.Message ID: @.> The point is that modifyLineBreaks:environments:BodyStartsOnOwnLine:1 should make anything inside an environment start on the next line. This works as it should if there is no optional argument, i.e. \begin{theorem} \label{something} Theorem text \end{theorem will be formatted as \begin{theorem} \label{something} Theorem text \end{theorem even without modifyLineBreaks:commands:label: CommandStartsOnOwnLine: 1. Unless I'm missing something I don't understand why the presence of the optional argument to theorem should matter. — Reply to this email directly, view it on GitHub <#508 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ7CYEDFN2B5FR4R7YRO2TYPAZ35AVCNFSM6AAAAABB6UP5WOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQOJWGY2TSMZTGI . You are receiving this because you modified the open/close state.Message ID: @.>

I'm sorry if I'm dense, but I really can't figure out how to get a linebreak after optional (or mandatory) arguments to an environment without also this applying to commands all over the place.

cmhughes commented 7 months ago

No worries :)

Here's a couple of options

option 1 (theorem only)

modifyLineBreaks:
  optionalArguments:
    theorem:
      RSqBFinishesWithLineBreak: 1        # -1,0,1,2,3,4

option 2 (all optional arguments)

modifyLineBreaks:
  optionalArguments:
    RSqBFinishesWithLineBreak: 1        # -1,0,1,2,3,4

both give

\begin{theorem}[something]
    \label{something}
    Theorem text
\end{theorem}
Enivex commented 7 months ago

Thanks, Option 1 will work, but is there a way to do it where I don't have to manually add an entry for every single environment? I just want it to apply to any \begin{environment}[optional argument]? Option 2 I have tried before, but it applies to literally any optional arguments to anything (even brackets in math)

cmhughes commented 7 months ago

option 3 (using replacement)

replacements:
  - 
   substitution: |-
     s/(\\begin\{[^}]+?\}\h*\[[^]]+?\])\h*(\\[a-zA-Z])/$1\n$2/sg

called using latexindent.pl -r -l myfile.tex

option 4

specialBeginEnd:
  specialBeforeCommand: 1
  myenv:
    begin: \\begin\{[^}]+?\}\h*\[[^]]+?\]
    end: \\end\{[^}]+?\}

modifyLineBreaks:
  specialBeginEnd:
    myenv:
      SpecialBodyStartsOnOwnLine: 1

fineTuning:
  environments:
    name: [1]+