# Using filecontents inside of xparse

I'm trying to reproduce this using xparse:

\begin{filecontents}{sections/autogen/somefile.tex} % Line to auto-produce
\begin{myotherenvironment}
Do lots of stuff
\end{myotherenvironment}
\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}%
%\begin{filecontents}{sections/traces/autogen/#1.tex}
#2
%\end{filecontents}}%
\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:

  \captionsetup[table]{name=Listing}
\table[H] \footnotesize
\setlength{\tabcolsep}{2pt}
\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}}
\endtabu
\endtable
\captionsetup[table]{name=Table}

• 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 '13 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 '13 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 '13 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 '13 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 '13 at 9:02

## 1 Answer

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

You can exploit VerbatimOut from fancyvrb.

\documentclass{article}
\usepackage{fancyvrb,xparse}
\usepackage{caption,tabu,listings}
\NewDocumentEnvironment{doubletable}{m}
{\captionsetup[table]{name=Listing}%
\table[htp]
\VerbatimOut{sections/autogen/#1}}
{\endVerbatimOut
\input{sections/autogen/#1}
\label{repeattable@#1}
\endtable}

\NewDocumentCommand{\repeattable}{m}{%
\begingroup\captionsetup[table]{name=Listing,list=no}
\begin{table}[!htp]
\renewcommand{\thetable}{\ref{repeattable@#1}}
\input{sections/autogen/#1}
\end{table}
\endgroup
}

\begin{document}

\begin{doubletable}{1}
\footnotesize
\setlength{\tabcolsep}{2pt}
\begin{tabu} to \textwidth {|r@{ }l|l|X|} %{|@{ }r@{:}l|l|X[l]|}
\rowfont{\bfseries} & LPS & MTH & SC \\
x & y & z & \lstinline{foo}
\end{tabu}
\caption{X}
\end{doubletable}

Repeat it

\repeattable{1}

\end{document}


• 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 '13 at 23:02