This is an attempt to make clear what happened in this question of xport (now deleted, sorry). The now deleted question included code that tried to make my answer to this question of xport work for optional arguments, but the code failed. A shorter illustration: The following code doesn't compile if one removes the [optional argument].
\documentclass{minimal}
\usepackage{listings,fancyvrb}
\newenvironment{Row}[1][]
{\VerbatimEnvironment
\begin{VerbatimOut}{\jobname.tmp}}
{\end{VerbatimOut}\lstinputlisting{\jobname.tmp}}
\begin{document}
\begin{Row}[optional argument]
\relax
\end{Row}
\end{document}
The error message says
! FancyVerb Error:
Extraneous input `\relax ' between \begin{Row}[<key=value>] and line end
Of course, the optional argument (and in fact the whole construction) is utterly pointless here, but hey, it's a minimal example. To make the example in xport's question also work for optional arguments is quite interesting, I think.
The question: What exactly is happening here, and how can one fix the code?
(What I found out so far: \begin{VerbatimOut}{\jobname.tmp}} wants to see a linebreak immediately after it (try putting a %), and this somehow gets messed up by \begin{Row} looking for the optional argument.)
\@xifnchfrom\@ifnch, but that didn't help. – Hendrik Vogt Jan 16 '11 at 10:18^^Mgets tokenized as a space character. I imagine that if you removed the\@xifnch, then you get into an infinite loop because the test of the let token (\@let@tokenor something) against\@sptoken(or whatever) keeps being true over and over. In any event, once the^^Mhas been tokenized, it's too late to change its catcode so the\begin{VerbatimOut}is going to find a token that isn't an active^^M. A better solution, perhaps, would be to use\@ifnextchar[\Row@{\Row@[]\activeeol}where\activeeolis\letto an active^^M. – TH. Jan 16 '11 at 10:30\FV@BeginScanning#1^^Mso it's looking for a literal character. I'll update my answer to the better one. – TH. Jan 16 '11 at 10:43\ifx\@let@token\@sptokentest from\@ifnch; this in particular removes the\@xifnch. This way I don't get an infinite loop, but still it doesn't work. That I still can't understand. – Hendrik Vogt Jan 16 '11 at 10:51\futurelethas tokenized the^^Mso it's a space_10 character token. Thus the argument to\FV@BeginScanningis the space_10 character and the\relaxtoken.\FV@BeginScanningchecks if#1is empty and since it isn't, you get the error. – TH. Jan 16 '11 at 11:01\futurelet, isn't it? I mean,\futureletisn't supposed to mess up anything following it; that's what we love about\futurelet. Is this behaviour of\futureletdocumented anywhere? – Hendrik Vogt Jan 16 '11 at 12:17\activeeolis defined, the%s are somehow not needed. 2. I'd find it easier if the last argument of\@ifnextcharwas\expandafter\Row@noarg\activeeol, with\def\Row@noarg{\Row@[]}. By the way, I didn't find this\futureletbehaviour in the TeXbook, but in TeXbyTopic it says "If a character token has been\futureletto a control sequence, its category code is fixed. The subsequent<token1>cannot change it anymore." Another lesson learned. – Hendrik Vogt Jan 16 '11 at 12:52\newenvironment{Row}{\catcode\^^M\active @ifnextchar[\Row@{\Row@[]}} {\end{VerbatimOut}\aftergroup\gtemp} \def\Row@[#1]{\gdef\gtemp{&\tabularnewline\hline}\VerbatimEnvironment\begin{VerbatimOut}{\jobname.tmp}}` – Hendrik Vogt Jan 16 '11 at 15:08^^Mis\parwhich is (almost completely) ignored in vertical mode. Re\futurelet, the text you quoted is familiar. I guess I read it in TeX by Topic. I thought it was in The TeXbook, but I cannot find it. – TH. Jan 16 '11 at 16:24minimalwitharticleand\relaxwith\large ain your new code, then you get! TeX capacity exceeded. (By the way, do you ever sleep?) – Hendrik Vogt Jan 16 '11 at 16:34\largewas getting tokenized. The fix is simple. But I'm starting to think that supporting optional arguments on the next line was a mistake. At any rate, I'll edit the code. As for sleep, I'm going to do so right now! – TH. Jan 16 '11 at 17:30\string- tricky. And I should have seen it myself, having written "another lesson learned". Once again this\futureletbehaviour that I've only learned today. Thanks a lot! – Hendrik Vogt Jan 16 '11 at 17:51