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

noindent for single line patterns #448

Closed Rdeisenroth closed 1 year ago

Rdeisenroth commented 1 year ago

I am trying to get latexindent to accept this kind of indentation where i use a comment in order to mark a section or chapter end.

original .tex code

\chapter{Quaternionen} % S.176
    \todo{Quaternionen}
    \section{Vergleich mit anderen Darstellungsarten} % S.179
        \todo{Quaternionen}
    % end
% end

\chapter{Zusammenhang zwischen Rotationsmatrix, Drehvektor und Drehwinkel} % S.179
    \todo{Rotationsmatrix, Drehvektor und Drehwinkel}

    \section{Umwandlung von Drehvektor und Drehwinkel zu Rotationsmatrix} % S.179
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
    % end

    \section{Umwandlung von Rotationsmatrix zu Drehvektor und Drehwinkel} % S.179, S.180, S.181
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
    % end
% end

yaml settings

defaultIndent: "\t"
indentRules:
  chapter: "\t"
  section: "\t"
  item: "\t"

# set noAdditionalIndent globally for codeblocks
noAdditionalIndentGlobal:
  environments: 0
  commands: 1
  optionalArguments: 0
  mandatoryArguments: 0
  ifElseFi: 0
  items: 0
  keyEqualsValuesBracesBrackets: 1
  namedGroupingBracesBrackets: 1
  UnNamedGroupingBracesBrackets: 1
  specialBeginEnd: 0
  afterHeading: 0
  filecontents: 0

indentAfterHeadings:
  part:
    indentAfterThisHeading: 1
    level: 1
  chapter:
    indentAfterThisHeading: 1
    level: 2
  section:
    indentAfterThisHeading: 1
    level: 3
  subsection:
    indentAfterThisHeading: 1
    level: 4
  subsection*:
    indentAfterThisHeading: 1
    level: 4
  subsubsection:
    indentAfterThisHeading: 1
    level: 5
  paragraph:
    indentAfterThisHeading: 1
    level: 6
  subparagraph:
    indentAfterThisHeading: 1
    level: 7

actual/given output

\chapter{Quaternionen} % S.176
    \todo{Quaternionen}
    \section{Vergleich mit anderen Darstellungsarten} % S.179
        \todo{Quaternionen}
        % end
        % end

\chapter{Zusammenhang zwischen Rotationsmatrix, Drehvektor und Drehwinkel} % S.179
    \todo{Rotationsmatrix, Drehvektor und Drehwinkel}

    \section{Umwandlung von Drehvektor und Drehwinkel zu Rotationsmatrix} % S.179
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
        % end

    \section{Umwandlung von Rotationsmatrix zu Drehvektor und Drehwinkel} % S.179, S.180, S.181
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
        % end
        % end

desired or expected output

\chapter{Quaternionen} % S.176
    \todo{Quaternionen}
    \section{Vergleich mit anderen Darstellungsarten} % S.179
        \todo{Quaternionen}
    % end
% end

\chapter{Zusammenhang zwischen Rotationsmatrix, Drehvektor und Drehwinkel} % S.179
    \todo{Rotationsmatrix, Drehvektor und Drehwinkel}

    \section{Umwandlung von Drehvektor und Drehwinkel zu Rotationsmatrix} % S.179
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
    % end

    \section{Umwandlung von Rotationsmatrix zu Drehvektor und Drehwinkel} % S.179, S.180, S.181
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
    % end
% end

anything else

I tried to define a custom noIndentBlock:

noIndentBlock:
  custom:
    name: '\R\h*%\h*end\h*'
    lookForThis: 1

and seperately a specialBeginEnd:

specialBeginEnd:
  customSection:
    begin: '\\section\*?'
    end: '%\h*end\h*'
    lookForThis: 1
  specialBeforeCommand: 0

which unfortunately gives the same result.

If there is some way to turn off indentation for a pattern like this it would be enough for my usecase. However ideally it would auto-indent the comments. Apologies if i am overlooking something.

cmhughes commented 1 year ago

Thanks for this, I hope to get to it soon.

cmhughes commented 1 year ago

As of https://github.com/cmhughes/latexindent.pl/commit/a7c87234807331ea445d1d17ef5e9bf561810c6d I've updated the fineTuning routine so that you can specify

defaultIndent: "\t"

specialBeginEnd:
  customSection:
    begin: \\(?:section|chapter)
    end: \%\h+end
  specialBeforeCommand: 1

fineTuning:
    trailingComments:
      notPreceededBy: (?<!\\)
      afterComment: (?!(?:\hend)).*?    # <!----- NEW BIT

and then you receive

\chapter{Quaternionen} % S.176
    \todo{Quaternionen}
    \section{Vergleich mit anderen Darstellungsarten} % S.179
        \todo{Quaternionen}
    % end
% end

\chapter{Zusammenhang zwischen Rotationsmatrix, Drehvektor und Drehwinkel} % S.179
    \todo{Rotationsmatrix, Drehvektor und Drehwinkel}

    \section{Umwandlung von Drehvektor und Drehwinkel zu Rotationsmatrix} % S.179
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
    % end

    \section{Umwandlung von Rotationsmatrix zu Drehvektor und Drehwinkel} % S.179, S.180, S.181
        \todo{Rotationsmatrix, Drehvektor und Drehwinkel}
    % end
% end

I'll get this released soon; feel free to pull from develop in the mean time.

Thanks again!

cmhughes commented 1 year ago

Implemented as of https://github.com/cmhughes/latexindent.pl/releases/tag/V3.22 upload to ctan imminent.

cmhughes commented 1 year ago

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