TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

I found two things peculiar in the common style of macro definitions:

  1. Ending lines in macro definitions with '%';
  2. Not wrapping a macro name in braces.



Being a software engineer first and a LaTeX user a distant second, I would expect:


Is there a reason for doing it the former way, or is it merely a matter of habit?

share|improve this question

marked as duplicate by Paul Gaborit, Werner macros Dec 10 '15 at 7:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

other (possible) duplicate: What is the use of percent signs at the end of lines? – Federico Poloni Jan 13 '12 at 11:24
up vote 28 down vote accepted

The comment at the end of certain lines removes the space otherwise inserted by the end-of-line character (which is usually turned into a space). It is not needed after macros (control words), because they remove spaces themselves. You do not need braces {} after a macro. I think you relate here to the () added in programming languages like C after functions. Macros aren't functions, even if they can often used in similar ways. If your \somecommand doesn't take an argument the {} will not be touched by it and the source code line break after it will be turned into an normally unwanted space. Normally a orphaned {} doesn't hurt, but in certain places, like in an expandable context, they might cause trouble. You should definitely not wrap all macros in braces, like {\somecommand}, because that makes them locally scoped.

So you should write your macros like:


Here the two % are required to remove the end-of-line-turned-space, but the \somecommand line doesn't require one because all spaces after control words are removed. This is done, because you might need to add at least one space after it to separate it from following text and multiple spaces are always reduced to one.

It should be noted that these (La)TeX rules can be dynamically changed, e.g. all line endings or even all spaces can be ignored by changing the required \endlinechar and catcodes. This is done for the LaTeX3 syntax, which avoids the need for this kind of comments.

share|improve this answer
It might be good to note that the {} after a macro are "searched for". That is to say, \def\x{x}\x y will result in xy instead of x y, because TeX goes into space skipping mode after a control sequence. – Roelof Spijker Jan 13 '12 at 9:37
@wh1t3: That's why I got into the habit of using {} everywhere (as in: \def{\x}{x}\x{} y, which yields the expected result of x y) - just like I use parantheses in every programming conditional "just in case" - and was wondering why common style derived from this... – DevSolar Jan 13 '12 at 9:39
@MartinScharrer: Enlightening. I just didn't catch your meaning when you said that \newcommand{\examplemacro}{...} would make it "locally scoped", and what exactly is meant by "expandable context". – DevSolar Jan 13 '12 at 9:44
@wh1t3: I added this in the meantime anyway. Note that "{} are seached for" is not correct in the general case. All spaces after a control word (normal macros/primitives) are always removed. You might mean the fact, that TeX also skips spaces when searching for a balanced text for an argument. E.g. \newcommand\a[2]{..}\a {1} {2} , will remove the spaces after \a (in any case), but also the spaces between {1} and {2} because it looks for the second argument. However \def\x{x}\x y simply must remove the space between \x and y because the user can't write \xy here. – Martin Scharrer Jan 13 '12 at 9:44
@wh1t3: Why are you now focusing on TeX-style definitions? That is more confusing then helpful. He uses \newcommand which reads \examplemacro as argument and so it can be wrapped in {}. It's actually the official LaTeX syntax. Just because \def requires the macro name directly and doesn't read it as an argument, it isn't a reason to tell people not to use them with \newcommand. – Martin Scharrer Jan 13 '12 at 9:53

I am not 100% sure but in one of my macros I could so suppress unwanted spaces in the output.

About question 2 why the braces are missing, I think because they are optional.

share|improve this answer
Ahhh... the line break becoming part of the macro, being replaced with a horizontal whitespace? And the '%' commenting out the line break? That makes sense. Thank you! (Keeping the question open for a while in case there's some input on the {}, but this looks like a winner. ;-) ) – DevSolar Jan 13 '12 at 9:30
I'm really sorry... you were first, but Martin scored on elaboration. ;-) – DevSolar Jan 13 '12 at 10:31
No problem his answer was much better. I upvoted his answer too. – rekire Jan 13 '12 at 11:04

As Martin has already commented, adding % to the end of a line prevents TeX from turning the end-of-line character into a space. This is needed at the end of every line except those where TeX is already skipping spaces, for example after a macro name which takes no arguments:


Here, the line with \othercommand does not need a % as TeX will skip the space here anyway.

The use of braces for the first argument of \newcommand is 'optional' due to the way TeX grabs arguments. We use a brace group in LaTeX to indicate a single argument, but TeX will grab either a brace group or a single token as an undelimited argument. In the example, \examplemacro is a single token (a control sequence), and so TeX will grab it in one go.

There are places where the braces are required. For example


will work but


will not, as the 'replacement text' argument for \newcommand has to be given in braces.

As LaTeX is built on TeX, some TeX ideas leak through. The formal LaTeX syntax always includes braces, but as TeX does not always need them people 'in the know' take shortcuts.

share|improve this answer
Acknowledged. I just got bitten by "shortcuts" so often that I try to avoid them altogether. In programming I put parentheses around all subconditionals, even if operator precedence should make it clear. I was trying to apply the same style to LaTeX. ;-) – DevSolar Jan 13 '12 at 9:45
With my 'LaTeX3' hat on, I'd point out that in the LaTeX3 programming environment, spaces are ignored and so % is not required. Also, we differentiate more clearly between cases where a single token is required and cases where the argument may contain multiple tokens and so braces are part of the formal syntax. – Joseph Wright Jan 13 '12 at 9:46

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