1
$\begingroup$

I've got some code which generates a series with coefficients that are a rather complicated expressions of Greek letters with subscripts. I want to store some of these coefficients for future use, rather than computing them each time I run my program.

What I would like, is to store each coefficient as if I had used the "Copy As" -> "Input Text" functionality which you get when right clicking on a specific cell. And I would like to do this via the Export[] function rather than doing it for each coefficient manually. Does anyone know how to do this so that, in particular, the Greek letters are exported as $\backslash[\text{Rho}]$ as opposed to $\rho$. "Copy As" -> "Input Text" does this, but Export[] gives the formatted $\rho$, which causes problems on import.

$\endgroup$
  • $\begingroup$ Have a look at CopyToClipboard I think it does what you want. You should note that there are functions for saving expressions for furture usesuch as Put and Save. $\endgroup$ – Quantum_Oli Nov 23 '16 at 9:38
  • $\begingroup$ Excellent, thank you! PutAppend and then ReadList gives me exactly what I want. $\endgroup$ – Billy Pilgrim Nov 23 '16 at 10:12
  • $\begingroup$ @BillyPilgrim If you use Put and Read together, then you shouldn't write to a file. Just use ToString. Your question seems to ask about exporting to a file, maybe copying to the clipboard, but your comment made me suspect that you just want a string. ... $\endgroup$ – Szabolcs Nov 23 '16 at 10:22
  • $\begingroup$ @Szabolcs What I meant by my comment is that I use Put/PutAppend to write to file, and then some time later, when I want the contents of the the file, I use ReadList. This provides (as far as I can tell) an acceptable solution for me. $\endgroup$ – Billy Pilgrim Nov 23 '16 at 11:15
  • $\begingroup$ @BillyPilgrim So your real question is how to save Mathematica expressions for later, and not how to format them in a certain way? $\endgroup$ – Szabolcs Nov 23 '16 at 11:15
1
$\begingroup$

Use Export with "Package". Export["file.txt",expr,"Package", "Comments" -> None]

Alternatively use

ToString[expr, InputForm]

then write the resulting string to a file. References: ToString, InputForm

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.