In the following code

\foreach\a in {x, y}{
  some code                                                                                                                                   

the environment verbatim fails inside the scope of \foreach:

! File ended while scanning use of \@xverbatim.

Also the environment lstlisting fails:

Package Listings Warning: Text dropped after begin of listing on input line 9.

How to insert code listings inside the scope of \foreach?

  • 3
    verbatim can not be an argument to any macro, including \foreach. I have an additional question. Your simplistic MWE doesn't explain how one set of verbatim code would be different than the next in the loop. What did you have in mind there? – Steven B. Segletes Aug 14 '18 at 18:27
  • @StevenB.Segletes Inside the \foreach loop imagine some other things that depend on \a, deleted for simplicity. But initially I tried to use lstlisting depending on \a which seems tricky to accomplish. – Viesturs Aug 14 '18 at 18:37
  • 2
    The thing you want to achieve isn't possible in that way, as the argument of \foreach is already tokenized. lstlisting and other verbatim environments and macro depend on category code changes, which won't have any effect on already tokenized content. What you can do, is setup a box or something like that which contains your verbatim content and use that in your loop. – Skillmon Aug 14 '18 at 18:57
  • 1
    If you should insist on the verbatim approach then you will probably have to resort to reading an external file, as its contents will not be tokenized by the loop. But then it would be quite problematic to modify its contents depending on \a. – Phelype Oleinik Aug 14 '18 at 18:58
  • 1
    verbatimbox package allows you to stuff verbatim content into a box and recall it later, though I don't know how you would make it dependent on \a. – Steven B. Segletes Aug 14 '18 at 19:37

I propose a solution by writing the verbatim material to an external file (with the tools of the package verbatim) and then inserting verbatim the file with VerbatimInput of fancyvrb. This last instruction can be put in a loop (here it's a loop of expl3).


\iow_new:N \myverbatim_stream
\cs_generate_variant:Nn \iow_now:Nn {NV}
\NewDocumentEnvironment {LoopVerbatim} {mmmm}
  {\iow_open:Nn \myverbatim_stream {myverbatim.tex}
   \tl_map_inline:nn {\ \\\{\}\$\&\#\^\_\%\~}
      {\char_set_catcode_other:N ##1}
   \char_set_catcode_active:N \^^M
   \cs_set:Npn \verbatim@processline
        {\iow_now:NV \myverbatim_stream \verbatim@line}
  {\iow_close:N \myverbatim_stream
   \int_step_variable:nnnNn {#1} {#2} {#3} {#4} 


def f(x):
    return x ** (\i)


Of course, you need escape characters in the verbatim. Here, \, { and } keep their TeX standard meaning (for Python, it's pretty good).

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.