Writing text files using Mathematica

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

  1. AlphaNumeric Fully ionized Registered Senior Member

    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 :
    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 :
    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.
  2. Google AdSense Guest Advertisement

    to hide all adverts.
  3. PieAreSquared Woo is resistant to reason Registered Senior Member

    & Chr$(34)

    maybe ? works in VB
  4. Google AdSense Guest Advertisement

    to hide all adverts.
  5. Absane Rocket Surgeon Valued Senior Member

    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.
  6. Google AdSense Guest Advertisement

    to hide all adverts.
  7. BenTheMan Dr. of Physics, Prof. of Love Valued Senior Member

    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. temur man of no words Registered Senior Member

    Generally it should be possible to implement N-nested loops by using just one loop and an array.
  9. funkstar ratsknuf Valued Senior Member

    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. rpenner Fully Wired Registered Senior Member

    "\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...

    Please Register or Log in to view the hidden image!

  11. AlphaNumeric Fully ionized Registered Senior Member

    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
    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. draqon Banned Banned

    isnt that the same thing with Java too? "\""

Share This Page