47

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.

Example:

\newcommand\examplemacro{%
    \somecommand%
    \othercommand%
}

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

\newcommand{\examplemacro}{
    \somecommand{}
    \othercommand{}
}

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

DevSolar
  • 7,857

3 Answers3

36

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:

\newcommand\examplemacro{%
    \somecommand
    \commandwitharg{somearg}%
}

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.

Martin Scharrer
  • 262,582
  • 7
    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 09:37
  • 1
    @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 09: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 09: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 09:44
  • @DevSolar: No \newcommand{\examplemacro}{...} and \newcommand\examplemacro{...} is the same. I was talking about the usage of \examplemacro not it's definition. I also wrote {\somecommand} not {\examplemacro}. An expanding context is when macros are fully expanded until only non-expandable elements remain. This is done when writing to auxiliary files, but also when macro names are formed from user input, like in \label etc. You can also do it manually using \edef, e.g. \def\B{Hello}\edef\A{\B{}\B{}} will make \A to Hello{}Hello{}, but you wanted to get "HelloHello". – Martin Scharrer Jan 13 '12 at 09:50
  • 2
    @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 09:53
  • @whlt3: My bad. I actually don't use \def (TeX) at all and was extrapolating from \newcommand (LaTeX)... I'm still feeling my way around the minefield that is TeX-vs-LaTeX. ;-) – DevSolar Jan 13 '12 at 09:54
  • @MartinScharrer: I know that they are not actually searched for, hence the "'s around searched for. What I meant is, TeX goes into space skipping mode after a control word. It will change to state N if a new line is started and to state M when a character of category 1–4, 6–8 or 11–13 is encountered. So the {} are not necessary, but there has to be a character with one of those codes to have TeX go into mode M as far as I know the only option that is not printed itself are the open and close braces. Anyway, the point was that you need {} after a cs to get a space after it. – Roelof Spijker Jan 13 '12 at 10:02
  • @MartinScharrer: So the bottom line is: Always using braces in \newcommand{\examplemacro} is OK, but in the usage of a macro I shouldn't use empty braces for macros that don't take arguments lest I get unwanted spaces? (This somewhat contradicts http://tex.stackexchange.com/questions/31091...) – DevSolar Jan 13 '12 at 10:03
  • @MartinScharrer: I was merely pointing out that \def{\x}{x} is invalid syntax. I worded it poorly though, it was not my intention to dissuade him from using braces within \newcommand. I removed the comment, since it does indeed more bad than good. Also, the amount of comments is becoming confusing :) – Roelof Spijker Jan 13 '12 at 10:07
  • @DevSolar: If you want to preserve the space after a macro you can use {} after it, or \ or \space, etc. Like \LaTeX document must be written as \LaTeX\ document or \LaTeX{} document to get the required space. But normally people do not want to have spaces after every macro usage, so they don't add {} all the time but % where it is required. – Martin Scharrer Jan 13 '12 at 10:24
19

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:

\newcommand{\examplemacro}{%
    \somecommand{}%
    \othercommand
}

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

\newcommand\test{a}

will work but

\newcommand{\test}b

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.

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 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 09: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 09:46
5

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.

rekire
  • 1,454
  • 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 09: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