cmiles – blog

Charles Miles – Tucson Hiking, Outside and Adventures, Excel, .net, Life

Some Experiences in the Display of Mathematical Equations in WPF

I recently needed to display ‘notes’ in a WPF application – the interesting detail was that the notes contained both text and mathematical equations. I did not find a tremendous amount of information about presenting mathematical equations in WPF – so I thought I would share my experiences.

The notes for the application were delivered by the client as a Microsoft Word document.

Because I do not have any experience with Adobe Illustrator or any of the Expression products none of the solutions I investigated involves those products.

 

jsMath – My first idea was to display the notes in a browser control with the hope of reusing them on the web. I quickly found jsMath which generates equations from TeX input (if formulas in TeX are not a familiar idea see Help:Displaying a Formula). Setup was quick and my test page looked nice in Firefox. However, the rendering in the WPF Browser control was unacceptable. It is completely possible that I did not have something configured correctly and/or that there were settings I needed to change, but I decided to move on…

 

MathML – As an alternative to having jsMath handle the equations I briefly looked into using MathML. Unfortunately this page failed to open correctly on my machine in IE8, Chrome, Safari and Opera. That was enough to steer me away from MathML. The page opened without problems in Firefox 3.5…

 

WPF Chromium WebBrowser control – I was not quite ready to give up on the idea of presenting the notes in a browser control with jsMath and thought the rendering in this control might be better. I did not get very far with this and did not try the control in my project – not because of problems with the control, but because the Awesomium project that this control uses is going closed source. I was unsure about the future of the WPF Chromium WebBrowser control without updated versions of Awesomium so I moved on to my next idea.

 

DocumentViewer -  I already had nicely formatted text and equations in the original Word document and thought that I might be able to leverage that. The WPF DocumentViewer control can display XPS documents – and Word documents can be saved as XPS. I saved the first note from the Word document as a .XPS file and – after figuring out that System.Windows.Xps.Packaging is in ReachFramework.dll – quickly put together a test. The file displayed quickly and easily (including equations) and for some applications this could be a great option!  However, I felt the DocumentViewer was too page and print focused for this application. I do not think – at least in it’s default configuration – that the DocumentViewer is a very good solution for displaying shorter items for easy on-screen reading (esp. in a smaller screen area). I am sure I could have tweaked options to improve the DocumentViewer for use in this application, but this seemed like the wrong approach.

 

Word/XPS converted to FlowDocument – I thought these articles were interesting, but was skeptical about the time/results, and did not try this approach – Converting FixedDocument (XPSDocument too) to FlowDocument and WordML to FlowDocument – how to convert docx files to WPF FlowDocument. I would be really curious to hear from anyone how the conversion of Word equations works with the ideas/code presented in these articles.

 

Flow Document  + Images – Images seem like an obvious/traditional choice for presenting the equations and the FlowDocument seemed like an interesting choice for presenting text and images for on-screen reading. One easy way to produce the images is Roger’s Online Equation Editor which transforms LaTeX equations into images. It is easy to work with images in WPF – but the problem I had was getting the images to look as sharp as the text in the FlowDocument when they were resized. If this was a document for printing, or had specific static layout requirements, I think that the images would have worked great. But as the images were resized (either because of dynamic layout changes or changes to the applications design) I was not able to keep them as sharp as the surrounding text. If I had more graphics expertise I may have been able to find a better image type/resolution/rescaling combination.

 

FlowDocument + Shapes – a good friend suggested that I look for ways to represent the equations as SVG. What I eventually found was Inkscape + textext. textext works as an add-in to Inkscape that allows you to render LaTeX. The install and troubleshooting steps near the bottom of the textext page are critical in finding the dependencies (MikTex, Pstoedit, Ghostscript) and dealing with errors (I had to add a directory to my path to get textext working). Once it is up and running textext worked great – it is very easy to create an equation and save it as an SVG file. The next problem is moving the equation from SVG into XAML.

 

SVG to XAML Canvas – All of the methods below can be used to convert an SVG file  into shapes in a XAML Canvas object. In the case of the equations I was producing all of the solutions below generally produced usable output. The Canvas output could be inserted directly into your application – or it could be placed in a resource file by stripping out the Name elements and adding a Key. In addition to using the Canvas directly it could also be used as a VisualBrush.

Xsl – I was able to use the xsl on this page to get canvas based output (and do a some xsl debugging in Visual Studio – a great feature and the first time I have used it).

Inkscape – Inkscape has save as XAML built-in. In the past, for simple images, this worked great for me. For the equations that I was producing in this project the canvas that Inkscape produced apparently lacked a RenderTransform that was crucial to correct display. The problem was fairly easy to fix, but mainly because I had the output from XamlTune as a comparison.

XamlTune – The command line executable in this project worked great – and could potentially be integrated into a build script which is a nice bonus. I had trouble with the gui portion of this project crashing.

The Canvas with shapes output is ok – but I want to reuse the same equation multiple times, use the same equations both as larger stand-alone figures and inline with the text and rescale the larger images as the application’s size changes. I assume that I could get the canvas based resources working for all of those requirements (although I not sure exactly how and imagine a bit of awkward code…). However, I know that having the equations as DrawingImages in a resource file would allow me to use the equations inside an Image tag, solving the problems above.

 

FlowDocument + DrawingImage – The only programs I found that produced DrawingImage output from SVG input were ViewerSvg Professional or Ab2d.ReaderSvg Professional (the ‘Basic’ versions are not listed as having ‘Export as Geometry’ support) from WPF Graphics (demo downloads available – note these will also convert from SVG to a Canvas). This software did nice conversions from SVG to DrawingImage with a good array of options. The Ab2d.ReaderSvg class library also includes a sample application that allows you to combine multiple SVGs into DrawingImages inside a ResourceDictionary, which is almost exactly what I wanted to do! For some situations these applications will not be appropriate since they are commercially licensed software with a cost of $169/$399.

 

Conclusion: The options above present several viable solutions for presenting mathematical equations in a WPF application. Every project will have different requirements that will dictate the best solution. With that said – in my opinion -  the combination of Inkscape and textext to create equations in SVG and Ab2d.ReaderSvg Professional to convert the SVG into DrawingImage resources is the best combination currently available for presenting mathematical  equations for on-screen reading in a WPF application.

 

Enjoy!

CM

Advertisement

Filed under: .net, , , , ,

3 Responses

  1. sedat says:

    i ve written an equation editor see http://www.sedatkurtoglu.com for previews , i will publish it soon…

  2. Jan Kučera says:

    Have you checked the assemblies included in Microsoft Mathematics?

  3. Daniel says:

    Good post ! if there is a demo , that will be great !!

Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

twitter -> twitterings

  • Just saw the new Resharper 6.1 Early Access version has Async CTP support listed - downloading now... 2 months ago
  • Quick post about FeedDemon/Pinboard/Save to Pinboard on Android as my choice for Google Reader Sharing http://t.co/EupWQupM 3 months ago
  • FeedDemon+Custom Sharing XML->Pinboard; NewsRob + Save to Pinboard on Android - replacing Google Reader sharing, Reader now just for sync... 3 months ago
  • Doing a few Max - http://t.co/wL8dpWk4 - tutorials - what fun, first time in a decade I have played with this... 4 months ago

RSS pinboard -> links

RSS cmiles-consuming -> posts

  • Beats, Rhymes & Life: The Travels of a Tribe Called Quest 2012 January 30
    I have no idea when I first heard a Tribe Called Quest (official site) – but it was the early 2000s before I really ‘found’ them and started listening. I would not call myself a devotee – casual fan is probably the best description – but even as a casual fan I was excited when [...]
  • Hull Zero Three, Greg Bear 2012 January 15
    I am sad to say that the first time I saw Hull Zero Three by Greg Bear (official site) on Amazon I skipped over it because of the rating – in retrospect a somewhat sad reminder to myself about the value of ratings… Thankfully on the Potpourri of Science Fiction Literature blog I came across [...]
  • Happy New Year! End of 2011 Notes… 2012 January 1
    This blog made it thru 2011! A few notes about my media/reading that I thought might be fun if this blogs lasts a few years… -Reading: I am mostly reading on my Sprint Evo 4G Android Phone via the Kindle app. While the reading experience on such a small screen is unexciting the compelling feature [...]
  • Revelation Space Universe, Alastair Reynolds 2011 December 31
    I don’t have a good enough memory or record of what I was reading in the late 1990s or early 2000s to know if this is the truth – but the way I remember it is that after a lull where I had trouble finding any science fiction I was interested in reading I came [...]
  • Sea of Glass, Barry B. Longyear 2011 December 20
    I don’t remember seeing Sea of Glass, by Barry B. Longyear, on ‘top’ science fiction lists – or stumbling across it in website recommendations; but I do remember this novel from reading it in (about…) 1990. What I remember is the brutality, terror and a dystopian future world on the brink of war. The novel [...]
  • String Quartets 2 & 3, Kevin Volans, Balanescu Quartet, Kronos Quartet 2011 December 12
    I believe I first heard Kevin Volans‘s (homepage) String Quartet No. 2 – ‘Hunting: Gathering’ in the mid-1990s on a Kronos Quartet CD. While I can not say this was immediately one of my favorite pieces, I will say that sounds from and sections of the 2nd String Quartet have stayed with me – coming [...]
  • Norwegian Wood, Haruki Murakami 2011 December 6
    Norwegian Wood was not quite what I was expecting – the Murakami novels that I have read – A Wild Sheep Chase, Hard-Boiled Wonderland and the End of the World, Dance Dance Dance, The Wind-Up Bird Chronicle, Sputnik Sweetheart, Kafka on the Shore and After Dark – all seem to me to have some place [...]
  • Looking Glass, James R Strickland 2011 November 27
    Cyberpunk! James Strickland delivers the classic elements in Looking Glass – a future United States now carved into different countries, powerful corporations, cyber space, techy jargon, decks, jacking in and action! The strength of this novel is not in offering something insightful and new – but rather in being an intelligent and fascinating rec […]
  • Await Your Reply, Dan Chaon 2011 November 21
    Dan Chaon gives us a clever plot – with several converging stories – and disturbed characters that work together to create an enjoyable and slightly uncomfortable novel. There were sections that certainly made me think about my own life and identity, but overall I was not quite completely hooked/pulled in. Rating: 3 of 5 First [...]
  • Running the Sahara 2011 November 16
    Running the Sahara – directed by James Moll – follows three athletes as they run across the Sahara Desert. They run (and sometimes walk) thru Senegal, Mauritania, Mali, Niger, Libya and Egypt. The film does a great job of showing some of the amazing landscapes that they cross – with small glimpses of the people [...]
email: charles@cmiles.info

flickr -> pictures

1202 Group Picture 2 after the Colossal Cave Run

1202 Group Picture 1 after the Colossal Cave Race

1202 Charles And Joe After the Colossal Cave Run

1201 View from about 4 miles down the trail

1201 Water coming down Sycamore Dam

1201 Life of an Outdoor Footwear Buyer

1201 Blacketts Ridge Night Run, Dana near the top

1201 Arizona Trail in the Colossal Cave Area (Rincon Valley)

1201 Richard Coming Up to hill after the turn off the AZ Trail

1112 Sunset from Pontatoc Canyon

More Photos
Follow

Get every new post delivered to your Inbox.