I have the following code:

a = 6.08717*10^6;
b = a/3;
c = a*1.5;
d = a^2;
matrix={{a, b, c, d},{b, c, d, a},{c, d, a, b},{d, a, b, c}};
matrix // EngineeringForm

Normally, I use this result by copying (Copy As ► MathML) and pasting into Microsoft Word.

However, I do not get the desired formatting.

The image below shows what the result is presented in Microsoft Word:

enter image description here

The image below shows the result that I want:

enter image description here

1 - Comma instead of point

2 - Without quotation marks

3 - Four decimal places

4 - Point instead of the Cross

  • 2
    matrix = {{a, b, c, d}, {b, c, d, a}, {c, d, a, b}, {d, a, b, c}}; NumberForm[matrix, {5, 4}, NumberPoint -> ",", NumberMultiplier -> ".", ExponentStep -> 3] That gets everything except the quotes in the pasted values. – N.J.Evans Jun 9 '16 at 21:08
  • Depending on how often you have to do this, you can find/replace the quotes from word pretty easily once you've got that much. Also, change the 5 in {5,4} to a 7. – N.J.Evans Jun 9 '16 at 21:14
  • 1
    I would use \[CenterDot] instead of the point as multiplier. – Sjoerd C. de Vries Jun 10 '16 at 10:05
  • 2
    To the closers: whereas most of the required formatting can be arranged with standard, documented options, this does not hold for the quotes that remain in Word. There isn't a straightforward solution to that AFAIK; so, no reason to close this question. – Sjoerd C. de Vries Jun 10 '16 at 10:11
  • @LCarvalho I've updated the answer with even better method. – Alexey Popkov Nov 8 at 8:44
up vote 8 down vote accepted

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]

output

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]

output

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:

screenshot

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]

screenshot


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]}]

output

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]

output

Copying the last output, pasting it into a new input cell and evaluating gives:

screenshot

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.

MathML export works by creating TraditionalForm boxes and then converting those boxes into MathML. The "Copy As MathML" menu item, on the other hand, takes the boxes in the cell to be copied, and converts those boxes to MathML.

Alexey's answer basically worked by modifying the expression so that the boxes corresponding to the expression avoided issues related to extra quotes. This is a bit inconvenient because you always have to remember to do this modification before using Export or "Copy as MathML".

It would be convenient to instead do this transformation automatically.

Before proceeding with an answer, let's see what goes wrong:

ExportString[EngineeringForm[3.15*^19], "MathML"]
<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <semantics>
  <mrow>
   <ms>31.5</ms>
   <mo>&#215;</mo>
   <msup>
    <mn>10</mn>
    <ms>18</ms>
   </msup>
  </mrow>
  <annotation \
encoding='Mathematica'>TagBox[InterpretationBox[RowBox[List[&quot;\
\\&quot;31.5\\&quot;&quot;, &quot;\\[Times]&quot;, \
SuperscriptBox[&quot;10&quot;, &quot;\\&quot;18\\&quot;&quot;]]], \
3.15`*^19, Rule[AutoDelete, True]], EngineeringForm]</annotation>
 </semantics>
</math>

Notice the string tagging: <ms>31.5</ms> and <ms>18</ms>. These need to be <mn>31.5</mn> and <mb>18</mn> instead.

The difference in tagging is related to extra quote characters in the boxes. We can eliminate the extra quote characters by using Alexey' s rule. Here is a function that does this:

fix[i:InterpretationBox[_, n_, ___]] := If[MatchQ[n, _Real | _Integer],
    ReplaceAll[
        i,
        s_String :> RuleCondition[StringTake[s, {2, -2}], StringMatchQ[s,"\""~~__~~"\""]]
    ],
    i
]

Next, the internal function that converts boxes to MathML is XML`MathML`BoxesToSymbolicMathML. We can modify it to post-process the boxes using the above function:

Unprotect[XML`MathML`BoxesToSymbolicMathML];
XML`MathML`BoxesToSymbolicMathML[
    boxes_?System`Convert`MathMLDump`boxStructureQ,
    o___?(System`Convert`MathMLDump`optionOfQ[XML`MathML`BoxesToSymbolicMathML])
] /; !TrueQ @ $MathML := Block[{$MathML=True},
    XML`MathML`BoxesToSymbolicMathML[
        boxes /. i_InterpretationBox :> fix[i],
        o
    ]
]
Protect[XML`MathML`BoxesToSymbolicMathML];

Now, MathML output should be as you desired. In each of the following examples, note that the tags used are <mn> and </mn>.

Using MathMLForm:

ToString[EngineeringForm[3.14*^19], MathMLForm]
<math>
 <semantics>
  <mrow>
   <mn>31.4</mn>
   <mo>&#215;</mo>
   <msup>
    <mn>10</mn>
    <mn>18</mn>
   </msup>
  </mrow>
  <annotation \
encoding='Mathematica'>TagBox[InterpretationBox[RowBox[List[&quot;31.4&quot;, \
&quot;\\[Times]&quot;, SuperscriptBox[&quot;10&quot;, &quot;18&quot;]]], \
3.14`*^19, Rule[AutoDelete, True]], EngineeringForm]</annotation>
 </semantics>
</math>

Using Export:

ExportString[EngineeringForm[3.14*^19], "MathML"]
<math xmlns='http://www.w3.org/1998/Math/MathML'>
 <semantics>
  <mrow>
   <mn>31.4</mn>
   <mo>&#215;</mo>
   <msup>
    <mn>10</mn>
    <mn>18</mn>
   </msup>
  </mrow>
  <annotation \
encoding='Mathematica'>TagBox[InterpretationBox[RowBox[List[&quot;31.4&quot;, \
&quot;\\[Times]&quot;, SuperscriptBox[&quot;10&quot;, &quot;18&quot;]]], \
3.14`*^19, Rule[AutoDelete, True]], EngineeringForm]</annotation>
 </semantics>
</math>

And using the "Copy As MathML" menu item:

EngineeringForm[3.24*^20]

enter image description here

SelectionMove[PreviousCell[],All,Cell];
FrontEnd`CopyAsMathML[];
First @ NotebookImport[
    NotebookGet[ClipboardNotebook[]],
    "Input" -> "Text"
]
<math xmlns='http://www.w3.org/1998/Math/MathML'
    mathematica:form='StandardForm'
    xmlns:mathematica='http://www.wolfram.com/XML/'>
 <semantics>
  <mrow>
   <mn>324.</mn>
   <mo>&#215;</mo>
   <msup>
    <mn>10</mn>
    <mn>18</mn>
   </msup>
  </mrow>
  <annotation \
encoding='Mathematica'>TagBox[InterpretationBox[RowBox[List[&quot;324.&quot;, \
&quot;*&quot;, SuperscriptBox[&quot;10&quot;, &quot;18&quot;]]], 3.24`*^20, \
Rule[AutoDelete, True]], EngineeringForm]</annotation>
 </semantics>
</math>
  • (+1) Is there a way to make "MathML" the default clipboard format for a certain Cell, something like TextClipboardType -> "MathML" (what currently isn't supported)? – Alexey Popkov Nov 12 at 11:19
  • @AlexeyPopkov Not as far as I'm aware. Maybe you could use "Package" and have something like TemplateBox[{"display", "mathml"}, "MathML", DisplayFunction->(#&), InterpretationFunction->(#2&)]? – Carl Woll Nov 12 at 17:06

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.