Within a custom command, I'm doing the following to write to a file:
\newcommand\mycommand[1]{ \newwrite\file \immediate\openout\file=foobar.tex \immediate\write\file{ foo bar #1 blarg } \immediate\closeout\file } Of course when I invoke it as \mycommand{baz}, I get the following contents in foobar.tex:
foo bar baz blarg with all new lines turned into spaces by LaTeX. My question is, is there a way to output these lines into the file "as expected", i.e. with real new lines at the end of each line instead of a space? I'm thinking some \catcode wizardry should do the trick, but I'm not sure which character I need to redefine and to what category code.
Update
The solution in cmhughes' answer works perfectly. The only problem is when #1 contains embedded newlines. I can of course send it to mycommand with its own slew of ^^J, I was wondering if there is a way to avoid this.
^^J