Closed marcin-serwin closed 2 years ago
Apologies for the delay, I'll get to this soon hopefully.
The default behaviour is for the text wrapping routine of latexindent.pl
to join lines using a space; this seems like the most sensible default.
For your desired output, there are at least two options.
modifyLineBreaks:
textWrapOptions:
columns: 70
blocksEndBefore:
other: |-
(?x)
\\begin\{
|
\\\[
|
\\end\{
|
\\index
and then calling
latexindent.pl -l -m myfile.tex
gives
\documentclass{article}
\begin{document}
Something%
\index{something}.
\end{document}
The above yaml instructs the text wrapping routine to stop at \index
.
modifyLineBreaks:
textWrapOptions:
columns: 70
replacements:
-
when: after
substitution: |-
s/([a-zA-Z])\h+(\\index)/$1$2/sg
and then calling
latexindent.pl -rv -l -m myfile.tex
gives
\documentclass{article}
\begin{document}
Something\index{something}.%
\end{document}
The replacement switch is used here to remove spaces between letters ([a-zA-Z]
) and the \index
command.
Thanks for the quick answer and the proposed solutions. My point was a bit more general though. While it is true that replacing newlines by spaces is a reasonable behavior in most cases, this is not the case if the line is ended by comment. This tells LaTeX that next newline should be ignored, so the latexinndent
transformation changes document, not just the source code formatting. While the proposed solutions work fine for lines starting with \index
command they require to specify exceptions case by case.
In general they wouldn't for example differentiate between these two situations:
foo%
bar
and
foo % this is a comment
bar
which communicate different intent on the part of the author.
So what about
\begin{myenv}
foo%
bar
\end{myenv}
Which changes to
begin{myenv}
foo%
bar
\end{myenv}
Again, this will change the output....
Spaces at the beginning of the line are ignored by LaTeX (unless it is verbatim environment, which can be controlled in localSettings
) so it wouldn't change the output.
OK, understood. I'll explore this.
For my reference :split at comment regex, test for trailing space.
As of https://github.com/cmhughes/latexindent.pl/commit/079b219f42c17073325ca56ab8308f83201a2294 the text wrap routine has been updated so that starting with
\documentclass{article}
\begin{document}
Something%
\index{something}.
\end{document}
and using your YAML settings, gives
\documentclass{article}
\begin{document}
Something\index{something}.%
\end{document}
I'll be getting this released and uploaded to ctan.
Thanks for highlighting this!
I've released this at https://github.com/cmhughes/latexindent.pl/releases/tag/V3.17.3 and uploaded it to ctan :) Thanks again!
For reference, dedicated part of the documentation
original .tex code
yaml settings
actual/given output
desired or expected output
or
anything else
The
-m
switch with text wrapping introduces additional spaces when trailing comments are present. This may change the document in some cases. For example if pagebreak occurs between theSomething
and\index{something}
then the index entry will point to the wrong page.