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

Avoid reorganizing some trailing comments #489

Closed peske closed 10 months ago

peske commented 10 months ago

First: thanks for the great work!

Problem

Let's say we have a paragraph like (note the trailing comment):

I'm writing about the minimal value of some beautiful quantity, and
I put it in inline math like $Q_{min}$. After that I write more and more % chktex 35
about it. And more, and more, and more, and more. And even more.

Note that comment % chktex 35 is there to disable a particular ChkTeX warning. (It warns me that I should probably use \min instead of simple text min, which is not true in this particular case.) With the comment in place, the warning is disabled and everything is OK. However, if I do latexindent with -m switch, with configured wrapping, I get something like (note the comment location):

I'm writing about the minimal value of some beautiful
quantity, and I put it in inline math like $Q_{min}$. After
that I write more and more about it. And more, and
more, and more, and more. And even more.% chktex 35

The warning reappears since the comment is not in the same line as the part that causes the warning. (And my CI/CD start failing :)) The only workaround currently is to disable this warning on the file level instead of on the line level. However, this obviously isn't an ideal solution - we wanted to disable the error only for one particular occurrence, not for the whole file.

Requested feature

It would be ideal if the output looks like (note the comment location):

I'm writing about the minimal value of some beautiful
quantity, and I put it in inline math like $Q_{min}$. After % chktex 35
that I write more and more about it. And more, and
more, and more, and more. And even more.

Implementation

I'm aware that implementing fixed comments while wrapping the block is not a trivial problem - the question is where to put the comment when the commented line is wrapped. Logically this can be solved in several different ways, some being simple, some trying to be "smart". Nevertheless, one possible implementation (that I would like to see) is to avoid moving certain comments if the command does not change the uncommented part. In other words, if a particular block needs to be changed by wrapping lines differently, then keep the current behavior (all the comments go after the end of the block). But if the block does not need any changes in line wrapping, avoid moving some trailing comments. This can be configurable by using regex, in the same way it is done for fineTuning.trailingComments.afterComment.

Thanks!

cmhughes commented 10 months ago

Thanks for this, it's an interesting situation :)

I'm not familiar ChkTeX but it sounds like it needs specific comments tied to specific lines to indicate specific things. Your proposal of trying to keep comments in precisely the right place during text wrapping sounds very difficult.

Instead, I propose a solution using the replacement switch. Specific details:

starting text

I'm writing about the minimal value of some beautiful quantity, and
I put it in inline math like $Q_{min}$. After that I write more and more % chktex 35
about it. And more, and more, and more, and more. And even more.

latexindent.yaml

modifyLineBreaks:
  textWrapOptions:
    columns: 60

replacements:
  - 
    when: after
    substitution: |-
      s/(\$Q_\{min\}\$.*$)/$1 % chktex 35/mg

call to latexindent.pl

latexindent.pl -l -m -r issue-489.tex

output

I'm writing about the minimal value of some beautiful
quantity, and I put it in inline math like $Q_{min}$. After % chktex 35
that I write more and more about it. And more, and more,
and more, and more. And even more.% chktex 35

comments

These settings add the % chktex 35 to the end of the line in which the $Q_{min}$ part is.

Perhaps you could combine this by removing the existing % chktex 35 comments beofre indentation:

modifyLineBreaks:
  textWrapOptions:
    columns: 60

replacements:
  - 
    when: before
    substitution: |-
      s/%\h*chktex\h*35//sg
  - 
    when: after
    substitution: |-
      s/(\$Q_\{min\}\$.*$)/$1 % chktex 35/mg

gives

I'm writing about the minimal value of some beautiful
quantity, and I put it in inline math like $Q_{min}$. After % chktex 35
that I write more and more about it. And more, and more,
and more, and more. And even more.
peske commented 10 months ago

Wow! Fast response and effective solution! It solved my problem. I'm closing this issue.

Thank you very much @cmhughes!

cmhughes commented 10 months ago

Great stuff, glad it helped!