# Check for sufficient number of tokens left

I want to use a command that takes a large number of arguments, possibly more than left in the input stream.

\def\consumeseven#1#2#3#4#5#6#7{}
\consumeseven
\space


In this scenario \consumeseven will see \space as its first argument and then complain that the "file ended while scanning use of \consumeseven".

Can something be done about this? Is there a way to check if there is a sufficient number of arguments left in the input stream?

## How did I come up with this? (Use Case)

I am experimenting with \obeylines to scan the source code of the main document for indentation. To do so, I would like to \def ^^M (the newline character) to examine the first few tokens of a line. But this leads to failure when the last active newline is processed as this line only has one token, \end. I know that I could scan the whole document into a single argument delimited by \end and append some bogus tokens there or scan the file line by line. But I pondered whether it would be possible to alternatively guard a macro against the end of the file generally, and thus asked this question.

• no \end is the first argument (white space is ignored here) – David Carlisle Nov 22 '15 at 16:37
• @DavidCarlisle Correct. It seems I had too much \obeylines lately... – XZS Nov 22 '15 at 16:56
• Is there a use case for this? – egreg Nov 22 '15 at 21:19
• @egreg There is now. – XZS Nov 22 '15 at 22:13

Just with three arguments, to show the idea.

Read the arguments one at a time.

\def\consumethree#1{%
\def\arga{?}\def\argb{?}\def\argc{?}%
\ifx\end#1%
\message{stopping}\consume\expandafter\end
\else
\def\arga{#1}
\expandafter\consumetwo
\fi
}

\def\consumetwo#1{%
\ifx\end#1%
\message{stopping}\consume\expandafter\end
\else
\def\argb{#1}
\expandafter\consumeone
\fi
}

\def\consumeone#1{%
\ifx\end#1%
\message{stopping}\consume\expandafter\end
\else
\def\argc{#1}
\expandafter\consume
\fi
}

\def\consume{[\arga][\argb][\argc]}

\consumethree a b c

\consumethree a
\end

• It wouldn't work if \bye is used instead of \end – egreg Nov 22 '15 at 16:56
• @egreg I know, but the idea of the site is that you answer the questions asked, not all possible questions. If you ask a question on detecting \bye I'll answer it for you:-) – David Carlisle Nov 22 '15 at 17:00
• I would actually like to request that, as there are many ways to signal the end of a TeX or LaTeX document. Apart from \bye and \end{document}, \end may be hidden within an arbitrarily named macro. The file may even end without any of these commands. So I am more interested in detecting the end of file instead of a token that signals it. – XZS Nov 22 '15 at 18:42
• @XZS do you really need to be doing this in the main document? Normally for any such token based parsing you can have it in an external file and use \read rather than \input and then testing for the end is built in there is an \ifeof primitive to test the end of file. – David Carlisle Nov 22 '15 at 19:49
• @DavidCarlisle having an \ifeof available indeed would help. I just added the use case that led to my question. This may explain why I am reluctant to defer to \read. – XZS Nov 22 '15 at 22:46