Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I spent some time manually editing a post replacing Mathematica ASCII \[Alpha] with Unicode α. I did this by laboriously choosing Copy as LaTeX, pasting into the edit box, and then copying the Unicode symbol from the preview below. This made me realize I am lacking a "Copy as Unicode string" function in Mathematica.

How can I most easily copy an expression such as:

Mathematica graphics

In Unicode:

αβ + Mod[δΨ, 2 ⁢ρ^2]
share|improve this question

2 Answers

Since a native method is not forthcoming, I shall post my file based circumvention, for Windows.

You will need to have this utility in the command path (it apparently is stock with Windows 7).

copyUnicode[expr_] := Run["clip <",
   Export["$Clipboard.temp", ToString[expr, InputForm],
          "Text", CharacterEncoding -> "Unicode"] ];

Usage:

expr = \[Alpha]\[Beta] + Mod[\[Delta]\[CapitalPsi], 2\[InvisibleTimes]\[Rho]^2];

copyUnicode[expr]

This leaves the following text in the Windows Clipboard:

αβ + Mod[δΨ, 2*ρ^2]
share|improve this answer
2  
I think clip is native in at least win-7. Perhaps you should note that this places the output in the system clipboard. The output of your copyUnicode function is just a 0 (at least, on my PC). You have to do a ctrl-v paste afterwards. A MMA Paste doesn't seem to work. – Sjoerd C. de Vries Feb 2 '12 at 21:10
Very useful function! Why not make it available for any code sample? (Now it works only for single expression…) – xzczd Nov 23 '12 at 14:09
@xzczd I'll see if I can improve it. – Mr.Wizard Nov 23 '12 at 23:56

To circumvent Mathematica's internal representation, I decided to use the operating system. Of course, this means it's only going to work on Mac OS X because it uses Cocoa bindings in the built-in Python interpreter:

copyAsUnicode[t_] := Module[{
   out = FileNameJoin[{$TemporaryDirectory, 
      "MathematicaOutput" <> StringJoin[Map[ToString, DateList[]]] <> 
       ".rtf"}]
   },
  Export[out, t];
  Run["printf \"from AppKit import *\n\
board=NSPasteboard.generalPasteboard()\n\
content=NSData.dataWithContentsOfFile_('" <> out <> 
    "')\nboard.declareTypes_owner_([NSRTFPboardType], None)\n\
board.setData_forType_(content, NSRTFPboardType)\n\" | \
/usr/bin/python"];
  DeleteFile[out]
]

The idea is to export to RTF and read the result to the clipboard outside of Mathematica. The function is invoked for example as copyAsUnicode["αβ+Mod[δΨ+ρ2]"]. This example itself was copied that way, too, i.e., I typed copyAsUnicode["copyAsUnicode[\"αβ+Mod[δΨ+ρ2]\"]"], which I again copied the same way... OK, I think you get the idea.

Of course the next step would be to make this into a Palette that acts on the NotebookSelection, but the above is the main step. Maybe someone else knows how to do something like this in other operating systems (I don't).

share|improve this answer
This looks good, but since I cannot test it on Windows I will leave it to Mac users to upvote. Thank you for your answer. – Mr.Wizard Feb 2 '12 at 4:27

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.