10

The code

\documentclass{article}

\begin{document}
\begingroup
\catcode`@\active
\def@#1\par{<#1>}

@some text


\endgroup
\end{document}

works flawlessly. Why not so when I replace it by the following?

\def@#1\space{<#1>}
Werner
  • 603,163
Gaussler
  • 12,801
  • \par appears when TeX tokenizes the end of lines (so the definition of @ actually finds the \par token because TeX inserts it). \space is a macro, but it doesn't appear “magically” like \par. You can use \def@#1 {<#1>}. – Manuel Apr 02 '15 at 15:08

1 Answers1

11

\def@#1\space{<#1>} doesn't work, unless TeX finds the token \space before the current paragraph ends.

Recall that TeX doesn't do expansion when

  1. absorbing the parameter text of a macro
  2. looking for arguments to a macro

On the other hand,

\def@#1 {<#1>}

will work. Here's a Plain TeX example (so I'll use \tt):

\begingroup
\catcode`@\active
\def@#1 {{\tt<#1>}}

@some text

@some

\endgroup

\bye

enter image description here

However, you probably want to delimit the argument with the end-of-line. See Using end-of-line delimiter in plain Tex macro for this case.

There is a fundamental difference between \par and \space. The former is a primitive (unless redefined); TeX inserts automatically a \par token when it sees a category code 5 character when in state S (start of line), during the tokenization process. So

\def\foo#1\par{Something with #1}

and the call

\foo xyz

something else after a blank line

will correctly identify xyz (with a trailing space, though) as the argument to \foo.

The macro \space is defined by \def\space{ }, and there's no magic process that automatically inserts such a token.

egreg
  • 1,121,712