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.

Consider the following setup, based on this answer by WReach:

eval = (ToString[#] &)

CellPrint[
 Cell[TextData["őúű"], "Program", 
  Evaluatable -> True,       
  CellGroupingRules -> "InputGrouping",
  CellEvaluationFunction -> eval]]

This will create a cell that, when evaluated, will simply output its own contents, converted to a string. My goal is to preserve non-ASCII characters during this conversion.

Notice that with the example contents I included, I get this string as output

"\[ODoubleAcute]\[UAcute]\[UDoubleAcute]"

This is not the same as the input, "őúű". The difference is not just in display, it's actually a different string as you can verify using StringLength or exporting the string to a file. What I need to get is:

"őúű"

i.e. a string of length 3.

How can I get this?


EDIT: Inspecting the cell expression shows that it does indeed contain the 39-character string "\[ODoubleAcute]\[UAcute]\[UDoubleAcute]". So the key to the solution may be figuring out how to convert this to a unicode string containing those characters.

EDIT 2: I found the following way to convert named characters in a string str to actual unicode characters. But this is undocumented, I don't know where it may go wrong, and I'm sure there must be something better ...

First@FrontEndExecute[FrontEnd`ExportPacket[Cell[str], "InputText"]]
share|improve this question
add comment

1 Answer

I'm not sure this answers

You won't get this to work with a text cell. Your CellEvaluationFunction already receives a parsed string.

If you use an inline text cell you can get it to work

CellPrint@
 Cell[BoxData@Cell@TextData["őúű"], "Program", Evaluatable -> True, 
  CellGroupingRules -> "InputGrouping", 
  CellEvaluationFunction :> (#[[1, 1]] &)]

But you can only write in the inline text cell. Otherwise

CellPrint@
 Cell[BoxData[""], "Program", Evaluatable -> True, 
  CellGroupingRules -> "InputGrouping", 
  CellEvaluationFunction :> (FE`makePlainText@First@# &)]
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.