On Sunday, June 29, 2014 7:24:35 AM UTC-4, Michael Hatherly wrote:
>
> This might not be the best way to do it, but the following has worked for
> me in the past.
>
> The output to the REPL is produced using display() (I think), so you can
> use the following to get the same kind of output as a string:
>
The display is just using writemime(io, "text/plain", x). So, you can
just use
stringmime("text/plain", x)
to get a string representation identical to the REPL output.
But it depends on what you want: there are a lot of print/string variants
that print things in slightly different ways. For example, you can use
repr(x) to get unabbreviated output -- the difference will arise for things
like large matrices: if you want the abbreviated output as in the REPL, use
stringmime, whereas if you want the full output you can use repr. For
scalars etc. the output will be identical. Or you can use print(x) or
string(x), which will differ e.g. for strings (when you print "foo", do you
want to print the quotation marks or not?).