3

In macros it makes sense to put percent signs at the end of line in order to avoid any line break characters in the macro definition. For example, a macro with a multi-line definition could look like this:

\newcommand{\provideName}[3]{%
  \providecommand{#1}{#2}%
  \addto\captionsenglish{\renewcommand{#1}{#2}}%
  \addto\captionsngerman{\renewcommand{#1}{#3}}%
}

Now I use the latexindent tool to format my code automatically. Usually, this works pretty well but in this case the formatted code looks like:

\newcommand{\provideName}[3]{%
  \providecommand{#1}{#2}%

  \addto\captionsenglish{\renewcommand{#1}{#2}}%

  \addto\captionsngerman{\renewcommand{#1}{#3}}%
}

Why are those extra newlines generated? And how can I turn off this behavior? (The percent signs are common practice, right? Or can I safely remove them?)

localSettings.yaml

defaultIndent: '  '
onlyOneBackUp: 1
modifyLineBreaks:
textWrapOptions:
    columns: 78

latexindent parameters

latexindent -w -m --cruft=build --local=./localSettings.yaml myfile.sty

Updated localSettings.yaml

As suggested by cmhughes, I added the perCodeBlockBasis option to get:

defaultIndent: '  '
onlyOneBackUp: 1
modifyLineBreaks:
textWrapOptions:
    columns: 78
    perCodeBlockBasis: 1

This results in the following code under texlive-2017:

\newcommand{\provideName}[3]{%
  \providecommand{#1}{#2}%

  \addto\captionsenglish{\renewcommand{#1}{#2}}%latexindenttrailingcomment36-EN
  D

 \addto\captionsngerman{\renewcommand{#1}{#3}}%latexindenttrailingcomment37-EN
  D
}
  • 1
    It's not just about common practice: they are necessary if the macro will be expanded while typesetting. There might be places where they are not relevant, and there are places where they are wrong (i.e. if TeX is looking for a number). If your macro \provideName can occur only in the preamble then you can remove them; otherwise not. See also What is the use of percent signs (%) at the end of lines? – campa Dec 10 '19 at 14:28
  • 1
    Thanks for the clarification. In this case I can remove the percent signs here and everything is fine. Still, isn't the behavior of latexindent peculiar? – carlosvalderrama Dec 10 '19 at 14:38
  • No idea, never used latexindent in my life. But running it on this snippet does not yield the result you show. Sorry, can't help there... – campa Dec 10 '19 at 14:39
  • 2
    When I start with your original code block, and run latexindent.exe myfile.tex then I don't receive any new blank lines. Can you post complete details of the settings that you're using? The script shouldn't modify line breaks unless you're calling it with the -m switch, and even then, you would have to specify some settings to receive the output you're getting. Please post your settings files, and the way that you're calling the script and I'll do my best to help :) – cmhughes Dec 10 '19 at 19:07
  • @cmhughes See the updated post! Thanks! – carlosvalderrama Dec 10 '19 at 19:14
  • Can you try the "huge" option as detailed in https://github.com/cmhughes/latexindent.pl/pull/173 – cmhughes Dec 10 '19 at 19:28
  • @cmhughes Added "huge: overflow" to the textWrapOptions, but still the same behavior. – carlosvalderrama Dec 10 '19 at 19:36
  • 1
    can you try perCodeBlockBasis: 1 https://latexindentpl.readthedocs.io/en/latest/sec-the-m-switch.html#lst-textwrap3-yaml – cmhughes Dec 10 '19 at 21:46
  • @cmhughes There are no extra newlines with perCodeBlockBasis: 1, which is the desired behavior. Thanks a lot -- if you care to write an answer, I'll accept it! – carlosvalderrama Dec 11 '19 at 16:25
  • @cmhughes Works fine under texlive2018 and 2019, but fails under texlive2017 (see updated question). Now this looks really strange! – carlosvalderrama Dec 11 '19 at 19:53
  • 1
    I'm not surprised it doesn't work for 2017, as texlive doesn't update once frozen. – cmhughes Dec 11 '19 at 20:25
  • @cmhughes OK, thanks a lot! And thanks for the cool latexindent tool! – carlosvalderrama Dec 13 '19 at 18:47
  • Very glad you like it :) – cmhughes Dec 13 '19 at 20:15

0 Answers0