Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Given a pretty long expression, such as

a={-11 ψ^2 λ[1]+6 ψ λ[2],35 ψ^2 λ[2],11 ψ^2 λ[1]^2-11 ψ^2 λ[2]-6 ψ λ[1] λ[2],ψ^2 λ[1]^2-ψ^2 λ[2]-ψ λ[1] λ[2]+λ[2]^2,-λ[1]^3+2 λ[1] λ[2],-3 ψ λ[1]^3+6 ψ λ[1] λ[2]+λ[1]^2 λ[2]-λ[2]^2,-ψ λ[1]^3+λ[1]^4+2 ψ λ[1] λ[2]-3 λ[1]^2 λ[2]+λ[2]^2}

a >> tmp would output the expression of a to an external file tmp. But I don't like the format in this output. It breaks lines, and contains extra spaces between operators of "+-*/".

In fact, in Mathematica, selecting an expression, we could do (right click) > Copy as > Plain Text, and then we could paste it to any external text editor without any problem.

Is it possible to specify the output stream using this "Plain Text" format above?

share|improve this question
add comment

4 Answers 4

up vote 5 down vote accepted
Export["test.txt", {a}] 

works for me

"Plaintext" is already the default output form of Export. The { } around a prevents Export from seeing it as a series of arguments (a is a list in your example) that each have to be put on its own line.

share|improve this answer
add comment

You can stay with Put using the method I showed here for PutAppend:

SetOptions[OpenWrite, PageWidth -> Infinity];
a >> tmp

This method is especially useful in the case of PutAppend because it allows you to maintain a running log file with results of intermediate computations with one expression per line.

share|improve this answer
add comment

Try using Export and Import.

Make sure to include "Data" as the second argument of Import.

SetDirectory[$TemporaryDirectory];
Export["myfile.txt", a]
in = Import["myfile.txt", "Data"]

data


If you really want to see the Greek letters spelled out you can then use

FullForm[in]

but this will also return List[] instead of {}.

share|improve this answer
add comment

Perhaps you could use Export instead of Put?

out = StringReplace[ToString[InputForm[a]], {" " | "\n" -> ""}]
Export["out.txt", out]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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