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

Output of `latexindent` isn't stable (specialBeginEnd) #477

Closed arodomanov closed 11 months ago

arodomanov commented 11 months ago

Hi,

I'm trying to make latexindent automatically indent the text inside any (round) parentheses (in other words, to treat ( and ) as the beginning and end, respectively, of a certain environment).

To achieve this, I've tried to declare a specialBeginEnd block (see the latexindent.yaml file below).

At first, this seemed to work fine and produced the result I wanted. However, I then noticed that the result isn't stable. Specifically, the output changes if you run latexindent again (one or several times) without changing anything. In particular, I managed to obtain two different outputs after running latexindent several times on the same file with the same settings and by using the same command (see below).

From my limited understanding (after inspecting the log file), it looks as if the problem is that each run of latexindent joins the settings from specialBeginEnd in an arbitrary order which results in different internal regular expressions.

Please confirm if it's a bug, or if I'm misusing the specialBeginEnd feature.

Original .tex code (source.tex)

$
  a
  +
  (
    b + c
  )
  =
  d
$
and
$
  e + f = g
$

yaml settings (latexindent.yaml)

defaultIndent: "  "

specialBeginEnd:
  specialBeforeCommand: 1

  parentheses:
    begin: \(
    end: \)
    lookForThis: 1

The command used to run latexindent

latexindent -l latexindent.yaml -o=+-mod.tex source.tex

Actual output 1 [good] (source-mod.tex)

$
  a
  +
  (
    b + c
  )
  =
  d
$
and
$
  e + f = g
$

Actual output 2 [bad] (source-mod.tex)

$
  a
  +
  (
    b + c
  )
  =
  d
  $
    and
  $
  e + f = g
$

Desired or expected output

The desired output is output 1 [good] presented above.

Version of latexindent

3.23.1, 2023-09-13
cmhughes commented 11 months ago

Many thanks, that's a helpful example.

Certainly not expected behaviour, and definitely a bug. I'll look into it.

cmhughes commented 11 months ago

Note to self, maybe try removing the g switch from the find and replace in

https://github.com/cmhughes/latexindent.pl/blob/main/LatexIndent/Special.pm

cmhughes commented 11 months ago

Note to self, maybe try removing the g switch from the find and replace in

https://github.com/cmhughes/latexindent.pl/blob/main/LatexIndent/Special.pm

This didn't work

cmhughes commented 11 months ago

Implemented and released at https://github.com/cmhughes/latexindent.pl/releases/tag/V3.23.2

thanks again!

arodomanov commented 11 months ago

Thanks a lot for such a quick fix!

cmhughes commented 11 months ago

You're welcome, and thank you for such a clear, concise example. Happy indenting!

On Mon, 25 Sep 2023, 14:11 Anton Rodomanov, @.***> wrote:

Thanks a lot for such a quick fix!

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