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
}
13
  • 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, 2019 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? Dec 10, 2019 at 14:38
  • 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, 2019 at 19:07
  • 1
    can you try perCodeBlockBasis: 1 latexindentpl.readthedocs.io/en/latest/…
    – cmhughes
    Dec 10, 2019 at 21:46
  • 1
    I'm not surprised it doesn't work for 2017, as texlive doesn't update once frozen.
    – cmhughes
    Dec 11, 2019 at 20:25

0

You must log in to answer this question.

Browse other questions tagged .