1

I tried to copy matrix from Mathematica to website, only end up with lots of garabge like this:

\!\(
TagBox[GridBox[{
{"16", "2", "3", "13"},
{"5", "11", "10", "8"},
{"9", "7", "6", "12"},
{"4", "14", "15", "1"}
},
AutoDelete->False,
GridBoxItemSize->{
     "Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, 
      "Rows" -> {{Automatic}}, "RowsIndexed" -> {}, "Items" -> {}, 
      "ItemsIndexed" -> {}}],
"Grid"]\)

Then I have to edit it manually. Sometimes I send math equations and matrices in Email. Is there a better way to paste matrix?

Nick
  • 459
  • 4
  • 9
  • 2
    Hi Nick, I voted to close the question here, because I think it's about how to use the site thus belong to the meta site. – Silvia Feb 06 '15 at 11:35
  • I presume that this is output from Mathematica. If so, transform it to InputForm and copy that. – bbgodfrey Feb 06 '15 at 11:39
  • @Silvia Of course you have a point there. Since people use Mathematica frequently here, they deal with Matrix and post questions and answers much more often than the others. I guess somebody may have already had a solution here. Would you please keep it for a while? – Nick Feb 06 '15 at 11:41
  • @Nick I understand your point, and I myself would like to see the answers for it, too. But as the current phrase of your question (i.e. copy to this site), IMHO, it would fit the meta site better. And it may even get more attention there so you can receive nice answers like this post. But again, if the main site is preferred, maybe the first sentence could be rephrased -- maybe something like "copy matrix from Mathematica as an InputForm"? – Silvia Feb 06 '15 at 11:50
  • @Silvia You're right, and thank you for pointing me the example on the meta site. I edited the question - made it more general for paste matrix to website and email contents. I think maybe other people may face the same problem as well. – Nick Feb 06 '15 at 12:23
  • @bbgodfrey I tried InputForm after reading your comment. It is much easier to edit. Thank you. – Nick Feb 06 '15 at 12:24
  • What is your purpose of the copy/paste? Do you want to preserve the computability or the traditional math formula style appearance or both? – Silvia Feb 06 '15 at 12:35
  • @Silvia As I see it the complication lies with Mathematica and not the Stack Exchange software, therefore I feel it is better here than on Meta. I will not contravene a community migration however. – Mr.Wizard Feb 06 '15 at 12:38
  • @Mr.Wizard I already retracted my vote after OP rephrased the question :) – Silvia Feb 06 '15 at 12:40

1 Answers1

2

Suppose we have a grid like this:

x = NestList[RotateRight, {1.5, 2/3, Pi, 9/7`, "!"}, 5];

Grid[x]

It looks like this in the Front End:

enter image description here

The image above was produced by selecting the Cell bracket and choosing File > Save Selection As....

If we use the Context Menu item Copy As > Plain Text we get:

1.5 2/3 \[Pi]   1.28571 !
!   1.5 2/3 \[Pi]   1.28571
1.28571 !   1.5 2/3 \[Pi]
\[Pi]   1.28571 !   1.5 2/3
2/3 \[Pi]   1.28571 !   1.5
1.5 2/3 \[Pi]   1.28571 !

Using halirutan's toolbar the \[Pi] are easily converted to π:

1.5 2/3 π   1.28571 !
!   1.5 2/3 π   1.28571
1.28571 !   1.5 2/3 π
π   1.28571 !   1.5 2/3
2/3 π   1.28571 !   1.5
1.5 2/3 π   1.28571 !

Copy As > Input Text results different things depending on what is selected. If only the content of the grid is selected (e.g. quadruple-click) we get:

{
 {1.5, 2/3, \[Pi], 1.28571, "!"},
 {"!", 1.5, 2/3, \[Pi], 1.28571},
 {1.28571, "!", 1.5, 2/3, \[Pi]},
 {\[Pi], 1.28571, "!", 1.5, 2/3},
 {2/3, \[Pi], 1.28571, "!", 1.5},
 {1.5, 2/3, \[Pi], 1.28571, "!"}
}

This form is useful because it can be pasted back into Mathematica.

However if the entire grid is selected we get something a lot messier:

\!\(
TagBox[GridBox[{
{"1.5`", 
FractionBox["2", "3"], "\[Pi]", "1.2857142857142856`", "\<\"!\"\>"},
{"\<\"!\"\>", "1.5`", 
FractionBox["2", "3"], "\[Pi]", "1.2857142857142856`"},
{"1.2857142857142856`", "\<\"!\"\>", "1.5`", 
FractionBox["2", "3"], "\[Pi]"},
{"\[Pi]", "1.2857142857142856`", "\<\"!\"\>", "1.5`", 
FractionBox["2", "3"]},
{
FractionBox["2", "3"], "\[Pi]", "1.2857142857142856`", "\<\"!\"\>", "1.5`"},
{"1.5`", 
FractionBox["2", "3"], "\[Pi]", "1.2857142857142856`", "\<\"!\"\>"}
},
AutoDelete->False,
GridBoxItemSize->{
     "Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{Automatic}}, 
      "RowsIndexed" -> {}, "Items" -> {}, "ItemsIndexed" -> {}}],
"Grid"]\)

Another option is Copy As > LaTeX and then pasting the expression between $ symbols, to yield:

$\begin{array}{ccccc} 1.5 & \frac{2}{3} & \pi & 1.28571 & ! \\ ! & 1.5 & \frac{2}{3} & \pi & 1.28571 \\ 1.28571 & ! & 1.5 & \frac{2}{3} & \pi \\ \pi & 1.28571 & ! & 1.5 & \frac{2}{3} \\ \frac{2}{3} & \pi & 1.28571 & ! & 1.5 \\ 1.5 & \frac{2}{3} & \pi & 1.28571 & ! \\ \end{array}$

This is visually pleasing but slow to render, and it cannot be easily pasted back into Mathematica.

Finally we can use Export or ExportString to export in any supported format:

ExportString[x, "TSV"]

And Copy As > Plain Text:

1.5 2/3 Pi  1.2857142857142856  !
!   1.5 2/3 Pi  1.2857142857142856
1.2857142857142856  !   1.5 2/3 Pi
Pi  1.2857142857142856  !   1.5 2/3
2/3 Pi  1.2857142857142856  !   1.5
1.5 2/3 Pi  1.2857142857142856  !

Note that the Real numbers are printed here with full precision rather than being truncated.

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Thank you very much for the detailed explanatin. I'll use Copy As > Input Text for SE, since it can paste back to Mathematica. I'm wondering if you also have any recommendation on matrix/equation in email content? – Nick Feb 06 '15 at 13:58
  • @Nick I am glad that what I wrote you found useful. How would the email situation be different? Is the audience different, are the formatting tools different, or something else? – Mr.Wizard Feb 06 '15 at 14:47
  • Well, in Email, I'd just need the best looking -- traditional style just like in math textbooks. There're already some Gmail+TeX/MathML solution; however, I use hotmail. Currently, I'm using a thirdparty online latex editor.http://www.codecogs.com/latex/eqneditor.php I'm wondering maybe there're better solution? – Nick Feb 06 '15 at 15:26