19

when would I use one over the other? Are they the same?

2 Answers 2

17

They're not the same at all: % will comment out the rest of the line, including the line break, while \relax is just mostly a no-op and you're using that spaces (including line breaks) after control sequences are discarded.

Frankly, I can't see any situation where you'd not want to use %.

3
  • 1
    Here's one place where you want to \relax instead of %: \relax can be gobbled. % can't. The next live thing after the % will be gobbled. Jun 14, 2011 at 8:06
  • 3
    In an expanding context you really don't want to use \relax just to get rid of spaces! Jun 14, 2011 at 8:34
  • @David: point taken, neither can % end a delimited argument because it's removed too early. Can't think why I'd be at the end of the line and unconditionally gobbling something, but maybe that occurs if one doesn't go the "usual" routine of making ^M active. Jun 14, 2011 at 12:42
6

I think your forgot "a space". When you write % it's after a space or not ? Like Ulrich says % and \relax are not the same. % will comment out the rest of the line but you can put a space before the %. Now the problem is to use a \relax or a space. I try to find an answer about this question, I read something about this, but I can't find it.

The next example comes from the book Advanced TexBook by D Salomon :

 \def\step{\advance\temp by\ifodd\pageno 1\else2\fi} 

This macro terminates with a \fi. Does it still need a space or a \relax? if

The expansion of \step gives either \advance by 1 or \advance by 2. After a number a \relax is necessary.

 \def\step{\advance\temp by\ifodd\pageno 1\else2\fi\relax}

The space is not enough because with

\def\step{\advance\temp by\ifodd\pageno 1\else2\fi }

expansions of \step will not include the space.

Remark : When TeX encounters digits, it expects the number to be followed by an unambiguous terminator (text, space, punctuation). if after the digits there is a command, this command is immediately executed in the attempt to find others digits.

10
  • \def\step{\advance\temp by\ifodd\pageno1 \else2 \fi} should work. Mine has one extra token though.
    – TH.
    Jun 14, 2011 at 9:45
  • No you get an error with \def\date{10/06/2011} \par \date \par \the\temp \par \step \date \par \the\temp Jun 14, 2011 at 11:38
  • I got no error and the output of \the\temp was 0 followed by 1. What error did you get?
    – TH.
    Jun 14, 2011 at 12:50
  • I should point out that when TeX is looking for a number, it looks for several specific things including octal, decimal, and hexadecimal constants. It stops looking for additional numbers (or letters in the case of hex) as soon as it encounters an unexpandable token that isn't one. A space counts.
    – TH.
    Jun 14, 2011 at 12:52
  • I use TeX with \newcount\temp \temp=0 after \step \date \par \the\temp gives 110 Jun 14, 2011 at 12:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .