3

I would like to implement a queue in the following way. A macro (variable?) \queue holds a string that may contain dots. Substrings separated by dots are the elements of the queue. Hence, the string "a.b.c" represents a queue with 3 elements, namely "a", "b" and "c". Then I want to define a macro that receives a queue as paramenter and return the queue without the first element.

When I define it as

\def\firstout#1.#2{#2}

(without any blank spaces in the line above) or

\def\firstout #1.#2{#2}

(with one or several blank spaces between \firstout and #1.#2{#2}) it works well, that is, the following code returns "b.c" as desired:

\def\queue{a.b.c}
\expandafter\firstout\queue

But, if I define it as

\def\firstout#1.#2 {#2}

(with a blank between \def\firstout#1.#2 and {#2}) or

\def\firstout #1.#2 {#2}

(with a blank after \def\firstout and another after #1.#2) then I receive the following error when I try to execute \expandafter\firstout\queue:

Paragraph ended before \firstout was complete.

Why? What is the role of those blank spaces? If possible, please refer to some documentation also.

Note: I am using TeXMaker on MikiTeX.

  • 1
    You're using the wrong approach anyway: what if \queue expands to X without periods? – egreg Nov 19 '13 at 18:13
  • in your example when you say it returns b.c as desired, in fact it returns b and .c is left in the stream, so b.c is printed, but #2 never was b.c and for that matter you could have defined \def\firstout #1.{} for the same effect. (notwithstanding egreg's relevant other remark). – user4686 Nov 19 '13 at 18:20
  • @egreg Yes, you are right. But I should confess that I would like to understand the blank space issue. – Franc Nov 20 '13 at 2:35
  • @jfbu Thank you, jfbu. Well, as you noted, I am new to TeX. I do not have sufficient background to understand the stream issue. I will try to learn about it. By the way, how can I force #2 to be b.c? – Franc Nov 20 '13 at 2:38
  • 1
    @Franc for #2 to be b.c you need something like \def\firstout #1.#2\queueend {#2} (recall the space of the control word is not significant) and either have \def\queue {a.b.c.\queueend} and \expandafter\firstout\queue or \def\queue {a.b.c.} and \expandafter\firstout\queue\queueend. And I have added a . after the last element of \queue (egreg's remark). The control word \queueend may be anything not encountered in the queue elements; for example you may use \firstout itself to avoid a new entry in the TeX hash table (or a ; perhaps). texdoc topic – user4686 Nov 20 '13 at 7:33
5

What are used in the macro definitions are tokens not characters in the input file. The space after a command name is taken as ending the command name token and is never itself tokenised. \firstout is one token. The csname token with name firstout. Conversely a space character after #2 is tokenised as a character token and so #2 becomes a delimited argument delimited by space, just as #1 is a delimited argument delimited by . .

  • Thank you David! I got it. Do you know what is the reference for token definition? – Franc Nov 19 '13 at 17:34
  • 1
    @Franc the TeXBook has the full spec for TeX's parser. – David Carlisle Nov 19 '13 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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