# How to copy in an auxiliary file the arguments of a macro?

I have tried to do so with the nice package newfile.

But, the following code does not compile :(

\documentclass{article}
\usepackage{newfile}

\newoutputstream{outstream}
\openoutputfile{out.aux}{outstream}

\newcommand{\test}[1]
{%
\begin{writeverbatim}{outstream}
#1
\end{writeverbatim}
#1
}

\begin{document}
\test{Does it work?}

\closeoutputstream{outstream}
\end{document}


PS : If possible, I would like a LaTeX-friendly answer :)

EDIT

I got nice answers, but they don't allow me to solve in a satisfying way my problem. So, let me expose you more precisely what I would like to do.

Imagine my latex file is :

 \documentclass{article}
\usepackage{my_macros}

\begin{document}
\Exercise{Bla Bla Bla}

\Solution{Bli Blu Bla}
\end{document}


I would like that the compilation of the latex file produce, as usual a .pdf file, but also two auxiliary files Exo.aux and Solu.aux that contain respectively Bla Bla Bla and Bli Blu Bla !

Thanks !

PS : If possible, I would like a LaTeX-friendly answer :) I would love to understand TeX, but although I got some answers to Online references for creating advanced macros?, I still find TeX very esoteric. I am looking for a user-friendly manual !

No, you can't use "verbatim" environment as arguments to commands.

All you can do is to define an environment with an abbreviated name:

\newenvironment{test}
{\writeverbatim{outstream}}
{\endwriteverbatim}


And then

\begin{test}
Does it work?
\end{test}

\begin{test}
Yes
\end{test}


will write the two lines in the file.

If you can be content with a "pseudoverbatim" writing, then

\newcommand{\testA}{\addtostream{outstream}{\unexpanded{#1}}}


can be what you are looking for.

With the environment form, something like

\begin{test}
\section{Test}
Text
\end{test}


will write

\section{Test}
Text


in the output file, while

\testA{\section{Test}
Text}


will write

\section {Test} Text


The command form will add a space after each control sequence (not control symbol) and won't respect line breaks. Of course the argument of the command form should be balanced with respect to braces. Also, # characters would be doubled (this can be cured).

It all depends on what you want to use the output file for.

• Thank you for your answer ! What would be the difference between your two propositions ? PS: the command \writeverbatim does not appear in the documentation of newfile --- but apparently, this is just a reflexion of my ignorance – Colas Oct 12 '11 at 21:12
• The second way will add a space after every control sequence name. It depends on what you are going to do with the out.aux file. The \writeverbatim-\endwriteverbatim trick is explained in the doc of verbatim, on which newfile is based. – egreg Oct 12 '11 at 21:16
• Could you give an short example ? – Colas Oct 12 '11 at 22:23
• Dear egreg, unfortunately, your ideas don't work for what I need... What I am looking for, in a way, is a set of macros that removes the preambule of the file and then split it in several files (basically, one file for the command \Exercise and one for the command \Solution), so that I can then input them automatically in my book.tex. Thanks ! – Colas Oct 13 '11 at 21:15
• @Colas it might be worth putting your last comment as an edit to your question- people may offer different approaches that circumvent the problem. – cmhughes Oct 18 '11 at 16:52

What I am looking for, in a way, is a set of macros that removes the preamble of the file and then split it in several files (basically, one file for the command \Exercise and one for the command \Solution), so that I can then input them automatically in my book.tex

you really just want a way to extract the contents of one of the macros while ignoring the contents of the other. For this, I would suggest you use the standalone package to include the original file directly within your main file. But, redefine the macros you want to ignore. So, for example, when you want only the contents of the \Exercise environment, you redefine the macro \Solution temporarily:

\newcommand*{\inputExercise}[1]{%
\let\OldSolution\Solution% Save original definition
\renewcommand{\Solution}[1]{}% Set \Solution to ignore parameter
\par\input{#1}%
\let\Solution\OldSolution% Restore original definition
}%


Then if your latex file contained:

 \documentclass{article}
\usepackage{my_macros}

\begin{document}
\Exercise{Bla Bla Bla}
\Solution{Bli Blu Bla}

\Exercise{Bla Bla Bla}
\Solution{Bli Blu Bla}
\end{document}


You could extract the specific contents and get a list of \Exercises and/or \Solutions. For illustrative (and testing) purposes I extracted both separately to generate:

\documentclass{article}
\usepackage{xcolor}
\usepackage{filecontents}
\usepackage{standalone}

%\usepackage{my_macros}% Since I do not have my_macros.sty
\newcommand*{\Exercise}[1]{\textcolor{blue}{#1}}%
\newcommand*{\Solution}[1]{\textcolor{red}{#1}}%

% Following is so that I can package this test into one file
\begin{filecontents}{LatexSubfile.tex}
\documentclass{article}
\usepackage{my_macros}

\begin{document}
\Exercise{Bla Bla Bla}
\Solution{Bli Blu Bla}

\Exercise{Bla Bla Bla}
\Solution{Bli Blu Bla}
\end{document}
\end{filecontents}

% When want exercise only, we ignore \Solution
\newcommand*{\inputExercise}[1]{%
\let\OldSolution\Solution% Save original definition
\renewcommand{\Solution}[1]{}% Set \Solution to ignore parameter
\par\input{#1}%
\let\Solution\OldSolution% Restore original definition
}%

% When want solution only, we ignore \Exercise
\newcommand*{\inputSolution}[1]{%
\let\OldExercise\Exercise% Save original definition
\renewcommand{\Exercise}[1]{}% Set \Exercise to ignore parameter
\par\input{#1}%
\let\Exercise\OldExercise% Restore original definition
}%

\begin{document}
Exercises:
\inputExercise{LatexSubfile.tex}

\bigskip
Solutions:
\inputSolution{LatexSubfile.tex}
\end{document}

• Very interesting solution :) – Colas Oct 19 '11 at 8:31