7

As we all know, when one is to send email with some Mathematica code in it, the practice is to do the following

Select the cell, Cell->ConvertTo -> INPUT FORM, then COPY AS TEXT

Then paste the code into the email, or any other exchange media it is.

The only problem with the above, is that when, on the other end, one copies the text from email and paste it back into a Mathematica notebook, the code will then no longer appear as it was in the original form (before the converting to INPUT FORM).

Because when one inserts the text copied from the email to Mathematica notebook, it becomes a standard form cell. All well, except the original code formatting is all lost.

Try it if you have not, convert a cell to INPUT FORM and the code will change shape, and I could not find a way to get back the original form before this operation.

Is there a way to email Mathematica code, in text of course, so that it keeps the same shape, and appear the same as it was originally when it was in standard form before the conversion?

Thanks --Nasser

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
Nasser
  • 12,849
  • 6
  • 52
  • 104
  • possible duplicate of [Known issues with copying code from Mathematica to other platforms?](http://stackoverflow.com/questions/5676993/known-issues-with-copying-code-from-mathematica-to-other-platforms) – abcd May 28 '11 at 06:33
  • @Nasser: Please see the question above. I'm voting to close as I think it is an exact duplicate of that question. – abcd May 28 '11 at 06:34
  • Thanks, I just looked at the question you put a link to. But I really did not understand what the final answer is. It said to use the function shown there, called translateQuotedPrintable[] and give it as argument in quotes, the whole code one wants to email? I tried, but the function gave many syntax errors. So, what is the answer to the problem I posted? Or is there no answer? i.e. this is a known issue, and that is it? If so, fine, this question can be closed. I do not know how this forum works really. – Nasser May 28 '11 at 07:27
  • 1
    @yoda @Nasser Yoda feels that your question is already covered by the earlier question. While I believe that may be technically true, my intention with that question was to cover _functional_ changes after copying, not changes in _layout_ (which is the focus of Nasser's question). So I'd suggest not to close this question. – Sjoerd C. de Vries May 28 '11 at 07:54
  • As to Nasser's comment: translateQuotedPrintable[] is meant to take a newsgroup posting as argument, not original Mathematica code. It removes the strange characters that sometimes appear after Mathematica code is posted on the newsgroup. Could you post errors in comments in that question? Did you use quotes? The function takes a string as argument – Sjoerd C. de Vries May 28 '11 at 07:55
  • @Sjoerd, Yes exactly, I am looking only at LAYOUT changes. When I write code in notebook, I add spaces, RETURN, and such to make the code look more pretty and easier to read. When I copy this code using COPY AS TEXT to email, the LAYOUT is all messed up, and I am asking how to copy it as text, while keeping the same layout, as one can do with say Java or C or Matlab, etc... source code. Thanks, Nasser – Nasser May 28 '11 at 07:57
  • @Sjoerd, I see now about translateQuotedPrintable[]. Sorry, I gave it as argument the code I wanted to email (inside ""), thinking that is how it is supposed to work. So, I got the error. Please ignore this issue then. --Nasser – Nasser May 28 '11 at 08:00
  • @Nasser even if you used it not as intended, I wonder how it could generate errors. The function just scans the string looking for quoted printable characters. If none are present nothing should happen. I'm still curious which particular string lead to errors in your case. If you still have it, could you please post it over there, so that I can update the function? – Sjoerd C. de Vries May 28 '11 at 08:11
  • I just copied the cell I had, which has a Manipulate function/code, and passed it to the function (added " before and added " after) and hit ENTER. I do not know how to post screen shot here, but I'll paste the call here, hopefully it will work Ok, I tried, it did not work, too long, wont let me. Here is a screen shot http://12000.org/tmp/may_28_2011/screen_shot.png may be I am not using it ok. – Nasser May 28 '11 at 08:24
  • @Nasser I think I understand. The function expects an ASCII string (7bits like used in newsgroups). Your string contains non-ASCII elements, and even colors. – Sjoerd C. de Vries May 28 '11 at 08:31
  • @Sjoerd: I don't think that is the reason. It's probably because the open quotes of the `translateQuotedPrintable[]` function get closed by the open quotes of a string already present in the argument. So anything that follows becomes garbage to the function. There ought to be a way to enclose quotes within quotes... perhaps open and close the function with `""` (two quotes instead of one). – abcd May 28 '11 at 16:55

3 Answers3

7

Cells formatted with the Code style (ALT/CMD-8) retain all formatting when using copy-and-paste. I use them all the time to format postings for SO. Well, truth be told, I use Code cells pretty much all the time, except for one-shot or throw-away input.

There are other advantages to using the Code style. It makes it easy to evaluate all of the definitions in a notebook, without also evaluating all of the incidental and test expressions: ALT/CMD-click on the cell bracket of any of the Code cells and then press SHIFT-ENTER. Also, Code cells automatically become initialization cells, which is frequently exactly what you want for the definitions in a notebook. Finally, when Mathematica spots Code cells in a notebook it offers to create an auto-save package that contains only those cells.

The downside to Code cells is that you lose the automatic formatting that Mathematica performs on cells with the Input style -- but then again that automatic formatting is precisely what we are trying to avoid in the context of this question.

WReach
  • 18,098
  • 3
  • 49
  • 93
  • I don't suppose you know a way to convert Input to Code that automatically tabulates the Code, do you? – Mr.Wizard May 28 '11 at 21:32
  • WoW !! I had no idea one can write in cell with code style like that. I just changed the cell using Format->Style->Code and did COPY AS TEXT, emailed it to myself, and it kept the same layout! This is amazing. Thank you WReach. I actually do not like the automatic formatting any way, I'd rather format the code myself, so this another advantage for me. I wonder though if I can use this style for cell when writing demonstrations as well. Have to check with someone at WRI on this. This was a very useful answer, than you again – Nasser May 28 '11 at 21:34
  • @Mr.Wizard Alas, no. It would be a nice starting point sometimes. – WReach May 28 '11 at 21:35
  • Nuts. Well, +1 anyway because the OP is happy. :-) – Mr.Wizard May 28 '11 at 21:38
  • @WReach: When you say _"you lose the automatic formatting that Mathematica performs on cells with the Input style"_, what exactly do you mean? Can you give me an example? I tried entering commands in a code cell and I see no difference from an Input cell... perhaps there are certain cases that I haven't tried yet. – abcd May 28 '11 at 23:23
  • 1
    @Yoda, in Cell mode, Mathematica does not automatic format as one is typing. i.e. for one example: when one hits RETURN at the end of an expression in the middle of it, i.e before the closing "]" is reached yet, you'll notice the cursor is positioned at the very start of the next line in CODE mode. In Input mode, the cursor will be positioned differently depending on the level of indentation it thinks is the correct one based on how deep on is inside an expression. I prefer the code mode now, since I like to layout the code myself, the way I wanted to look. This is just one example. – Nasser May 28 '11 at 23:39
  • @Nasser: Ah I see! I didn't try that out. Thanks! – abcd May 28 '11 at 23:47
2

Copying the cell as a cell expression doest the trick. Just right-click the cell bracket and the Copy as > Cell expression.

The resulting code is unusable for posting in newsgroups, as it is highly unreadable, but copying to and from email and back into Mathematica seems to preserve layout in the few tests that I did.

Sjoerd C. de Vries
  • 16,122
  • 3
  • 42
  • 94
  • Thanks, Yes this works for the purpose of copying to email as text, but my goal is to have it readable as well :) since I want to say email it to mathgroup or may be email code to someone to look on the screen visually, who might not have Mathematica to paste into in order to recover it. But thank you! – Nasser May 28 '11 at 08:17
  • @Nasser Perhaps newsgroup postings should have two sections, one human readable and one for copying back to mathematica, perhaps built from Cell expression and `Compress`. – Sjoerd C. de Vries May 28 '11 at 08:53
0

Since the problem is sending the code over email, why not just send (a part of) the notebook as an attachment. No copy-pasting mess, and guaranteed identical formatting.

rubenvb
  • 74,642
  • 33
  • 187
  • 332