For example, I have a command which has a lot of options:


I want to split it to several lines so I could read the code better:


What is the proper way to do it?


End each line with a %. This will gobble the line-feed and all whitespace at the beginning of next line. Thus you'd get


to do what you want it to.

Edited To Add: As Geoffrey points out in a comment to this answer, in this context, this solution is overkill. Using % at the end of a line will always gobble up the newline and the leading whitespace — however, this is (as Geoffrey points out) irrelevant unless in a context where initial whitespace is influential. Thus, in a \documentclass, it can be omitted and Kit's original code used; while in a case like

  \foreach \p/\x/\y in {%
  } {
    \node [coordinate] (p\p) at (\x,\y) {};
% Do stuff with the defined coordinates

it becomes relevant, as otherwise the whitespace would be included with the definition of \p, and destroy the crafted coordinate names.

Plenty similar examples outside of TikZ exist — this was the example I could think of the quickest where it becomes relevant.

  • 10
    Mikael, technically you are correct. However, your solution is overkill. Don't bother with the %'s in places where TeX\LaTeX is not in (let's call it) typesetting mode, that is, where not tokenising the stream of characters for eventual output processing. Unless you have some very weird list processing command, most comma separated lists expansion rules treat newlines as spaces. So, the %'s are quite unnecessary in the context of your particular example. Most coders would omit then since they're quite a pain to read and write. – Geoffrey Jones Sep 12 '10 at 15:11
  • 1
    Right, Geoffrey. Thank you! I'm not quite proficient enough to know exactly when newlines are relevant and when they are not. – Mikael Vejdemo-Johansson Sep 12 '10 at 18:47
  • 1
    TeX (almost) always ignores spaces at the beginning of the line. I wrote a bit about TeX's three states (N, M, and S), which determine how TeX treats a space, here. – TH. Sep 12 '10 at 23:28
  • @TH.: yes, it does almost always ignore the spaces after a line break, but typically it does notice the line break; e.g. if in horizontal mode and in state M, it will generally insert the same thing it would have on encountering a space, will it not? – SamB Feb 22 '11 at 0:50
  • @SamB: Yes. TeX inserts an \endlinechar character at the end of every line (after stripping of trailing spaces). By default, this has category code 5. In state M, a catcode 5 token ends the line and inserts a space token (char code 32, catcode 10). In state N, it inserts a \par token and in state S, it does nothing. – TH. Feb 22 '11 at 4:06

If you are too lazy to type % at the end of each input line (or you think it looks ugly to do so), then a sneaky trick is to redefine \endlinechar. Another trick is to set the catcode of ^^M to 9 (ignore). In these cases if you do want a space at the end of the line you have to use "%" (space percent) at the line ending.


is equivalent to

    c %

is equivalent to

    c %

is equivalent to

{abc d}
  • How does that zap the initial spaces on the lines with b and c on them? – SamB Feb 22 '11 at 0:46
  • Isn't the ^^M thing platform-specific? – cfr Mar 8 '18 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.