I'm trying to reproduce this using xparse:

\begin{filecontents}{sections/autogen/somefile.tex} % Line to auto-produce
    Do lots of stuff
\end{filecontents}                                  % Line to auto-produce
\input{sections/autogen/somefile.tex}               % Line to auto-produce

The above works fine. I've tried this using both NewDocumentCommand and NewDocumentEnvironment, and using both begin/end and csname/endcsname. I'm showing attempting to include the #2 manually, previously I was just attempting to use NewDocumentEnvironment with one required parameter

\NewDocumentCommand \writefile { m m } {%
  \typeout{Writing file sections/autogen/#1.tex}%
  \csname filecontents*\endcsname{sections/traces/autogen/#1.tex}%
  \csname endfilecontents*\endcsname%

It seems that the file is being generated, but when using NewDocumentEnvironment \end{writefile} is being inserted into the file generated by filecontents. When using NewDocumentCommand there seem to be a multitude of problems.

An example output file would contain this:

  \table[H] \footnotesize
    \tabu to \textwidth {|r@{ }l|l|X|} %{|@{ }r@{:}l|l|X[l]|}
    \rowfont{\bfseries} & LPS & MTH & SC \\
    \lcell 12 & \lpscell #3 & \mthcell #4 & \sccell {\lstinline[style=jt]{foo}}
    \lcell 12 & \lpscell #3 & \mthcell #4 & \sccell {\lstinline[style=jt]{foo}}
    \lcell 12 & \lpscell #3 & \mthcell #4 & \sccell {\lstinline[style=jt]{foo}}
  • 2
    You can't use filecontents or filecontents* in the argument to another command because they are quite similar to verbatim. Can you add a short example of a typical file you'd like to write out?
    – egreg
    Oct 16, 2013 at 17:30
  • Sure, I've added an example, perhaps there's a fix somewhere. This is reduced from my real code - In general I need to maintain the tabular format with my custom formatting for each cell, and the table comes from a NewDocumentEnvironment while each line comes from a NewDocumentCommand.
    – Hamy
    Oct 19, 2013 at 3:18
  • I can't see where a command form is better than the environment form. If it's just for adding a default path in front of the file name, then it shouldn't be difficult to adapt filecontents; with fancyrb there is already a generic facility.
    – egreg
    Oct 19, 2013 at 6:55
  • Yea, the only point here is to allow me to use filecontents so I can doubly-include some of my complex tables, but to place all of the auto-generated files in a nice folder(and perhaps steal the name from some parameters that I'm already passing such as label). I don't care if the new item is an environment/command, but I can't get either to work. I'll take a look at fancyrb - I'm not understanding now how it can help me but I know nothing about it so that's expected ;-)
    – Hamy
    Oct 20, 2013 at 7:37
  • Do you really need to write out the tables or are you just wanting to typeset them again in the same document?
    – egreg
    Oct 20, 2013 at 9:02

1 Answer 1


Any approach with macros will fail because you want to preserve the contents and also use \lstinline.

You can exploit VerbatimOut from fancyvrb.




\begin{tabu} to \textwidth {|r@{ }l|l|X|} %{|@{ }r@{:}l|l|X[l]|}
  \rowfont{\bfseries} & LPS & MTH & SC \\
  x & y & z & \lstinline{foo}

Repeat it



enter image description here

  • oh...wow..this works! I want to create a second account just to vote you twice! Seriously thank you for sticking with me through this!!
    – Hamy
    Oct 21, 2013 at 23:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .