How to copy formatted numbers as MathML without quotation marks?
Original answer
The reason for getting extra quotes is that these extra quotes are explicitly present in the box form of the expression generated by such functions as EngineeringForm
, NumberForm
etc.:
ToBoxes@EngineeringForm[6.08717*10^6]
TagBox[InterpretationBox[ RowBox[{"\"6.08717\"", "\[Times]", SuperscriptBox["10", "\"6\""]}], 6.08717*10^6, AutoDelete -> True], EngineeringForm]
We can post-process these raw boxes and remove extra quotes using the removeExtraQuotes
function defined as follows (see this for description of RuleCondition
):
removeExtraQuotes =
RawBoxes[ToBoxes[#] /.
n_String :>
RuleCondition[StringTake[n, {2, -2}],
StringMatchQ[n, "\"" ~~ __ ~~ "\""]]] &;
Now we can proceed as follows:
removeExtraQuotes@
NumberForm[Grid[matrix], {7, 4}, NumberPoint -> ",",
NumberMultiplier -> "\[CenterDot]", ExponentStep -> 3]
After copying the output using Copy As ► MathML
we get in the clipboard the MathML expression without extra quotes.
Further development
The original answer solves the problem as stated but I was worrying about slightly wrong formatting of the numbers when they are displayed inside of a Notebook: as seen from the above screenshot, the space between the comma ,
and the fractional part of the number is too large. So I decided to try to overcome this problem.
Here is an improved version of removeExtraQuotes
which generates output correctly displayed inside of a Notebook (the only change is that now I wrap the string by Cell
effectively creating an inline plain text Cell
):
removeExtraQuotes =
RawBoxes[ToBoxes[#] /.
n_String :>
RuleCondition[Cell@StringTake[n, {2, -2}],
StringMatchQ[n, "\"" ~~ __ ~~ "\""]]] &;
Testing:
removeExtraQuotes@
NumberForm[Grid[matrix], {7, 4}, NumberPoint -> ",",
NumberMultiplier -> "\[CenterDot]", ExponentStep -> 3]
As one can see, the extra spacing between the comma "," and the fractional part disappeared. But the spaces around the multiplication symbol are larger than needed.
I have copied the output using Copy As ► MathML
and then successfully pasted in MathType 6.9b. Here is what I got:
As one can see the result is almost perfect. The only shortcoming is that the multiplication symbol (center dot) is seemingly taken from the Times New Roman font while when creating this symbol using the MathType interface it is taken from the Symbol font and looks slightly different.
UPDATE from 08.11.2018: a perfect solution
The way to remove all the extra spacing is to apply the AutoSpacing -> False
option:
removeExtraQuotes =
RawBoxes[ToBoxes[#] /.
n_String :>
RuleCondition[StringTake[n, {2, -2}], StringMatchQ[n, "\"" ~~ __ ~~ "\""]]] &;
removeExtraQuotes@
NumberForm[Grid[matrix, BaseStyle -> AutoSpacing -> False], {7, 4}, NumberPoint -> ",",
NumberMultiplier -> "\[CenterDot]", ExponentStep -> 3]
For those who want to develop their own NumberForm
My experiments (with Mathematica 10.4.1) showed that the most nice-looking and at the same time interoperable method of creating formatted numbers is to make an inline Cell
for the whole number with TextData
as a container. Inside of TextData
boxes can only be present inside of a Cell
what complicates things a bit but the resulting expression is rendered much better than the built-in NumberForm
output (which uses RowBox
instead of TextData
):
Column[{RawBoxes@
Cell[TextData[{"6", ",", "0872", "\[CenterDot]",
Cell[BoxData[SuperscriptBox["10", "6"]]]}]],
NumberForm[6.08717*10^6, {7, 4}, NumberPoint -> ",",
NumberMultiplier -> "\[CenterDot]", ExponentStep -> 3]}]
Of course one can place the inline Cell
inside of InterpretationBox
as it is implemented for NumberForm
and wrap the whole thing with TagBox
in order to achieve predictable formatting:
RawBoxes@TagBox[
InterpretationBox[
Cell[TextData[{"6", ",", "0872", "\[CenterDot]",
Cell[BoxData[SuperscriptBox[10, 6]]]}]], 6.08717`*^6,
AutoDelete -> True], NumberForm]
Copying the last output, pasting it into a new input cell and evaluating gives:
As one can see inside of input cell it preserves nice formatting. At the same time it can be copied as MathML
and then pasted into MathType (or other MathML
-compatible application) without extra quotes and retaining superscripts, subscripts etc.
P.S.: It disappoints that TextClipboardType -> "MathML"
option for Cell
isn't supported.