# Writing text files using Mathematica

Discussion in 'Physics & Math' started by AlphaNumeric, Feb 4, 2009.

1. ### AlphaNumericFully ionizedModerator

Messages:
6,697
I want to be able to use Mathematica to write a text file with quotation marks in it. Getting Mathematica to write a text file is no problem, I use the following bits of code :
Code:
OpenTextFile := Module[{},
InputStream["MathematicaTxt", MathematicaTxt, FormatType ->OutputForm,ShowStringCharacters -> True];
OpenWrite["MathematicaTxt"]; ];

TextFile[Text_] := Module[{},   WriteString["MathematicaTxt", Text];   ];

CloseTextFile := Module[{},   Close["MathematicaTxt"]; ];
They open/make, write to and close a file called MathematicaTxt and I use it to do various repetitive procedures which the syntax of Mathematica doesn't like, such as having a number, N, of nested Do or For loops, where N is dependent upon the problem I'm doing. I use Mathematica and one Do loop to write a Do loop into a textfile N times, Mathematica reads it in and does N nested Do loops. All well and good.

However, Mathematica's syntax is such that the symbol " is not viewed as text but is much the same role as { is in LaTeX. In LateX you just say \{ to produce the symbol { but I don't know how to do the analogous problem with Mathematica and ".

How would I write the following, " and all into a textfile using Mathematica :
Code:
Print[5,"     ",3]

That would give a spacing between the 5 and the 3 because Mathematica views the " "[/red] but as 'Here's some text'. But if I use the above code and input TextFile[" "] is doesn't put in either ".

If that made sense and you're a Mathematica savvy person (Rpenner, you've always been great at knowing the syntax of Mathematica?) I'd much appreciate the help. It's just a matter of asthetics in terms of the work but I like having a nice layout to things.

3. ### PieAreSquaredWoo is resistant to reasonRegistered Senior Member

Messages:
2,144
& Chr\$(34)

maybe ? works in VB

5. ### AbsaneRocket SurgeonValued Senior Member

Messages:
8,989
Oh man... Mathematica. Years ago I tried to learn Mathematica but I discovered that my only use for it was to generate really cool graphs and to work complex equations.

I did all my math writing with LaTeX.

Sorry that I am of little help.

7. ### BenTheManDr. of Physics, Prof. of LoveValued Senior Member

Messages:
8,967
Hmmm... I use this trick.

Make Mathematica write XXX or something. Then after you write the file, go in and do a blanket replace (Usually ctrl + R) and replace all of the XXX's with ".

Also, if you're trying to write a .tex file, Mathematica 6 has this great function called "Copy as LaTeX". So make it spit out your output, then right click on the cell, and you should see an option to copy the cell contents as TeX.

8. ### temurman of no wordsRegistered Senior Member

Messages:
1,330
Generally it should be possible to implement N-nested loops by using just one loop and an array.

9. ### funkstarratsknufValued Senior Member

Messages:
1,390
The technical term for what you're looking for is "escape code". I.e. in LaTeX, the escape code for { is \{. A bit of searching reveals that the escape code for " is \", cf. here, under "Possible issues".

Another possibility is to use Write instead of WriteString, cf. this, since you're writing Mathematica expressions.

Disclaimer: I don't know Mathematica.

10. ### rpennerFully WiredStaff Member

Messages:
4,833
"\n" and "\"" should be strings with a newline and a quote.

TextFile["Print[5,\" \",3]\n"] should work according to my 1991 reference.

(I am resisting the urge to re-write your declarations entirely...

)

11. ### AlphaNumericFully ionizedModerator

Messages:
6,697
Thanks guys, the "\"" sorted it. I thought I'd tried that, incase the syntax is the same as LaTeX, but I think I tried just \" , rather than "\"".
I hadn't thought about doing that, that'd be useful for real industrial sized textfiles, but mostly I use it for things such as doing
Code:
Print[MatrixForm[Matrix1],"        ",MatrixForm[Matrix2],   (......) , MatrixForm[MatrixN]]

It's not possible (at least not obviously possible) to get Mathematica to output N, where N changes from case to case, MatrixForm[ ] within a single Print[ ] section, so I have Mathematica using a Do loop to write a text file with N matrixform's in it and then reading that textfile and thus displaying the matrices.

It's mostly bells and whistles, not something which is obscuring me from getting results. But it seems that the two of us who use Mathematica heavily spend say a week trying to solve an algebraic problem and then 2 weeks messing with various options on how to display it!

12. ### draqonBannedBanned

Messages:
35,006
isnt that the same thing with Java too? "\""