stanch
stanch

Reputation: 325

Convert MathML to MathType in MS Word

I am writing a converter from XML&MathML to MS Word document.
I'm using MFC and Word automation, so there's no problem in writing text like this:

_Application app;  
COleVariant vtOpt(DISP_E_PARAMNOTFOUND, VT_ERROR),
            vtTrue((short)TRUE),
            vtFalse((short)FALSE);
app.CreateDispatch("Word.Application",NULL);
Documents docs = app.GetDocuments();
_Document doc = docs.Add (vtOpt, vtOpt, vtOpt, vtOpt);
Range range = doc.Range (vtOpt, vtOpt);
range.InsertAfter (_T("Hello Word!"));

Now the problem is in converting MathML equations into embedded MathType objects. One of the possible ways, that I've found, is to write equations in TeX and then programmatically call MTCommand_TeXToggle (found in MathType 6.5 library for Word) macro, that replaces TeX with MathType OLE objects. But then I have to convert MathML to TeX somehow, wich is not so easy.

I know, that MathType OLE object should accept raw MathML data, but when I'm trying to create and access OLE object programmatically:

InlineShapes shapes = doc.GetInlineShapes ();
InlineShape control = shapes.AddOLEObject (COleVariant("Equation.DSMT4"), vtOpt, vtFalse, vtFalse, vtOpt, vtOpt, vtOpt, vtOpt);
OLEFormat fm = control.GetOLEFormat ();
COleDispatchDriver drv = fm.GetObject();

I end up with no reasonable interface to feed it with the MathML data. So, the question is: 1) Is there a way to get control over the OLE object and send it some MathML data? Or 2) Is there a way to get a MS Word VB macro, that converts the selection from MathML to MathType OLE object?

Thanks in advance, Nick Stanch

Upvotes: 6

Views: 9676

Answers (4)

PericoSam
PericoSam

Reputation: 1

Following simple procedure works:

  • Configure MathType to copy/paste preferences to: "Mathml" (suitable version to be chosen)
  • Paste your Mathml equation into Mathtype panel : the equation appears and can be edited/modified -> Fine adjust your equation.
  • Reconfigure copy/paste preferences of Mathtype to "Equation Object (Windows OLE Graphic)" -Copy paste now the equation from MathType to Word

Upvotes: 0

stanch
stanch

Reputation: 325

Cracked it!

One can use COleClientItem object to obtain an instance of MathType OLE Control in his code. That is shown in the MathType SDK MFC sample. Then, manipulating the COleDataSource, feed the control with MathML data and call COleClientItem::CopyToClipboard(); Now the data, representing the control itself, as an OLE object, is available upon Range::Paste(opt1, opt2); call from Word automation classes. Looks a bit weird, but worked for me :) And all you have to do is add one more line to the sample, namely the one that calls CopyToClipboard method.

Upvotes: 4

Bob Mathews
Bob Mathews

Reputation:

Nick, you should try our MathType SDK. It's offered "as-is", but it's free: http://www.dessci.com/en/reference/sdk/

Roel, thanks for the kind words. I'll pass this along to our Support team.

Bob Mathews Design Science

Update: Here's some clarification and additional information about my recommendation to try the MathType SDK. Starting with MathType 6.5, one can use the IDataObject interface on a MathType object to pass in MathML. The expression the MathML encodes is inserted at the current cursor location. Thus, in general, the strategy is to insert a blank MathType equation, activate it, get the IDataObject interface, set the equation via MathML, and then close the equation again.

The MathType SDK includes documentation with further detail, and a sample "OLECont" application demonstrating the technique. Based on what you've posted here, you should have no problem getting your code working.

Upvotes: 4

Roel
Roel

Reputation: 19612

The only advice I can offer is that I've always found the Design Science support to be quite good. You may want to ask them whether their OLE interface offers MathML support.

I haven't investigated the Word 2007 equation editor yet. Maybe it can import other formats more easily, if it has a different automation interface.

Upvotes: 0

Related Questions