19

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

Dimples
  • 193

2 Answers2

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 %.

  • 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. – David Hammen Jun 14 '11 at 08:06
  • 3
    In an expanding context you really don't want to use \relax just to get rid of spaces! – Martin Scharrer Jun 14 '11 at 08: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. – Ulrich Schwarz Jun 14 '11 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.

Alain Matthes
  • 95,075
  • \def\step{\advance\temp by\ifodd\pageno1 \else2 \fi} should work. Mine has one extra token though. – TH. Jun 14 '11 at 09:45
  • No you get an error with \def\date{10/06/2011} \par \date \par \the\temp \par \step \date \par \the\temp – Alain Matthes Jun 14 '11 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 '11 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 '11 at 12:52
  • I use TeX with \newcount\temp \temp=0 after \step \date \par \the\temp gives 110 – Alain Matthes Jun 14 '11 at 12:57
  • I'm guessing you didn't copy my definition of \step correctly then. tex '\newcount\temp\temp=0 \def\step{\advance\temp by\ifodd\pageno1 \else2 \fi}\def\date{10/06/2011}\step \date \par \showthe\temp\bye' shows 1. – TH. Jun 14 '11 at 13:01
  • More simple 1. \def\x{12} 2. \count0=\x % 3. \the\count0 gives 123. it's the same error – Alain Matthes Jun 14 '11 at 13:07
  • Your comment is confusing because of the % there, but I think I know what you're trying to say. TeX expands macros looking for numbers. My original code does not have the flaw you describe as I was very careful to put spaces after 1 and 2. – TH. Jun 14 '11 at 13:31
  • @TH. Ok I understand but you example is different you put a space after 1 and 2 . My (Salomon) example does not have space after 1 and 2 so the problem is what to do after \fi : space or \relax. With my text ,the answer is relax because the space after \fi is not useful – Alain Matthes Jun 14 '11 at 13:38
  • Yes, I understood. In fact the first and third definition of \step in your answer are identical since the space after the \fi is ignored when TeX is parsing the replacement text. – TH. Jun 14 '11 at 19:36